Enter the year, print the calendar of the Year, and identify the date of the day. Similar to linux cal, linuxcal
Package com. qunar. training. questionDate;
Public class Permanent {
Public static boolean isLeapYear (int year) {// can be divisible by 4, but cannot be divisible by 100, or can be divisible by 400
Boolean leapYear = false;
If (year % 100 = 0 & year % 400 = 0) | (year % 100! = 0 & year % 4 = 0 )){
LeapYear = true;
}
Return leapYear;
}
Public static int countDays (int year) {// select a benchmark, which is January 1, 2015, Thursday
Int countDays = 0;
Int maid = year> 2015? 2015: year;
Integer endYear = year> 2015? Year: 2015;
For (int I = beginYear; I <endYear; I ++ ){
If (isLeapYear (I )){
CountDays ++ = 366;
} Else {
CountDays ++ = 365;
}
}
Return countDays;
}
Public static void showCaledar (int year ){
Int days = countDays (year );
Int weekDay = days % 7;
If (year & gt; 2015 ){
WeekDay = (weekDay + 4) % 7;
} Else {
WeekDay = (11-weekDay) % 7;
}
String [] monthLabels = new String [] {"January", "February", "March", "April", "May", "June", "July ", "August", "September", "October", "November", "December "};
String [] weekLabels = new String [] {"Sun", "Mon", "Tur", "Wen", "Thr", "Fra", "Sat "};
Int [] monthDay = {, 30, 31 };
For (int I = 0; I <12; I ++ ){
System. out. println ("\ n" + monthLabels [I]);
For (String weekLabel: weekLabels) {// tags for each week
System. out. print (weekLabel + "");
}
System. out. println (); // The next row.
For (int j = 0; j <weekDay; j ++) {// locate the first date
System. out. print ("");
}
If (isLeapYear (year )){
MonthDay [1] = 29;
} Else {
MonthDay [1] = 28;
}
For (int k = 1; k <= monthDay [I]; k ++ ){
If (k + weekDay-1) % 7 = 0 ){
System. out. println ();
}
If (k <10) {// alignment problem
System. out. print (k + "");
} Else {
System. out. print (k + "");
}
}
WeekDay = (weekDay + monthDay [I]) % 7;
}
}
Public static void main (String [] args ){
ShowCaledar (1, 2014 );
}
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.