Enter the year, print the calendar of the Year, and identify the date of the day. Similar to linux cal, linuxcal

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.