Calculate the day of the week for a date using Java
- 2020-05-10 18:15:31
- OfStack
Without further ado, let's go straight to the example code
Specific code:
DayOfWeek4Birthday.java
package com.gua;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.*;
import static java.lang.System.out;
/**
* Created by 2gua on 2014/9/27.
* DayOfWeek4Birthday : depending on what day of the week you are looking for,
* For example, look up "what day of the week is your birthday?"
*/
public class DayOfWeek4Birthday {
private String[] date; // Save input data: year, month, date.
// Calculate the day of the week for which the date data is entered.
private void caculateData() {
GregorianCalendar gc = new GregorianCalendar();
final char[] day_of_week = {' day ','1','2','3','4','5','6'};
int year = gc.get(Calendar.YEAR); // From the current year.
char week;
for(int i = year; i<= year + Integer.valueOf(date[0]) - 1; i++) {
gc.set(i, Integer.valueOf(date[1]) - 1, Integer.valueOf(date[2]));
week = day_of_week[gc.get(Calendar.DAY_OF_WEEK) - 1];
out.println(i + " years " + date[1] + " month " + date[2] + " Number is week " + week + " . ");
}
}
// Enter date data.
private void inputData() {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
try {
out.print(" Please enter year range and date, format: year number A few months Number ( 5 9 15 ), enter the end: ");
String in = br.readLine();
date = in.split(" ");
out.println(" Input results: " + date[0] + " A number of years, " + date[1] + " month " + date[2] + " Number. ");
}
catch(Exception e) {
out.println(" Uh-oh, something went wrong -_- . sorry ! ");
} finally {
try {
br.close();
is.close();
} catch (IOException e) {
out.println("IO Error! -_- . sorry ! ");
}
}
}
public static void main(String[] args) {
DayOfWeek4Birthday dayOfWeek4Birthday = new DayOfWeek4Birthday();
dayOfWeek4Birthday.inputData();
dayOfWeek4Birthday.caculateData();
}
}
Let's say I want to see what day of the week September 15th is every year for five years from this year.
The results are as follows:
Please enter year range and date, format: year number A few months Number ( 5 9 15 ), enter the end: 5 9 15
Input results: 5 A number of years, 9 month 15 Number.
2014 years 9 month 15 Number is week 1 .
2015 years 9 month 15 Number is week 2 .
2016 years 9 month 15 Number is week 4 .
2017 years 9 month 15 Number is week 5 .
2018 years 9 month 15 Number is week 6 .
Process finished with exit code 0
Remember to close the stream after use.
The above is the practice of JDK 6, which can be used in JDK 7 and JDK 8
automatic resource management(ARM)
New features, refactoring
inputData()
Methods:
// Enter date data.
private void inputData() {
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
out.print(" Please enter year range and date, format: year number A few months Number ( 5 9 15 ), enter the end: ");
String in = br.readLine();
date = in.split(" ");
out.println(" Input results: " + date[0] + " A number of years, " + date[1] + " month " + date[2] + " Number. ");
}
catch(Exception e) {
out.println(" Uh-oh, something went wrong -_- . sorry ! ");
}
}
Of course, remember to set the module language level accordingly to JDK 7 or JDK 8. JDK 6 and below will not pass.
conclusion
The above is the whole content of this article, I hope the content of this article can help you to learn or use Java, if you have any questions, you can leave a message to communicate.