Java Calendar, enter the month to get the date

Source: Internet
Author: User

1 //enter the year and month to print out this month's calendar table2 /*3 1.19 Year January 1 is Monday4 2. Calculate the year of the input how many days from 1900, and how many days of the year are January 1 from this month ?5 1)6 3. The total number of days%7 from the beginning of the week7 Note: The time in the computer is as low as 1900, and the UNIX system considers January 1, 1970 0 o'clock to be the time era. 8 So, in this procedure did not test the year 1900 years ago. Interested can study under their own. 9 */Ten ImportJava.util.Scanner; One classcalender{ A  -      Public Static voidMain (string[] args) { -          the print (); -  -     } -  +     //Print Output -      Public Static voidprint () { +Scanner sc =NewScanner (system.in); ASystem.out.println ("Please enter the year:"); at         intYear =sc.nextint (); -System.out.println ("Please enter month (1~12):"); -         intmonth =sc.nextint (); -  -         intDays = GetDays (year, month);//GetDays Method Details please look down -         //Days+1:day is the total number of days, the total number of days to enter the month is just the number of days before this month in         //plus 1 became the first day of the month. -         intWeek = days%7==0?1:days%7+1;//the first day of the start is the days of the week toSystem.out.println ("t \ t two \ t three \ Four \ five \ t six"); +          -         //Output first line (first week) empty out part the          for(intI=1; i<=week; i++){ *System.out.print ("\ t"); $         }Panax Notoginseng  -         //outputs the first line (first week) of each day the          for(intI=1; i<=7-week; i++){ +System.out.print (i+ "\ T"); A         } the  + System.out.println (); -  $         //1-12 Month number of months $         intMonthDay = 0; -         Switch(month) { -                  Case2: the                     if(Year%4==0&&year%100!=0 | | year%400==0 ){ -monthday=29;Wuyi}Else{ theMonthday=28; -                     } Wu                      Break; -  About                  Case4: $                  Case6: -                  Case9: -                  Case11: -Monthday=30; A                      Break; +  the                 default : -Monthday=31; $                      Break; the             } the  the         //output The remaining date, starting from the second week, so it's 8-week. the          for(intI=8-week; i<=monthday; i++){ -      inSystem.out.print (i+ "\ T"); the              the             //A line is changed every seven days, and the date can be divisible by 7. About             if((I+week)%7==0){ the System.out.println (); the             } the         } +  -  the     }Bayi  the     /* the calculates the total number of days from 1900 to 1.1 for the current month -     */ -      Public Static intGetDays (intYearintmonth) { the         //judge this year is a leap year or common year, get the total number of years the         intDay1=0, day2=0; the  the          for(inti=1900; i<year; i++){ -             if(I%4==0&&i%100!=0 | | i%400==0){ theday1+=366; the}Else{ theday1+=365;94             } the         } the      the         //get the total number of months98          for(intI=1; i<month; i++){ About             Switch(i) { -                  Case2:101                     if(Year%4==0&&year%100!=0 | | year%400==0 ){102day2+=29;103}Else{104Day2+=28; the                     }106                      Break;107 108                  Case4:109                  Case6: the                  Case9:111                  Case11: theDay2+=30;113                      Break; the  the                 default : theDay2+=31;117                      Break;118             }119         } -     121         returnday1+Day2;122 123     }124      the}

Java Calendar, enter the month to get the date

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.