日历特点:
1、一个星期有七天。公元元年的第一天,也就是公元1年1月1日。具体:公元1年1月1日是星期六。
2、如今世界通行的公历就是一种阳历,平年365天,闰年366天,每四年一闰,每满百年少闰一次,到第四百年再闰,即每四百年中有97个闰年。公历的历年平均长度与回归年只有26秒之差,要累积3300年才差一日。
3、一年有十二个月。每个月份的天数不等,一、三、五、七、八、十、十二月为31天,四、六、九、十一月为30天,二月份平年时28天,闰年是29天。用switch语句实现。
以下程序是某本书的例子,对其做了一些注释和修改。
import java.io.*;
public class TextControl{ // 操作打印任一年日历的类
static int year, monthDay, weekDay; // 定义静态变量,以便其它类调用
public static boolean isLeapYear(int y) { // 判断是否是闰年
return ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0));
}
public static int firstDay(int y) { // 计算该年第一天是星期几
long n = y * 365;//从公元元年距离你所输入的年份的日期总数
for (int i = 1; i < y; i++)
if (isLeapYear(i)) // 判断是否是闰年
n += 1;
return (int) n % 7;
}
public static void printWeek() { // 打印标头
System.out.println("========================================");
System.out.printf(" %1$8s %2$8s %3$8s %4$8s %5$8s %6$8s %7$8s\n",
"日","一","二","三","四","五","六");
}
public static int getMonthDay(int m) { // 获取每个月的天数
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear(year)) // 判断是否是闰年
return 29;
else
return 28;
default:
return 0;
}
}
public static void printMonth() { // 分别按不同条件逐月打印
for (int m = 1; m <= 12; m++) // 循环月份
{
System.out.println(m + "月");
printWeek();
for (int j = 1; j <= weekDay; j++){// 按每个月第一天是星期几打印相应的空格
System.out.print(" ");
}
int monthDay = getMonthDay(m); // 获取每个月的天数
for (int d = 1; d <= monthDay; d++) {
System.out.printf("%5d",d);
/*if (d < 10) // 以下4行对输出格式化
System.out.print(d + " ");
else
System.out.print(d + " ");*/
weekDay = (weekDay + 1) % 7; // 每打印一天后,反应第二天是星期几
if (weekDay == 0) // 如果第二天是星期天,便换行。
System.out.println();
}
System.out.println('\n');
}
}
public static void main(String[] args) throws IOException {//java程序的主入口处
System.out.print("请输入一个年份:");
// 以下接受从控制台输入
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
String s = in.readLine();
year = Integer.parseInt(s);
weekDay = firstDay(year); // 计算该年第一天是星期几
System.out.println("\n " + year + "年");
printMonth();
}
}
/*out:
请输入一个年份:2010
2010年
1月
========================================
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
2月
========================================
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28
3月
========================================
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
4月
========================================
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
5月
========================================
日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
6月
========================================
日 一 二 三 四 五 六
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
7月
========================================
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
8月
========================================
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
9月
========================================
日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
10月
========================================
日 一 二 三 四 五 六
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
11月
========================================
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
12月
========================================
日 一 二 三 四 五 六
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
*/
注意:
switch中的数据类型应为int、short、byte、char而不能为long、String,如果case的值为空,没有break语句,则循环一直往下寻找直到遇到return;在没有匹配的请况下进入default。