import java.util.Scanner;
public class DateTime{
public static void main(String[] args) {
//1.用户输入年和月,保存到变量中
Scanner input = new Scanner(System.in);
System.out.println("请输入一个年份:");
int year = input.nextInt();
System.out.println("请输入一个月份:");
int month = input.nextInt();
//2.计算出年总天数--[1900,2022)年的总天数
int years = 0;//保存年的总天数
for (int i = 1900; i < year; i++){
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
years += 366;
}else{
years += 365;
}
}
//1900 - 2022年总天数为44560
//System.out.println("年的总天数:"+years+"天");
//3.计算出当年该月的总天数--[1,12)月的总天数
int months = 0;//保存月的总天数
for (int i = 1; i < month; i++){
if (i==1||i==3||i==5||i==7||i==8||i==10||i==12){
months += 31;
}else if (i == 2){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
months += 29;
}else{
months += 28;
}
}else{
months += 30;
}
}
//System.out.println("月的总天数:"+months+"天");
//4.根据2和3计算出1号是周几?
int week = (1 + years + months) % 7;
//System.out.println(year+"年"+month+"月是周"+week);
//5.这个月有多少天?
int days = 0;//保存当月的总天数
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days = 31;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
days = 29;
}else{
days = 28;
}
}else{
days = 30;
}
//System.out.println(month+"月有"+days+"天");
//按日历格式输出
System.out.println("日\t一\t二\t三\t四\t五\t六");
//先打印1号之前的空格--是周几就打印几个
for (int i = 1; i <= week; i++){
System.out.print("\t");
}
//再打印后序日期数
for (int i = 1; i <= days; i++){
System.out.print(i);
if (( i + week ) % 7 == 0){
System.out.print("\n");
}else {
System.out.print("\t");
}
}
}
}