传入当前日期获取下一天的日期【时隔2年,我抛弃数据结构和算法已经那么久了,真是不应该啊。我有罪。】

  1 package God.froest.test1;
2
3 import java.util.Scanner;
4
5 class Day {
6 private String year;
7 private String month;
8 private String day;
9
10 public String getYear() {
11 return year;
12 }
13
14 public void setYear(String year) {
15 this.year = year;
16 }
17
18 public String getMonth() {
19 return month;
20 }
21
22 public void setMonth(String month) {
23 this.month = month;
24 }
25
26 public String getDay() {
27 return day;
28 }
29
30 public void setDay(String day) {
31 this.day = day;
32 }
33
34 Day(String year, String month, String day) {
35 this.year = year;
36 this.month = month;
37 this.day = day;
38 }
39
40 public String toString() {
41 return year + month + day;
42 }
43 }
44
45 public class NextDay {
46 /**
47 * isLeap判断是否为闰年
48 * @param day
49 * @return
50 */
51 public Boolean isLeap(Day day) {
52 int year = Integer.parseInt(day.getYear());
53 return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
54 }
55 /**
56 * nextDay传入当前日期,得到下一天日期,格式"yyyyMMdd"
57 * @param day
58 * @return
59 */
60 public Day nextDay(Day day) {
61 int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
62 if (isLeap(day) && Integer.parseInt(day.getMonth()) == 2) {
63 days[2] = 29;
64 }
65 int tempDay = Integer.parseInt(day.getDay());
66 int tempMonth = Integer.parseInt(day.getMonth());
67 int tempYear = Integer.parseInt(day.getYear());
68 if (tempDay < days[tempMonth]) {
69 String temp = "" + (tempDay + 1);
70 if (tempDay + 1 < 10) {
71 temp = "0" + temp;
72 day.setDay(temp);
73 } else {
74 day.setDay(temp);
75 }
76 } else if (tempMonth != 12) {
77 String temp = tempMonth + 1 + "";
78 if (tempMonth + 1 < 10) {
79 temp = "0" + temp;
80 day.setMonth(temp);
81 day.setDay("01");
82 } else {
83 day.setMonth(temp);
84 day.setDay("01");
85 }
86 } else {
87 day.setYear(tempYear + 1 + "");
88 day.setMonth("01");
89 day.setDay("01");
90 }
91 return day;
92 }
93 /**
94 * isRightDate用于判断当前天的日期是否正确
95 * @param day
96 * @return
97 */
98 public Boolean isRightDate(Day day) {
99 int[] days = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
100 if (isLeap(day) && Integer.parseInt(day.getMonth()) == 2) {
101 days[2] = 29;
102 }
103 int tempDay = Integer.parseInt(day.getDay());
104 int tempMonth = Integer.parseInt(day.getMonth());
105 /**
106 * 判断天数大于那个月的最大天数
107 */
108 if (tempDay > days[tempMonth]) {
109 return false;
110 }
111 /**
112 * 判断不是闰年,但是日期为29
113 */
114 if(!isLeap(day)&&day.getDay()=="29"){
115 return false;
116 }
117 return true;
118 }
119
120 public static void main(String[] arg0) {
121 Scanner scan = new Scanner(System.in);
122 /**
123 * 连续输入
124 */
125 while (true) {
126 String date = scan.next();
127 String year = date.substring(0, 4);
128 String month = date.substring(4, 6);
129 String day = date.substring(6, 8);
130
131 NextDay dateUtil = new NextDay();
132 Day start = new Day(year, month, day);
133 if (!dateUtil.isRightDate(start)) {
134 System.out.println("请输入正确的日期");
135 continue;
136 }
137 Day nextDay = dateUtil.nextDay(start);
138 System.out.println(nextDay);
139 }
140 }
141 }

这个获得下一天的日期算是我重新捡起算法的第一步吧。不管用什么语言,算法思想是不会变的。

                                                                     ------>froest

posted on 2011-11-05 23:21  Kahuna  阅读(428)  评论(0编辑  收藏  举报

导航