输入年月查询该月的日历
public class Date {
public static void main(String[] args) {
//1990.1.1星期一
//输入某年某月
Scanner input = new Scanner(System.in);
System.out.print("请输入你要查询的年月(1990 1):");
int year = input.nextInt();
int mouth = input.nextInt();
// 离1990共多少天
//年的天数
int totalDay = 0;
for(int i = 1990;i<year;i++){
totalDay += ((year%4==0 && year%100!=0 || year%400 == 0)?366:365);
}
int mouthDay = 0;
for(int i = 0;i<mouth;i++){
totalDay += mouthDay(year,i);
}
//月的天数
System.out.println("\n"+year+"年"+mouth+"月:");
// 排版
System.out.println("日\t 一\t 二\t 三\t 四\t 五\t 六");
for(int i = 1;i<=(totalDay-1)%7;i++){
System.out.print("\t ");
}
for(int i = 1;i<=mouthDay(year,mouth);i++){
if((totalDay++)%7==0){
System.out.println(i);
}else{
System.out.print((i>9?i+"\t ":i+"\t "));
}
}
}
public static int mouthDay(int year,int mouth){
int mouthDay;
switch (mouth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
mouthDay = 31;
break;
case 2:
mouthDay = ((year%4==0 && year%100!=0 || year%400 == 0)?29:28);
break;
default:
mouthDay = 30;
}
return mouthDay;
}
}