Without further ado, let's take a look at the sample code directly
Specific code:
DayOfWeek4Birthday.javapackage 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: Look at the specific day you are querying, * For example, check who is "the birthday corresponds to the day of the week". */public class DayOfWeek4Birthday { private String[] date; //Save input data: year, month, and date. // Calculate the day of the week corresponding to the input date data. private void caculateData() { GregorianCalendar gc = new GregorianCalendar(); final char[] day_of_week = {'day','one','two','three','four','five','six'}; 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 + "year" + date[1] + "month" + date[2] + "sign is the week" + week + ". "); } } //Input date data. private void inputData() { InputStreamReader is = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(is); try { out.print("Please enter the year range and date, format: what month and what day of the year (5 9 15), end of the carriage return: "); String in = br.readLine(); date = in.split(""); out.println("Input result: " + date[0] + "number of years," + date[1] + "month" + date[2] + "sign."); } catch(Exception e) { out.println("Ah, there was an error in running -_-.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(); }}If you want to run, for example, if you want to see the 5 years from this year, September 15th is the week of the week.
The results are as follows:
Please enter the year range and date, format: what month and day of the year (5 9 15), and the end of the carriage entry: 5 9 15 Enter the result: 5 years, September 15. September 15, 2014 is Monday. September 15, 2015 is Tuesday. September 15, 2016 is Thursday. September 15, 2017 is Friday. September 15, 2018 is Saturday. Process finished with exit code 0
Remember to close the stream after running out.
The above is the practice of JDK 6. In JDK 7 and JDK 8, you can use the new automatic resource management(ARM) feature to reconstruct inputData() method:
//Enter date data. private void inputData() { try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { out.print("Please enter the year range and date, format: what month and day of the year (5 9 15), end of the carriage return: "); String in = br.readLine(); date = in.split(" "); out.println("Input result: " + date[0] + "years," + date[1] + "month" + date[2] + "sign."); } catch(Exception e) { out.println("Oh, there was an error in running-_-.sorry!"); }}Of course, remember to set the module language level to JDK 7 or JDK 8 accordingly, and JDK 6 and below will not pass.
Summarize
The above is the entire content of this article. I hope the content of this article will be helpful to everyone in learning or using Java. If you have any questions, you can leave a message to communicate.