-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalendar.java
More file actions
275 lines (261 loc) · 5.02 KB
/
Calendar.java
File metadata and controls
275 lines (261 loc) · 5.02 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* 输出日历 :1900年以后的任意时期的月日历。
*
* 写完的java 源代码 用eclipse 生成可以运行的软件
* 将其导出为jar文件:右击要导出的文件-export-jar file
* 注意一定要选择Main class最后生成.jar文件,
* 运行时使用命令:java -jar xxx.jar
* 或者创建一个bat文件搜索,将上面的命令写入其中,
* 双击.bat文件执行jar命令即可
* 代码中右键->运行->Java应用程序,程序运行了,
* 对应的目录下面会生成后缀为class的文件,
* 只要有Jre的地方都可以运行class。
* 如果要变为exe的话,
* 下载exe4j来生成exe文件也可以。
*/
import java.util.*;
public class Calendar {
private final int epochYear = 1900;
private final int[] monthDays = {29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int year;
private int month;
public void getInputData(){
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:(年份为从1900年开始的任意年份时间,必须是整数。如:2018)");
this.year = input.nextInt();
while (!checkYear()){
System.out.println("对不起,您输入的年份非法,请再次输入你想查询的年份,谢谢!");
System.out.println("请输入年份:");
this.year = input.nextInt();
}
System.out.println("请输入月份:(月份为1,2,...,12中的任意月份。如:12)");
this.month = input.nextInt();
while (! checkMonth()){
System.out.println("对不起,您输入的月份非法,请再次输入你想查询的月份,谢谢!");
System.out.println("请输入月份:");
this.month = input.nextInt();
}
}
private boolean checkYear(){
return this.year >= this.epochYear;
}
private boolean checkMonth(){
return 1 <= this.month && this.month <= 12;
}
public long calculateDays(){
long days = 0;
for (int i = epochYear; i < year; i++){
days += 365;
if (((i%4 == 0) && (i%100 != 0)) || (i%400 == 0)){
days += 1;
}
}
for (int i = 1; i < month; i++){
days += getMonthDays(i, year);
}
return days;
}
private int getMonthDays(int monthNum, int yearNum){
if (monthNum == 2 && (((yearNum%4 == 0) && (yearNum%100 != 0)) || (yearNum%400 == 0))){
return monthDays[0];
}
return monthDays[monthNum];
}
public void printCalendar(long days){
// 打印日历星期头
System.out.println("日\t一\t二\t三\t四\t五\t六");
// 定位1号在本月中的星期位置
long offsetDay = days % 7;
for (int i = 0; i <= offsetDay; i++){
System.out.print("\t");
}
System.out.print("1\t");
// 打印本月的其他日期:依次按照1号日期顺序打印即可
int monthdays = getMonthDays(month, year);
for (int i = 2; i <= monthdays; i++){
if((offsetDay + i - 1) % 7 == 6){
System.out.println();
}
System.out.print(i+"\t");
}
}
public static void calendarManager(){
System.out.println("Welcome to my calendar app.");
Calendar calendar = new Calendar();
Scanner in = new Scanner(System.in);
int command = 1;
while (command == 1){
calendar.getInputData();
calendar.printCalendar(calendar.calculateDays());
System.out.println("\n\n\nContinue to use calendar app? (1/0)");
command = in.nextInt();
}
}
public static void main(String[] args){
Calendar.calendarManager();
}
/*********** version 0.1 ***********/
private void calendar_01(){
int sum = 0;
int i;
Scanner input = new Scanner(System.in);
System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
for (i=1900; i<year; i++)
{
if (((i%4 == 0) && (i%100 != 0)) || (i%400 == 0))
{
sum += 366;
}
else
{
sum += 365;
}
}
/*
switch (month)
{
case 1:
{
if (month == 1)
{
break;
}
else
{
sum += 31;
}
}
case 2:
{
if (month == 2)
{
break;
}
else
{
if(((year%4 == 0) && (year%100 != 0)) || (year%400 == 0))
{
sum += 29;
}
else
{
sum += 28;
}
}
}
case 3:
{
if (month == 3)
{
break;
}
else
{
sum += 31;
}
}
case 4:
{
if (month == 4)
{
break;
}
else
{
sum += 30;
}
}
case 5:
{
if (month == 5)
{
break;
}
else
{
sum += 31;
}
}
case 6:
{
if (month == 6)
{
break;
}
else
{
sum += 30;
}
}
}
*/
for (i=1; i<month; i++)
{
if (i == 2)
{
if (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0))
{
sum += 29;
}
else
{
sum += 28;
}
}
else
{
if ((i == 4) || (i == 6) || (i == 9) || (i == 11))
{
sum += 30;
}
else
{
sum += 31;
}
}
}
sum++; //1号,多加一天。
int weekday = sum%7;
System.out.println("日\t一\t二\t三\t四\t五\t六");
for (i=0; i<weekday; i++)
{
System.out.print("\t");
}
System.out.print("1\t");
int monthdays;
if (month == 2)
{
if (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0))
{
monthdays = 29;
}
else
{
monthdays = 28;
}
}
else
{
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
{
monthdays = 30;
}
else
{
monthdays = 31;
}
}
for (i=2; i<=monthdays; i++)
{
if(sum%7 == 6)
{
System.out.println();
}
System.out.print(i+"\t");
sum++;
}
}
}