Algorithm, Java Code Implementation print perpetual calendar

Source: Internet
Author: User

 Perpetual Calendartake Monday, January 1, 1900, as the time origin.
Sunday First day
Monday Next day
Tuesday Third Day
Wednesday Fourth day
Thursday Fifth day
Friday Sixth day
Saturday Seventh Day
          1. Calculate the number of days from the origin of the current date (example: 2016/9/18)how many Swiss-year peace years are there between 2015 and 1900-->count1      total days from January 2016 to August-->count2this month's number onecount = count1+count2+12. The first day of the week is calculatedK = count%7; (print how many spaces are preceded by a number)3. Calculate the number of days of the monthMaxdays4. Cycle Printing1.for (int i=1;i<=maxdays;i++) 2.K acts as an indicator and wraps to the seventh day of the week           
1  PackageCom.bjsxt.game;2 ImportJava.util.Scanner;3  Public classCalendar {4      Public Static BooleanIsleapyear (intYear ) {5         if(Year%4==0&&year%100!=0 | | year%400==0){6             return true;7         }8             return false;9     }Ten      One     //calculate the maximum number of days for the current month A      Public Static intMaxdays (intMonthintYear ) { -          -         intmaxdays; the         if(month==1 | | month==3 | | month==5 | | month==7 | | month==8 | | month==10 | | month==12) -Maxdays = 31; -         Else if(month==4 | | month==6 | | month==9 | | month==11) -Maxdays = 30; +         Else{ -                 if(Isleapyear (year)) +maxdays = 29; A                 Else atMaxdays = 28; -         }  -         returnmaxdays; -     } -      -     //Calculate the current month number is the day ordinal of a week in      Public Static intCountintMonthintYear ) { -         intK//indicates that the current input value is the day ordinal of a week to         intR = 0, p = 0; +         intCount//indicates how many days away from 1970.1.1 -         //calculate the number of years before the current year for the Swiss year of Peace the          for(inti=1900;i<year;i++){ *             if(Isleapyear (i)) $r++;Panax Notoginseng             Else -p++; the         } +Count = 366*r+365*p; A         //determine the total number of days before the current month the          for(inti=1;i<month;i++){ +                 if(i==2){ -                     if(Isleapyear (year)) $Count = count + 29; $                     Else -Count = Count + 28; -                 } the                 Else if(i==1 | | i==3 | | i==5 | | i==7 | | i==8 | | i==10 | | i==12) -Count = Count + 31;Wuyi                 Else theCount = Count + 30;  -         } WuK = (count+1)%7+1; -         returnK; About     } $      Public Static voidMain (string[] args) { -Scanner input =NewScanner (system.in); -          -System.out.print ("Please enter the year:"); A         intYear =input.nextint (); +          theSystem.out.print ("Please enter the month:"); -         intmonth =input.nextint (); $          theSystem.out.print ("Please enter the day:"); the         intDay =input.nextint (); the          the         intMaxdays = Maxdays (month,year);//calculates the maximum number of days of the current input month -         intK = count (month, year);//Calculate number 1th for the current month is the day of the week in                  theSystem.out.println ("t \ t two \ t three \ Four \ five \ t six"); the          for(inti=0;i<k-1;i++){ AboutSystem.out.print ("\ t"); the         } the          for(inti=1;i<=maxdays;i++){ theSystem.out.print (i+ "\ T"); +             if(k%7 = = 0){ -System.out.println ("\ n"); theK = 0;Bayi             } thek++; the         } -     } -}
View Code

Algorithm, Java Code Implementation print perpetual calendar

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.