To complete the production of a perpetual calendar, you need to use knowledge such as arrays and loops.
Programmatically calculate the number of days from January 1, 1900 to find the total number of days before the current month (not including the number of days in the current input month,)
Programmatically calculate the first day of the input month is the day of the week (Formula: day of the week = 1 + day difference %7).
import java.util.Scanner; public class Calendar{ public static void main(String[] args){ int year; int month; int totaldays=0; Scanner sc=new Scanner(System.in); System.out.println("Please enter year"); year=sc.nextInt(); System.out.println("Please enter month"); month=sc.nextInt(); //Calculate the total number of days of the year for(int i=1900;i<year;i++){ if((i%400==0)||(i%4==0&&i%100!=0)){ totaldays+=366; }else{ totaldays+=365; } } //The total number of days from January 1, 1900 totaldays+=monthdays(month,year); //System.out.println(totaldays); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //The first day of the month is the day of the week, and there are a few spaces int x=1+totaldays%7; if(x==7){ x=0; } for(int i=0;i<x;i++){ System.out.print(" /t"); } int days=monthday(month,year); int i=1; while(i<=days){ System.out.print(i+" /t"); if((i+x)%7==0){ System.out.println(); } i++; } } //Total days in the month public static int monthdays(int month,int year){ int totaldays=0; for(int i=1;i<month;i++){ totaldays+=monthday(i,year); } return totaldays; } //The number of days in a certain month public static int monthday(int month,int year){ if((year%400==0)||(year%4==0&&year%100!=0)){ int[] arr={0,31,29,31,30,31,30,31,31,30,31,30,31}; return arr[month]; }else{ int[] arr={0,31,28,31,30,31,30,31,31,30,31}; return arr[month]; } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.