分享
 
 
 

Java 作业 习题2 关于日期的三道编程题

王朝java/jsp·作者佚名  2006-05-27
窄屏简体版  字體: |||超大  

对于任意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

else

79

...{

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

else

106

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

int

brD=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

int

brH=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

else

178

...{

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 IOException

7

...{

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

else

35

...{

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 IOException

4

...{

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 IOException

4

...{

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

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有