1. 输入任何一年,2020则输出该年的所有月历表
  2. 输入任何年月,2020 11,则输出该月的月历表
  3. 输入任何年月日,2020 11 14,则输出该天是星期几
  • 已知年月日,求星期几

    int w=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;

  • 判断是闰年

    (year%400 == 0 ) || (year%4==0 && year%100 != 0)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Scanner;

public class Demo {

int choice;
int year = 0;
int month=0;
int day;
int m_d[] = new int[13];
boolean is_rn;

public void calendar() {
while(true) {
System.out.println("Enter a letter from three choices marked the 1), 2) and 3): ");
System.out.println("1. year");
System.out.println("2. year month");
System.out.println("3. year month day");
Scanner input = new Scanner(System.in);
choice = input.nextInt();

System.out.println("Enter the format you have just chosen: ");
switch(choice) {
case 1:

year = input.nextInt();
this.creat_calendar();
this.print_year();
break;
case 2:

year=input.nextInt();
month=input.nextInt();
this.creat_calendar();
this.print_month();
break;

case 3:

year=input.nextInt();
month=input.nextInt();
day=input.nextInt();
this.print_week();
break;
default:
System.out.println("The number is invalid.");
break;

}
}
}

public void creat_calendar() {
if((year%400 == 0 ) || (year%4==0 && year%100 != 0))
is_rn=true;
else
is_rn=false;
for(int i = 1; i <= 12; i++) {

if(i==1 || i==3 ||
i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
day=31;
else if(i == 4 || i == 6 || i == 9 || i == 11 )
day=30;
else if(i == 2) {
if(is_rn)
day=29;
else
day=28;
}
m_d[i] = day;
}

}

public void print_year() {

for(int i = 1; i <=12; i++) {
System.out.println("the " + (i) +" month");
for(int j = 1; j <= m_d[i]; j++) {
if(j % 7 ==0 )
System.out.println();
System.out.print(j + "\t");
}
System.out.println();
}
}
public void print_month() {
System.out.println("the " + month + " month");
for(int i = 1; i <= m_d[month]; i++) {
if(i % 7 ==0)
System.out.println();
System.out.print(i + "\t");
}
System.out.println();
}

public void print_week() {
int w=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400+1)%7;
System.out.println("Today is " + w + "th day of a week");

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Demo d = new Demo();
d.calendar();

}

}