PK creative Spring Festival, I am participating in the “Spring Festival creative submission contest”, please see: Spring Festival creative submission Contest

The annual Spring Festival means the New Year is about to begin, the beginning of the New Year is certainly not a perpetual calendar, today with Java to show a New Year’s perpetual calendar

start

Calculating a calendar year begins with some special conditions: leap year, February. Since the year of the embellishment year is 366 days, the week will be pushed back one day. February is different from other months, and usually has 28 days, while the embellishment year has 29 days.

A leap year is defined as a common leap year and a century leap year. A Gregorian calendar year is a common leap year that is a multiple of 4 rather than a multiple of 100. Gregorian calendar years are hundreds and must be multiples of 400 to be leap years

  1. Calculate the number of days between 1900 and the current year
int yearDays = 0;
for (int i = 1900; i < year; i++) {
    / / a leap year
    if (i % 4= =0 && i % 100! =0 || i % 400= =0) {
        yearDays += 366;
    } else {
        yearDays += 365; }}Copy the code
  1. Then, starting from January, determine the number of days in the current month

Judge the number of days in the current month, add the number of days in the year gap, and take the remaining 7 to determine the week of the first day of the current month

Integer month = 12;
/ / a leap year
boolean run = year % 4= =0 && year % 100! =0 || year % 400= =0;
for (int j = 1; j <= month; j++) {
    month(j);
    int monthDays = 0;
    for (int i = 1; i < j; i++) {
        if (i == 2) {
            if (run) {
                monthDays += 29;
            } else {
                monthDays += 28; }}else if (i == 4 || i == 6 || i == 9 || i == 11) {
            monthDays += 30;
        } else {
            monthDays += 31; }}int total = yearDays + monthDays + 1;
    // take the current week
    int week = (total % 7);
}
Copy the code

This gives you the day of the week of the first day of the month

  1. The output starts from 1, week +1, with a remainder of 7 to 0, the line breaks to start the next week
System.out.println("Day \t one \t two \t three \t four \t five \t six");
int dayTotal;
if(j == 2) {
    if(run) {
        dayTotal = 29;
    } else {
        dayTotal = 28; }}else if(j == 4 || j ==6 || j ==9 || j ==11) {
    dayTotal = 30;
} else {
    dayTotal = 31;
}

int day = 1;
for (int i = 0; i < week; i++) {
    System.out.print("\t");
}
while (day < dayTotal + 1) {
    System.out.print(day++ + "\t");
    week++;
    if (week % 7= =0) {
        System.out.println();
    }
}
System.out.println();
System.out.println();
Copy the code

The overall output is as follows

The same is true of the comparative calendar, except that there are no lunar dates and holidays

Thanks for watching