JAVA implementation of the simple calendar code

  • 2020-04-01 04:16:45
  • OfStack

This article illustrates a simple perpetual calendar implemented in JAVA. Share with you for your reference, as follows:


import java.util.Scanner;
public class PrintCalendar {
 public static void main(String[] args) {
 int years = 0;
 int month = 0;
 int days = 0; 
 boolean isRun = false;
 //Year, month
 Scanner input = new Scanner(System.in);
 System.out.print(" Year: ");
 years = input.nextInt();
 System.out.print(" Month of entry: ");
 month = input.nextInt();
 System.out.println("n*********"+years+" years "+month+" month   Day  �  table ************");
 //Decide if it's a leap year
 if((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)){
 isRun = true;
 }
 int totalDays = 0; //Tired day  �   � 
 //The days from January 1, 1900
 for(int i = 1900; i < years; i++){
 if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)){
 totalDays = totalDays + 366;
 }else{
 totalDays = totalDays + 365;
 }
 }
 int beforeDays = 0;
 //The root color determines the sky color
 for(int j = 1; j <= month; j++){
 switch(j){
 case 1:
 case 3:
 case 5:
 case 7:
 case 8:
 case 10:
 case 12:
 days = 31;
 break;
 case 4:
 case 6:
 case 9:
 case 11:
 days = 30;
 break;
 case 2:
 if(isRun){
  days = 29;
 }else{
  days = 28;
 }
 default:
 System.out.println(" Enter month incorrectly !!");
 }
 if(j < month){
 beforeDays = beforeDays + days;
 }
 }
 totalDays = totalDays + beforeDays; //So far so good
 int firstDayOfMonth = 0;
 int temp = 1 + totalDays % 7 ;
 if(temp == 7){
 firstDayOfMonth = 0; // � ,
 }else{
 firstDayOfMonth = temp;
 }
 
 System.out.println(" Sunday t Monday t Tuesday t Wednesday t Thursday t Friday t Saturday ");
 for(int k = 0; k < firstDayOfMonth; k++){
 System.out.print("t");
 }
 for(int m = 1; m <= days; m++){
 System.out.print( m + "t");
 if((totalDays + m) % 7 == 6){
 System.out.print("n");
 }
 }
 }
}

About the production of the calendar interested in friends can also refer to this site online tools:

(link: http://tools.jb51.net/bianmin/wannianli)

(link: http://tools.jb51.net/bianmin/webwannianli)

I hope this article has been helpful to you in Java programming.


Related articles: