知识点:一重循环、日期算法
思路:判断这个月的前一天(n-1月)是周几,算出这个月的第一天是周几,确定月历第一天的位置。然后依次打印出剩余天数
int total = 1,year = 1999;
final int BEGIN = 1900;
// 累加整数年
for (int i = BEGIN; i <year ; i++) {
total += (i%4==0&&i%100!=0)||i%400==0?366:365;
}
// 控制月份
for (int i = 1,day=0; i <=12 ; i++) {
System.out.println(MessageFormat.format("\n\n"+"{0}年{1}月:",year,i));
System.out.println("日\t一\t二\t三\t四\t五\t六");
// 1号之前的\t
for (int j = 0; j <=total%7 ; j++) {
System.out.print("\t");
}
switch (i){
case 1:case 3:case 5:case 7:case 8:case 10: case 12:day=31;break;
case 4:case 6:case 9:case 11:day=30;break;
case 2:day=(year%4==0&&year%100!=0)||year%400==0?29:28;break;
}
for (int j = 1; j <=day ; j++) {
System.out.print(j+"\t");
if (total++%7==5){
System.out.println();
}
}
}