The example in this article describes a simple perpetual calendar implemented in JAVA. Share it with everyone for your reference, the details are 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; //The console enters the year and month Scanner input = new Scanner(System.in); System.out.print("Enter year:"); years = input.nextInt(); System.out.print("Input month:"); month = input.nextInt(); System.out.println("/n**********"+years+"year"+month+"month day Table************"); //Determine whether it is a leap year if((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) { isRun = true; } int totalDays = 0; //Total days//Calculate 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 day of the root month 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("The input month is incorrect!!"); } if(j < month){ beforeDays = beforeDays + days; } } totalDays = totalDays + beforeDays; //The current day int firstDayOfMonth = 0; int temp = 1 + totalDays % 7; if(temp == 7){ firstDayOfMonth = 0; //Day}else{ firstDayOfMonth = temp; } /* Output calendar*/ System.out.println("Sunday/tMonday/tTuesday/tWednesday/tThursday/tFriday/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"); } } }}Friends who are interested in the production of perpetual calendar can also refer to the online tools of this site:
Online Perpetual Calendar
Web Perpetual Calendar
I hope this article will be helpful to everyone in Java programming.