Java Perpetual calendar, input year/year to obtain year/year calendars, and java Perpetual Calendar
1 // enter the year and month, and print the calendar table for this month 2/* 3 1.1900-January 1 is Monday 4 2. calculate the number of days from January 1, 1900, and then calculate the number of days from January 1, January 1 to the current month. the total number of days % 7 is derived from the day of the week. Note: The time in the computer is as short as 1900. In addition, the UNIX system considers January 1, 1970 as the time epoch. 8 so, I will not take the year before 1900 in this program. If you are interested, you can study it on your own. 9 */10 import java. util. california; 11 class Calender {12 13 public static void main (String [] args) {14 15 print (); 16 17} 18 19 // print the output 20 public static void print () {21 then SC = new then (System. in); 22 System. out. println ("Enter the year:"); 23 int year = SC. nextInt (); 24 System. out. println ("Enter the month (1 ~ 12): "); 25 int month = SC. nextInt (); 26 27 int days = getDays (year, month); // For details about the getDays method, see 28 // days + 1: day is the total number of days, the total number of days of the input month is only the number of days before the month. 29 // Add 1 to the first day of the month. 30 int week = days % 7 = 0? 1: days % 7 + 1; // the first day of the week is the day of the week 31 System. out. println ("day \ t one \ t Two \ t three \ t four \ t five \ t six"); 32 33 // output the first line (the first week) null Part 34 for (int I = 1; I <= week; I ++) {35 System. out. print ("\ t"); 36} 37 38 // output the first line (the first week) each day 39 for (int I = 1; I <= 7-week; I ++) {40 System. out. print (I + "\ t"); 41} 42 43 System. out. println (); 44 45 // 1 ~ The number of months in March 46 int monthDay = 0; 47 switch (month) {48 case 2: 49 if (year % 4 = 0 & year % 100! = 0 | year % 400 = 0) {50 monthDay = 29; 51} else {52 monthDay = 28; 53} 54 break; 55 56 case 4: 57 case 6: 58 case 9: 59 case 11: 60 monthDay = 30; 61 break; 62 63 default: 64 monthDay = 31; 65 break; 66} 67 68 // output the remaining date, since the second week, it is 8-week 69 for (int I = 8-week; I <= monthDay; I ++) {70 71 System. out. print (I + "\ t"); 72 73 // if you change a line every seven days, wrap the line 74 if (I + week) % 7 = 0) {75 System. out. println (); 76} 77} 78 79 80} 81 82/* 83 calculate the total number of days from 1.1 in the current month of the current year 84 */85 public static int getDays (int year, int month) {86 // judge whether this year is a leap year or a year, obtain the total number of days of the year: 87 int day1 = 0, day2 = 0; 88 89 for (int I = 1900; I <year; I ++) {90 if (I % 4 = 0 & I % 100! = 0 | I % 400 = 0) {91 day1 + = 366; 92} else {93 day1 + = 365; 94} 95} 96 97 // get the total number of days of the month 98 for (int I = 1; I <month; I ++) {99 switch (I) {100 case if (year % 4 = 0 & year % 100! = 0 | year % 400 = 0) {102 day2 + = 29; 103} else {104 day2 + = 28; 105} 106 break; 107 108 case 109 case day2 + = 30; 113 break; 114 115 default: 116 day2 + = 31; 117 break; 118} 119} 120 121 return day1 + day2; 122 123} 124 125}