This example shares the specific code of Java to realize the effect of dynamic calendar for your reference. The specific contents are as follows

**Title: dynamic calendar**

Enter the year and month to display the calendar layout of the current month

### analysis

The basic idea of realizing the calendar is to determine a base date and calculate the total number of days from the base date to the month of the specified year. The premise of calculating the total number of days is to judge whether the specified year is a leap year (leap year is a year that can be divided by 400 or 4 but not 100), The remainder of the total number of days divided by 7 can be used to determine the number of spaces in front of the current month. To simply make this remainder the number of spaces, the base date must be January 1 and Monday. The base date of most calendars is January 1, 1900 and this day is Monday, which makes it easier to implement a dynamic calendar.

### Step analysis

1: Judge whether the specified year is a leap year

2: Calculate the total number of days from January 1900 to the month of the target year

3: Obtain the total number of days in the current month according to the month provided

4: Gets the number of spaces to output when printing the current month information

5: Print calendar

### code implementation

Calendar Class:

```
package com.softeem.j2106.oop2;
/**
*Dynamic calendar:
*Enter the year and month to display the calendar layout of the current month
* @author xcc
* 2021/7/12
*/
public class Calendar {
/**
*Calendar year
*/
int year;
/**
*Calendar month
*/
int month;
/**
*Constructor to specify the year and month of the calendar when creating the calendar object
* @param y
* @param m
*/
public Calendar(int y,int m){
year = y;
month = m;
}
/**
*Calculate the total number of days from January 1900 to the previous month of the current date
* @return
*/
public int getTotalDaysFrom1900(){
int days = 0;
//Count the number of days in the whole year
for(int i = 1900;i < year;i++){
days += isLeapYear(i) ? 366 : 365;
}
//Count the days of the whole month
for(int i = 1;i < month;i++){
days += getDaysOfMonth(i);
}
return days;
}
/**
*Obtain the total number of days in the current month according to the month provided
* @param m
* @return
*/
public int getDaysOfMonth(int m){
switch(m){
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return isLeapYear(year) ? 29 : 28;
default:
return 31;
}
}
/**
*Returns whether the parameter year is a leap year
* @param year
* @return
*/
public boolean isLeapYear(int year){
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
/**
*Show calendar layout
*/
public void show(){
System. Out. Println ("now it is:" + year + "year" + month + "month");
System. Out. Println ("= = Softeem perpetual calendar [" + year + "] year [" + month + "] month = =");
System.out.println ("one \ t two \ t three \ t four \ t five \ t six days");
System.out.println("===============================");
//Get the number of spaces before the calendar (the first day of the month is the day of the week)
int space = getTotalDaysFrom1900() % 7;
//Get the total number of days in the current month
int days = getDaysOfMonth(month);
int count = 0;
//Output space
for (int i = 0; i < space; i++) {
count++;
System.out.print("\t");
}
//Output date
for(int i = 1;i <= days;i++){
System.out.print(i+"\t");
count++;
//If the counter is a multiple of 7, it indicates that it has been printed for one week and needs to wrap
if(count % 7 == 0){
System.out.println();
}
}
}
public static void main(String[] args) {
Calendar c = new Calendar(2021,8);
c.show();
}
}
```

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.