Simple java calendar

Output on the page in any year after 1900 simple calendar

 

package text3;

import java.util.Scanner;

{class MyCalendar public
  public static void main (String [] args) {
    String Weeks = "Su of Mo Tu We Th Fr of Sa"; // weeks list
    int i, j; // loop control variable
    System.out.println ( "Please at the year you want to the INPUT Research,: ");
    int year = new new Scanner (System.in) .nextInt ();
    // used to calculate a given year January 1 to week, the initial value of 1.1 in 1900 Monday
    int Ospa3 = 1;
    // from 1900 onwards, calculated for each subsequent year of 1.1 to a few weeks, until obtaining a given year on the 11th.
    for (i = 1; i <year; i ++) {
      // if . leap year, subsequent year on the 11th plus two days, and one day or
      IF (! I. 4 == 0 &&% I = 0% 100% 400 == 0 || I)
        Ospa3 + = 2;
      the else
        Ospa3 ++;
    }
    // Since seven days a week, so the final value of the remainder Ospa3 should die 7
    Ospa3% = 7;
    a first output calendar prompt information //
    System.out.println ( "\ T \ T \ tCalender of" + year);
    // list output weeks, three weeks per line
    I =. 1;
    the while (I <=. 3) {
      of System.out.print (Weeks );
      I ++;
    }
    System.out.println ( "");
    day of the week and the number of days in month 1 // storage for three consecutive months
    int spa1 = 0, spa2 = 0 , spa3 = 0, cou1 = 0, = 0 cou2, cou3 = 0;
    // store the current date for three consecutive months
    int DAY1 = 0, DAY2 = 0, Day3 = 0;
    // determines whether the year is a leap year, the number of days in February
    boolean leap = year% 4 ! == 0 && year = 100% 400% year == 0 || 0;
    for (J =. 1; J <= 28; J ++) {// ???? why 28
      IF ((. 1-J)% 0 == 7) {
      DAY1 = 1;
      DAY2 = 1;
      Day3 = 1; // set the date output for three consecutive months are starting from No. 1
      // set respectively in the 1st week of each month year, and month the total number of days
      switch(j/7){
      case 0:
        spa1 = Ospa3;
        cou1 = 31; //1月
        spa2 = (spa1 + cou1) % 7;
        cou2 = leap ?29:28; //2月
        spa3 = (spa2 + cou2) % 7;
        cou3 = 31; //3月
        Ospa3 = spa3;
        break;
      case 1:
        spa1 = (Ospa3 + cou3) % 7;
        cou1 = 30; //4月
        spa2 = (spa1 + cou1) % 7;
        cou2 = 31; //5月
        spa3 = (spa2 + cou2) % 7;
        cou3 = 30; //6月
        Ospa3 = spa3;
        break;
      case 2:
        spa1 = (Ospa3 + cou3) % 7;
        cou1 = 31; //7月
        = SPA2 (SPA1 COU1 +). 7%;
        cou2 = 31 is; // August
        Spa3 = (SPA2 cou2 +). 7%;
        cou3 = 30; // September
        Ospa3 = Spa3;
        BREAK;
      Case. 3:
        SPA1 = (+ Ospa3 cou3). 7%;
        COU1 = 31 is; // October
        SPA2 = (SPA1 COU1 +). 7%;
        cou2 = 30; // November
        Spa3 = (SPA2 cou2 +). 7%;
        cou3 = 31 is; // December
        break ;

      }

    }
    // output calendar on the screen, the display line 3 weeks common output 21
    for (I =. 1; I <= 21 is; I ++) {
      IF ((. 1-I) /. 7 == 0) // each the output of the first row of seven a month ago
        System.out.print (SPA1 - <= 0 && DAY1 <= COU1 DAY1 <10 "0" +??
        DAY1 ++: "" + DAY1 ++: "");
      else if((i - 1)/7 == 1)
        System.out.print(spa2--<=0 && day2 <= cou2? day2 < 10?" 0"+
        day2++ :" " + day2++ :" ");
      else
        System.out.print(spa3--<=0 && day3 <= cou3? day3 < 10?" 0"+
        day3++ :" " + day3++ :" ");
      if(i % 7 == 0)
        System.out.print(" ");//每行中月份之间的空格
    }
    System.out.println(" "); //换行
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/anannan/p/11699189.html