Java implementation prints the calendar for the current month

Source: Internet
Author: User

The current calendar is printed, and the current date is expressed in *.

The key is that the first day of the month is the week.

Kimlarsson Calculation formula
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 +1,m represents the number of months. Y represents the number of years.

Note 1: There is a different formula in the formula:
Think of January and February as the January or March and January or April of the previous year,
Example: Assume that the 2004-1-10 is converted to: 2003-13-10 to substitute the formula calculation.

Note 2: In the calendar of most Catholic countries, in 1752 years there is no 9.3-9.13. In the calendar of the year September 2
The following is a direct September 14, so in the calculation of the day is the days of the algorithm to take this particular situation into account


Package Work_0529;import Java.util.scanner;public class Main {private static int year;private static int month;private STA               Tic int day;private static boolean flag;private static int count;//days of the current month public static void main (string[] args) {         if (!getinput ()) return;        if (!judgedata ()) return;                 int a = GetData ();        System.out.println ("Day 123456");        int j=0; while (a--> 0) {System.out.print ("");        j + +;}        for (int i=1;i <= count; i++) {if (j%7==0) System.out.println ("\ n");        j + +;        System.out.print (String.Format ("%4d", I));        if (day==i) System.out.print ("*"); }}private static Boolean Judgedata () {flag=false;//false represents common year.    Conversely if (year%100==0) flag=true;    else if (year%4==0) flag=true;    if (year<=0) {System.out.println ("year cannot be less than or equal to 0");    return false;    } if (Month<=0 | | month>12) {SYSTEM.OUT.PRINTLN ("Check the range of months"); return false;} if (month==2) {if (fLag && day>29) {System.out.println ("current is" + (flag?)    Leap year ":" Common Year ") +" February cannot be greater than 29 ");    return false; if (!flag && day >) {System.out.println ("current is" + (flag?

"Leap Year": "Common Year") + "February cannot be greater than 28"); return false; } if (flag) count=29; else count=28; } if (Month==1 | | month==3 | | month==5| | month==7| | month==8| | month==10| | month==12) {if (day<=0 | | Day > 3 1) {System.out.println ("Scope of the inspection date"); return false; } count = 31; } else if (month!=2) {if (day<=0 | | day>30) {System.out.println ("Scope of the check Date"); return false; } count = 30; }return true;} private static int getData () {int tmpday=1;//denotes int A; if (month = = 1 | | month = = 2) {month + =; year--;} if ((year<1752) | | | (year==1752 && month<9) | | (year==1752 && month==9 && tmpday<3)) {a = (Tmpday + 2*month + month+1)/5 + year + YEAR/4 +5)% 7; }else{A = (Tmpday + 2*month + (month+1)/5 + year + YEAR/4-year/100 + year/400)%7; }return A;} private static Boolean getinput () {try {System.out.print ("Please enter date (format xxxx-xx-xx):"); Scanner Scanner = new Scanner (system.in); String str = scanner.nextline ();Year = Integer.parseint (str.substring (0, Str.indexof ('-'))); str = str.substring (Str.indexof ('-') + 1, str.length ()); month = Integer.parseint (str.substring (0, Str.indexof ('-'))), str = str.substring (Str.indexof ('-') + 1, str.length ()); Day = Integer.parseint (str); return true;} catch (NumberFormatException e) {System.out.println ("Malformed"),} catch (Stringindexoutofboundsexception E2) { System.out.println ("Malformed");} return false;}}




Java implementation prints the calendar for the current month

Related Article

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.