several algorithms that calculate the day of the week are:
One: Commonly used formulas
W = [Y-1] + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + D
Y is the number of years, and D is the cumulative number of days of the day in this year, that is, the day of the year.
Two: Caille (Zeller) formula
w=y+[y/4]+[c/4]-2c+[26 (m+1)/10]+d-1
The meanings of the symbols in the formula are as follows, W: week; c: century; Y: Year (two digits); M: month (m greater than or equal to 3, less than or equal to 14, i.e., in the Caille formula, the December of the year is calculated as the previous year's 13, 1 April) ; d: day; [] represents rounding, that is, as long as the integer part.
the Caille (Zeller) formula greatly reduces the computational complexity compared to the general general purpose calculation formula.
Three: the improvement of Caille (Zeller) formula
Feng Sixian
The Caille (Zeller) formula greatly reduces the computational complexity compared to the other general purpose calculation formula. However, the general formula given by the author seems to be more concise (including the computational process). The formula is now listed below:
W=[y/4]+r (Y/7) -2r (C/4) +m ' +d
The symbols in the formula have the following meanings, R () represents the remainder, that is, as long as the remainder part, M ' is the correction number of M, now gives 1-12 months of correction number 1 ' to 12 ' as follows: (1 ', 10 ') = 6; (2 ', 3 ', 11 ') = 2; (4 ', 7 ') =5;5 ' =0;6 ' =3;8 ' = 1; (9 ', 12 ') =4 (note: In the formula given by the author, Y is the run Year 1 ' =5;2 ' =1). The other symbols are the same as those in the Caille (Zeller) formula.
Four: Kimlarsson calculation formula
This formula name is I give name, haha hope everybody don't take offense.
W= (d+2*m+3* (m+1)/5+y+y/4-y/100+y/400) MoD 7
In the formula, D represents the number of days in a date, m represents the number of months, and Y represents the number of years.
1 //W = [Y-1] + [(Y-1)/4]-[(Y-1)/100] + [(Y-1)/400] + D2 //Y: Represents the year D: Indicates the cumulative number of days of 31st last month in this year3 intYear//the year used to store the input4 intMonth//the month to store the input5 intdays=0;//the number of days to store in the month6 intzdays=0;//The number of days from 1900.1.1 that is used to store the input of the day difference7 intWeek//this month's number 1th is the day of the week.8 BooleanISRN;//determines whether a leap year returns a true or False value9SYSTEM.OUT.PRINTLN ("☆☆☆☆☆☆☆☆☆☆ Welcome to use perpetual calendar ☆☆☆☆☆☆☆☆☆");TenScanner scan=NewScanner (system.in); OneSystem.out.print ("Please enter the year:"); AYear=scan.nextint (); -System.out.print ("Please enter month:"); -Month=scan.nextint (); the //first determine whether the year entered is common year or leap years - if(year%400==0| | (year%4==0&&year%100!=0)){ -isrn=true; -System.out.print (year+) is a leap year! "); + } - Else{ +isrn=false; ASystem.out.print (year+ "Year is common year! "); at } - //plus the number of days to number 31st last month. - for(intj=1;j<=month;j++){ - Switch(j) { - Case1: - Case3: in Case5: - Case7: to Case8: + Case10: - Case12: theDays=31; * Break; $ Case4:Panax Notoginseng Case6: - Case9: the Case11: +Days=30; A Break; the Case2: + if(ISRN) {days=29;} - Else{days=28;} $ Break; $ default: -System.out.println ("The month you entered is wrong! "); - } the if(j<month) { -zdays+=Days ;Wuyi } the } - WuSystem.out.println (month+ "Month has" +days+ "days. "); - //calculate the number of days of the week 1th. Aboutweek= (year-1) + (year-1)/4-(year-1)/100+ (year-1)/400+zdays; $Week=1+week%7; - if(week==7) {week=0;}//if 7 means 0 tabs in front of Sunday -System.out.println ("day \ t two \ t three \ Four \ five \ t six \ n"); - for(inti=0;i<week;i++){ ASystem.out.print ("\ t"); + } the for(inti=1;i<=days;i++){ -System.out.print (i+ "\ T"); $ if((I+week)%7==0) {//Print to 7 digit line break the System.out.println (); the } the}
Operation Result:
Statement Exercise 4 (Print perpetual calendar)