Known (only one known condition):
- Monday, January 1, 1900
Functions implemented:
- Write a corresponding year and month by this condition, and then output the corresponding month and month calendar in the console.
Basic ideas:
-
It is known that January 1, 1900 is Monday. The first thing we need to know to export this month's calendar is what day it is on January 1, so that we can arrange the position of the first day of this month's calendar.
The number of days from January 1, 1900 to January 1, 1900, is set to days. If the balance of 7 days is taken, the date of January 1 can be calculated as the week of this month. 0 Monday | 1 Tuesday | 2 Wednesdays | 3 Thursdays | 4 Fridays | 5 Saturdays | 6 Sundays Days = input year - 1900) * 365 (if leap year is added one day) Judgment conditions for leap years (years divisible by four but not by 100 or by 400)
-
The second point is to know how many days there are in this month.
The number of days in this month is judged from January to December.
-
The third point is to switch to the next line every time after the output of the date on Saturdays (Sunday is the first day of the week).
The method of judging the date of the week is the same as that of judging No. 1. For convenience, we can know the time of today by adding the date of today to the date of the first month directly and subtracting one from the date of today. The time difference between January 1, 1900 and 7 is the date of the week.
Here is the code written by the individual:
public class WanNianLi { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Welcome interface System.out.println("*************************"); System.out.println("******** Perpetual calendar *********"); System.out.println("*************************"); System.out.print("Please enter the year:"); // Particular year int year = input.nextInt(); System.out.print("Please enter month:"); // Month int month = input.nextInt(); boolean isRun = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; /** * What is the date of January 1 of the current month from January 1, 1900? */ int dates = 0; for (int i = 1900; i < year; i++) { dates += 365; //Judging whether it is a leap year or not, if it is a leap year, it is on the basis of dates + 1 day. if ((i % 4 == 0 && i % 100 != 00) || (i % 400 == 0)) { dates += 1; } // System.out.println(i + "year:" + dates); } // System.out.println(dates); // 31 days in January, March, May, July, August and October, 30 days in March, June, September and November, 28 days in February and 29 days in leap year. for (int i = 1; i < month; i++) { switch (i) { case 1: case 3: case 5: case 7: case 8: case 10: dates += 31; break; case 4: case 6: case 9: case 11: dates += 30; break; case 2: if (isRun) { dates += 29; } else { dates += 28; } break; } } // System.out.println(dates); System.out.println("Sunday\t Monday\t Tuesday\t Wednesday\t Thursday\t Friday\t Saturday"); /** * 1 Number needs empty tabs */ int week = dates % 7; switch (week) { case 0: System.out.print("\t");// Monday break; case 1: System.out.print("\t\t");// Tuesday break; case 2: System.out.print("\t\t\t");// Wednesday break; case 3: System.out.print("\t\t\t\t");// Thursday break; case 4: System.out.print("\t\t\t\t\t");// Friday break; case 5: System.out.print("\t\t\t\t\t\t");// Saturday break; default: break; } /** * Choose the number of days to output this month */ int days = 31; switch (month) { case 4: case 6: case 9: case 11: days = 30; break; case 2: if (isRun) { days = 29; } else { days = 28; } break; } /** * Cyclic output for each day of the month. Judge if it's Saturdays. */ for (int i = 1; i <= days; i++) { System.out.print(i + "\t"); if ((dates + i - 1) % 7 == 5) { System.out.println(); } } } }
The result demonstrates: