对于任意3个整数,判断其是否为一个合法的日期值?
给定一个日期值,计算若干天数后的日期值。
如果有两个日期值,计算它们相距的天数。
我将这些功能写在了一个类里面。Test_2_6.java
使用Test_2_6_1.java,Test_2_6_2.java,Test_2_6_3.java 来使用Test_2_6.java
1
public class Test_2_6 ...{2
private int LaterDayCount,LaterCurrentDay;3
//判断是否为闰年4
public boolean isLeapYear(int year)5
...{6
boolean leap=false;7
leap=(year%400==0)|(year%100!=0)&(year%4==0);8
return leap;9
}10
//是否是合法日期值11
public boolean isDate(int year,int month,int day)12
...{13
boolean isDate=false;14
if (month>0&&month<13&&year>0&&day>0)15
...{16
switch (month)17
...{18
case 1:if (day<32)break;else return false;
19
case 2:20
if (!isLeapYear(year))//不是闰年21
...{22
if (day<29)break;else return false;
23
}24
else//闰年25
...{26
if (day<28)break;else return false;
27
}28
case 3:if (day<32)break;else return false;
29
case 4:if (day<31)break;else return false;
30
case 5:if (day<32)break;else return false;
31
case 6:if (day<31)break;else return false;
32
case 7:if (day<31)break;else return false;
33
case 8:if (day<32)break;else return false;
34
case 9:if (day<31)break;else return false;
35
case 10:if (day<32)break;else return false;
36
case 11:if (day<31)break;else return false;
37
case 12:if (day<32)break;else return false;
38
}39
isDate=true;40
}41
42
return isDate;43
}44
public boolean isDate(int year,int month)45
...{46
boolean isDate=false;47
if (month>0&&month<13&&year>0) isDate=true;48
return isDate;49
}50
public boolean isDate(int year)51
...{52
boolean isDate=false;53
if (year>0) isDate=true;54
return isDate;55
}56
//年历中,从第一天开始到某月的最后一天的天数总数。57
public int DateCountTable(int year,int month)58
...{59
int DateCount=0;60
if (this.isLeapYear(year))61
...{62
switch (month)63
...{64
case 1:DateCount=31;break;
65
case 2:DateCount=31+29;break;
66
case 3:DateCount=31+29+31;break;
67
case 4:DateCount=31+29+31+30;break;
68
case 5:DateCount=31+29+31+30+31;break;
69
case 6:DateCount=31+29+31+30+31+30;break;
70
case 7:DateCount=31+29+31+30+31+30+31;break;
71
case 8:DateCount=31+29+31+30+31+30+31+31;break;
72
case 9:DateCount=31+29+31+30+31+30+31+31+30;break;
73
case 10:DateCount=31+29+31+30+31+30+31+31+30+31;break;
74
case 11:DateCount=31+29+31+30+31+30+31+31+30+31+30;break;
75
case 12:DateCount=31+29+31+30+31+30+31+31+30+31+30+31;break;
76
}77
}78
else79
...{80
switch (month)81
...{82
case 1:DateCount=31;break;
83
case 2:DateCount=31+28;break;
84
case 3:DateCount=31+28+31;break;
85
case 4:DateCount=31+28+31+30;break;
86
case 5:DateCount=31+28+31+30+31;break;
87
case 6:DateCount=31+28+31+30+31+30;break;
88
case 7:DateCount=31+28+31+30+31+30+31;break;
89
case 8:DateCount=31+28+31+30+31+30+31+31;break;
90
case 9:DateCount=31+28+31+30+31+30+31+31+30;break;
91
case 10:DateCount=31+28+31+30+31+30+31+31+30+31;break;
92
case 11:DateCount=31+28+31+30+31+30+31+31+30+31+30;break;
93
case 12:DateCount=31+28+31+30+31+30+31+31+30+31+30+31;break;
94
}95
}96
97
return DateCount;98
}99
//某年一年内的天数100
public int GetDateCountInYear(int year)101
...{102
int DateCount=0;103
if (this.isLeapYear(year))104
DateCount=366;105
else106
DateCount=365;107
return DateCount;108
}109
//计算两个日期间的天数110
public int GetDayCountBetweenTwoDate(int beforeYear,int beforeMonth,int beforeDay,int afterYear,int afterMonth,int afterDay)111
...{112
int DayCount=1;113
//同年同月114
if (beforeYear==afterYear&&beforeMonth==afterMonth)115
...{116
DayCount=afterDay-beforeDay+1;117
return DayCount;118
}119
//同年不同月120
if (beforeYear==afterYear)121
...{122
int aH=this.DateCountTable(afterYear,afterMonth);123
int bH=this.DateCountTable(beforeYear,beforeMonth);124
intbrD=this.getRemainDateNumberInMonth(beforeYear,beforeMonth,beforeDay);
125
int arD=this.getRemainDateNumberInMonth(afterYear,afterMonth,afterDay);126
DayCount=(aH-(bH-brD))-arD;
127
return DayCount;128
}129
130
//不同年131
int currentYear;//yearCount=0;132
//隔1年133
//yearCount=afterYear-beforeYear-1;134
135
for (currentYear=beforeYear+1;currentYear<afterYear;currentYear++)136
...{137
DayCount=+this.GetDateCountInYear(currentYear);138
}139
140
int DateCountInYear=this.GetDateCountInYear(beforeYear);141
int bH=this.DateCountTable(beforeYear,beforeMonth);142
intbrH=this.getRemainDateNumberInMonth(beforeYear,beforeMonth,beforeDay);
143
int aH=this.DateCountTable(afterYear,afterMonth);144
int arH=this.getRemainDateNumberInMonth(afterYear,afterMonth,afterDay);145
DayCount+= (DateCountInYear-(bH-brH));
146
DayCount+= (aH-arH)-1;147
return DayCount;148
}149
//计算若干天数后的日期值150
public String getLaterDate(int year,int month,int day,int number)151
...{152
this.LaterCurrentDay=0;153
this.LaterDayCount=number;154
int currentYear=year,currentMonth=month;155
//第二天算“后”的第一天156
157
//如果 天数 大于该年剩余的天数,年数增加1,天数=天数-该年剩余的天数158
if (LaterDayCount>this.DateCountTable(year,12)-(this.DateCountTable(year,month)-day))159
...{160
LaterDayCount=LaterDayCount-(this.DateCountTable(year,12)-(this.DateCountTable(year,month)- this.getRemainDateNumberInMonth(year,month,day)));161
currentYear++;162
163
String tempDebug;164
tempDebug=this.getLaterDate(currentYear,1,1,LaterDayCount);165
}166
//如果 天数 大于该月剩余的天数167
if (LaterDayCount>this.getDateNumberInMonth(year,month)-day)168
...{169
170
LaterDayCount=LaterDayCount-(this.getRemainDateNumberInMonth(currentYear,currentMonth,day));171
currentMonth++;172
173
String tempDebug;174
tempDebug=this.getLaterDate(currentYear,currentMonth,1,LaterDayCount);175
176
}177
else178
...{179
if (LaterDayCount==0) LaterDayCount=1;180
LaterCurrentDay=LaterDayCount;181
}182
183
String laterDate,strCurrentYear,strCurrentMonth,strCurrentDay;184
strCurrentYear=String.valueOf(currentYear);185
strCurrentMonth=String.valueOf(currentMonth);186
strCurrentDay=String.valueOf(LaterCurrentDay);187
188
laterDate=strCurrentYear + "-" + strCurrentMonth+ "-" + strCurrentDay ;189
190
return laterDate;191
}192
//某年中某月的天数193
public int getDateNumberInMonth(int year,int month)194
...{195
if (!this.isDate(year,month)) return -1;196
int DateNumberInMonth=0;197
switch (month)198
...{199
case 1:DateNumberInMonth=31;break;
200
case 2:201
if (isLeapYear(year))//闰年202
...{203
DateNumberInMonth=29;break;
204
}205
else//非闰年206
...{207
DateNumberInMonth=28;break;
208
}209
case 3:DateNumberInMonth=31;break;
210
case 4:DateNumberInMonth=30;break;
211
case 5:DateNumberInMonth=31;break;
212
case 6:DateNumberInMonth=30;break;
213
case 7:DateNumberInMonth=31;break;
214
case 8:DateNumberInMonth=31;break;
215
case 9:DateNumberInMonth=30;break;
216
case 10:DateNumberInMonth=31;break;
217
case 11:DateNumberInMonth=30;break;
218
case 12:DateNumberInMonth=31;break;
219
}220
return DateNumberInMonth;221
}222
//某年中某月中某日剩余的天数223
public int getRemainDateNumberInMonth(int year,int month,int day)224
...{225
if (!isDate(year,month,day)) return -1;226
int remainDateNumberInMonth=0,DateNumberInMonth=0;227
DateNumberInMonth=this.getDateNumberInMonth(year,month);228
remainDateNumberInMonth= DateNumberInMonth-day ;229
return remainDateNumberInMonth;230
}231
}232
1
import java.io.BufferedReader;2
import java.io.IOException;3
import java.io.InputStreamReader;4
5
public class Test_2_6_1 ...{6
public static void main (String args[]) throws IOException7
...{8
String strYear,strMonth,strDay;9
int intYear,intMonth,intDay;10
Test_2_6 tools=new Test_2_6();11
12
System.out.println("判断任意3个整数是否为一个合法的日期(YYYY-MM-DD)");13
14
//读取15
System.out.println("请输入年:");16
BufferedReader brYear = new BufferedReader(new InputStreamReader( System.in));17
strYear =brYear.readLine();
18
intYear=(int) Integer.parseInt(strYear);19
20
System.out.println("请输入月:");21
BufferedReader brMonth = new BufferedReader(new InputStreamReader( System.in));22
strMonth =brMonth.readLine();
23
intMonth=(int) Integer.parseInt(strMonth);24
25
System.out.println("请输入天:");26
BufferedReader brDay = new BufferedReader(new InputStreamReader( System.in));27
strDay =brDay.readLine();
28
intDay=(int) Integer.parseInt(strDay);29
30
if (tools.isDate(intYear,intMonth,intDay))31
...{32
System.out.println("是有效的日期。");33
}34
else35
...{36
System.out.println("不是有效的日期。");37
}38
}39
}1
import java.io.*;2
public class Test_2_6_2 ...{3
public static void main (String args[]) throws IOException4
...{5
String strYear,strMonth,strDay,strNumber;6
int intYear,intMonth,intDay,intNumber;7
Test_2_6 tools=new Test_2_6();8
9
10
System.out.println("给定一个日期,计算若干天数后的日期");11
12
//读取13
System.out.println("请输入年:");14
BufferedReader brYear = new BufferedReader(new InputStreamReader( System.in));15
strYear =brYear.readLine();
16
intYear=(int) Integer.parseInt(strYear);17
18
System.out.println("请输入月:");19
BufferedReader brMonth = new BufferedReader(new InputStreamReader( System.in));20
strMonth =brMonth.readLine();
21
intMonth=(int) Integer.parseInt(strMonth);22
23
System.out.println("请输入天:");24
BufferedReader brDay = new BufferedReader(new InputStreamReader( System.in));25
strDay =brDay.readLine();
26
intDay=(int) Integer.parseInt(strDay);27
28
System.out.println("请输入您将计算的天数:");29
BufferedReader brNumber = new BufferedReader(new InputStreamReader( System.in));30
strNumber =brNumber.readLine();
31
intNumber=(int) Integer.parseInt(strNumber);32
33
34
System.out.println(intYear + "-" + intMonth + "-" + intDay +" 之后的 " + intNumber + " 天是 " + tools.getLaterDate(intYear,intMonth,intDay,intNumber));35
}36
}1
import java.io.*;2
public class Test_2_6_3 ...{3
public static void main (String args[]) throws IOException4
...{5
Test_2_6 tools=new Test_2_6();6
7
System.out.println("计算给定两个日期相距的天数");8
9
String strBeforeYear,strBeforeMonth,strBeforeDay,strAfterYear,strAfterMonth,strAfterDay;10
int intBeforeYear,intBeforeMonth,intBeforeDay,intAfterYear,intAfterMonth,intAfterDay;11
12
//读取13
System.out.println("请输入年:");14
BufferedReader brBeforeYear = new BufferedReader(new InputStreamReader( System.in));15
strBeforeYear =brBeforeYear.readLine();
16
intBeforeYear=(int) Integer.parseInt(strBeforeYear);17
18
System.out.println("请输入月:");19
BufferedReader brBeforeMonth = new BufferedReader(new InputStreamReader( System.in));20
strBeforeMonth =brBeforeMonth.readLine();
21
intBeforeMonth=(int) Integer.parseInt(strBeforeMonth);22
23
System.out.println("请输入日:");24
BufferedReader brBeforeDay = new BufferedReader(new InputStreamReader( System.in));25
strBeforeDay =brBeforeDay.readLine();
26
intBeforeDay=(int) Integer.parseInt(strBeforeDay);27
28
System.out.println("请输入年:");29
BufferedReader brAfterYear = new BufferedReader(new InputStreamReader( System.in));30
strAfterYear =brAfterYear.readLine();
31
intAfterYear=(int) Integer.parseInt(strAfterYear);32
33
System.out.println("请输入月:");34
BufferedReader brAfterMonth = new BufferedReader(new InputStreamReader( System.in));35
strAfterMonth =brAfterMonth.readLine();
36
intAfterMonth=(int) Integer.parseInt(strAfterMonth);37
38
System.out.println("请输入日:");39
BufferedReader brAfterDay = new BufferedReader(new InputStreamReader( System.in));40
strAfterDay =brAfterDay.readLine();
41
intAfterDay=(int) Integer.parseInt(strAfterDay);42
43
44
System.out.println(tools.GetDayCountBetweenTwoDate(intBeforeYear,intBeforeMonth,intBeforeDay,intAfterYear,intAfterMonth,intAfterDay));45
}46
}