java perpetual calendar obtain the calendar table

  • 2020-06-23 01:56:50
  • OfStack

This article USES java technology to input year and month to get the calendar table of this month and year. The following is a code to show you:

Enter the year and month and print out the calendar for the month
January 1, 1900 was Monday
2. Calculate the number of days from 1900 to the year you entered and the number of days from January 1 to this month
3. The total number of days %7 is calculated from the day of the week
Note: The minimum time in the computer is 1900, and the UNIX system considers 0 on January 1, 1970 to be the time era.
so, years prior to 1900 are not included in this program. If you're interested, you can study it yourself.


import java.util.Scanner;
class Calender{
 public static void main(String[] args){
 print();
 }
 // A printout 
 public static void print(){
 Scanner sc = new Scanner(System.in);
 System.out.println(" Please enter the year: ");
 int year = sc.nextInt();
 System.out.println(" Please enter month ( 1~12 ) : ");
 int month = sc.nextInt();
 int days = getDays(year, month);//getDays See below for details 
 //days+1:day It's the total number of days, the total number of days in a month is just the number of days before that month, 
 // add 1 Becomes the beginning of the month 1 day 
 int week = days%7==0?1:days%7+1;// Start the first 1 What day of the week is it 
 System.out.println(" day \t1\t2\t3\t4\t5\t6");
 // Output the first 1 Line (the first 1 Week) 
 for(int i=1; i<=week; i++){
  System.out.print(" \t");
 }
 // Output the first 1 Line (the first 1 Weeks) each day 
 for(int i=1; i<=7-week; i++){
  System.out.print(i+"\t");
 }
 System.out.println();
 //1~12 The number of days of a month 
 int monthDay = 0;
 switch(month){
  case 2:
   if(year%4==0&&year%100!=0 || year%400==0 ){
   monthDay=29;
   }else{
   monthDay=28;
   }
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   monthDay=30;
   break;
  default :
   monthDay=31;
   break;
  }
 // Output the remaining dates from the 2 The week started, so it was 8-week
 for(int i=8-week; i<=monthDay; i++){
  System.out.print(i+"\t");
  // every 7 Days in 1 Row when the date is divisible 7 Just a newline 
  if((i+week)%7==0){
  System.out.println();
  }
 }

 }
 /*
  Calculate the distance of the current month 1900 years 1.1 The total number of days 
 */
 public static int getDays(int year, int month){
 // Decide whether it is a leap year or a normal year , You get the total number of days in a year 
 int day1=0, day2=0;
 for(int i=1900; i<year; i++){
  if(i%4==0&&i%100!=0 || i%400==0){
  day1+=366;
  }else{
  day1+=365;
  }
 }
 // You get the total number of days in a month 
 for(int i=1; i<month; i++){
  switch(i){
  case 2:
   if(year%4==0&&year%100!=0 || year%400==0 ){
   day2+=29;
   }else{
   day2+=28;
   }
   break;
  case 4:
  case 6:
  case 9:
  case 11:
   day2+=30;
   break;
  default :
   day2+=31;
   break;
  }
 }
 return day1+day2;
 }
 }

Above is the calendar code implementation process, I hope to help you.


Related articles: