1
|
|
package org.asyrinx.brownie.core.util;
|
2
|
|
|
3
|
|
import java.util.Calendar;
|
4
|
|
import java.util.Date;
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
public final class SimpleDate {
|
12
|
|
|
13
|
|
private final int year;
|
14
|
|
|
15
|
|
private final int month;
|
16
|
|
|
17
|
|
private final int day;
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
104
|
public SimpleDate(int year, int month, int day) {
|
23
|
104
|
super();
|
24
|
104
|
this.year = year;
|
25
|
104
|
this.month = month;
|
26
|
104
|
this.day = day;
|
27
|
|
}
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
16
|
public SimpleDate(Date d) {
|
33
|
16
|
this(d, Calendar.getInstance());
|
34
|
|
}
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
16
|
public SimpleDate(Date d, Calendar calendar) {
|
40
|
16
|
super();
|
41
|
16
|
calendar.setTime(d);
|
42
|
16
|
this.year = calendar.get(Calendar.YEAR);
|
43
|
16
|
this.month = calendar.get(Calendar.MONTH) + 1;
|
44
|
16
|
this.day = calendar.get(Calendar.DAY_OF_MONTH);
|
45
|
|
}
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
0
|
public Date toDate() {
|
51
|
0
|
return toDateBegin();
|
52
|
|
}
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
5
|
public Date toDateBegin() {
|
58
|
5
|
return DateUtils.toDate(year, month, day, 0, 0, 0, 0);
|
59
|
|
}
|
60
|
|
|
61
|
|
|
62
|
|
|
63
|
|
|
64
|
1
|
public Date toDateEnd() {
|
65
|
1
|
return DateUtils.toDate(year, month, day, 23, 59, 59, 999);
|
66
|
|
}
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
61
|
public boolean equals(Object obj) {
|
73
|
61
|
if (obj instanceof SimpleDate) {
|
74
|
56
|
final SimpleDate date = (SimpleDate) obj;
|
75
|
56
|
return (this.year == date.year) && (this.month == date.month)
|
76
|
|
&& (this.day == date.day);
|
77
|
5
|
} else if (obj instanceof Date) {
|
78
|
5
|
return this.equals(new SimpleDate((Date) obj));
|
79
|
|
} else {
|
80
|
0
|
return super.equals(obj);
|
81
|
|
}
|
82
|
|
}
|
83
|
|
|
84
|
60
|
public boolean after(SimpleDate when) {
|
85
|
60
|
if (when.year < this.year) {
|
86
|
16
|
return true;
|
87
|
44
|
} else if (when.year > this.year) {
|
88
|
14
|
return false;
|
89
|
|
} else {
|
90
|
30
|
if (when.month < this.month) {
|
91
|
13
|
return true;
|
92
|
17
|
} else if (when.month > this.month) {
|
93
|
8
|
return false;
|
94
|
|
} else {
|
95
|
9
|
return when.day < this.day;
|
96
|
|
}
|
97
|
|
}
|
98
|
|
}
|
99
|
|
|
100
|
74
|
public boolean before(SimpleDate when) {
|
101
|
74
|
if (when.year > this.year) {
|
102
|
21
|
return true;
|
103
|
53
|
} else if (when.year < this.year) {
|
104
|
20
|
return false;
|
105
|
|
} else {
|
106
|
33
|
if (when.month > this.month) {
|
107
|
14
|
return true;
|
108
|
19
|
} else if (when.month < this.month) {
|
109
|
9
|
return false;
|
110
|
|
} else {
|
111
|
10
|
return when.day > this.day;
|
112
|
|
}
|
113
|
|
}
|
114
|
|
}
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
0
|
public int getDayGap(SimpleDate when) {
|
126
|
0
|
int result = 0;
|
127
|
0
|
if (when.getYear() > this.getYear()) {
|
128
|
|
|
129
|
0
|
result = getDayGap(new SimpleDate(this.getYear(), 12, 31));
|
130
|
|
|
131
|
0
|
result++;
|
132
|
|
|
133
|
0
|
for (int y = this.getYear() + 1; y < when.getYear(); y++)
|
134
|
0
|
result += (DateUtils.isLeapYear(y) ? 366 : 365);
|
135
|
|
|
136
|
0
|
result += new SimpleDate(when.getYear(), 1, 1).getDayGap(when);
|
137
|
0
|
return result;
|
138
|
0
|
} else if (when.getYear() < this.getYear()) {
|
139
|
|
|
140
|
0
|
result = getDayGap(new SimpleDate(this.getYear(), 1, 1));
|
141
|
|
|
142
|
0
|
for (int y = this.getYear() - 1; y > when.getYear(); y--)
|
143
|
0
|
result -= (DateUtils.isLeapYear(y) ? 366 : 365);
|
144
|
|
|
145
|
0
|
result--;
|
146
|
|
|
147
|
0
|
result += new SimpleDate(when.getYear(), 12, 31).getDayGap(when);
|
148
|
0
|
return result;
|
149
|
|
} else {
|
150
|
0
|
if (when.getMonth() > this.getMonth()) {
|
151
|
|
|
152
|
0
|
result += (DateUtils.getLastDayOfMonth(this.getYear(), this
|
153
|
|
.getMonth()) - this.getDay());
|
154
|
|
|
155
|
0
|
result++;
|
156
|
|
|
157
|
0
|
for (int m = this.getMonth() + 1; m < when.getMonth(); m++)
|
158
|
0
|
result += DateUtils.getLastDayOfMonth(this.getYear(), m);
|
159
|
|
|
160
|
0
|
result += (when.getDay() - 1);
|
161
|
0
|
return result;
|
162
|
0
|
} else if (when.month < this.month) {
|
163
|
|
|
164
|
0
|
result -= this.getDay();
|
165
|
|
|
166
|
0
|
for (int m = this.getMonth() - 1; m > when.getMonth(); m--)
|
167
|
0
|
result -= DateUtils.getLastDayOfMonth(this.getYear(), m);
|
168
|
|
|
169
|
0
|
result -= (DateUtils.getLastDayOfMonth(when.getYear(), when
|
170
|
|
.getMonth()) - when.getDay());
|
171
|
0
|
return result;
|
172
|
|
} else {
|
173
|
0
|
return when.day - this.day;
|
174
|
|
}
|
175
|
|
}
|
176
|
|
}
|
177
|
|
|
178
|
|
|
179
|
|
|
180
|
|
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
|
|
191
|
|
|
192
|
0
|
public int getMonthGap(SimpleDate when) {
|
193
|
0
|
int result = 0;
|
194
|
0
|
if (when.getYear() > this.getYear()) {
|
195
|
|
|
196
|
0
|
result = 12 - this.getMonth();
|
197
|
|
|
198
|
0
|
for (int y = this.getYear() + 1; y < when.getYear(); y++)
|
199
|
0
|
result += 12;
|
200
|
|
|
201
|
0
|
result += when.getMonth();
|
202
|
0
|
return result;
|
203
|
0
|
} else if (when.getYear() < this.getYear()) {
|
204
|
|
|
205
|
0
|
result -= this.getMonth();
|
206
|
|
|
207
|
0
|
for (int y = this.getYear() + 1; y < when.getYear(); y++)
|
208
|
0
|
result -= 12;
|
209
|
|
|
210
|
0
|
result -= (12 - when.getMonth());
|
211
|
0
|
return result;
|
212
|
|
} else {
|
213
|
0
|
return when.getMonth() - this.getMonth();
|
214
|
|
}
|
215
|
|
}
|
216
|
|
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
|
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
|
|
228
|
|
|
229
|
|
|
230
|
|
|
231
|
0
|
public int getYearGap(SimpleDate when) {
|
232
|
0
|
return when.getYear() - this.getYear();
|
233
|
|
}
|
234
|
|
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
|
|
239
|
|
|
240
|
0
|
public int getDayOfWeek() {
|
241
|
0
|
Calendar calendar = Calendar.getInstance();
|
242
|
0
|
calendar.setTime(this.toDate());
|
243
|
0
|
return calendar.get(Calendar.DAY_OF_WEEK);
|
244
|
|
}
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
0
|
public String toString() {
|
250
|
0
|
return year + "/" + month + "/" + day;
|
251
|
|
}
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
|
|
256
|
0
|
public int getDay() {
|
257
|
0
|
return day;
|
258
|
|
}
|
259
|
|
|
260
|
|
|
261
|
|
|
262
|
|
|
263
|
0
|
public int getMonth() {
|
264
|
0
|
return month;
|
265
|
|
}
|
266
|
|
|
267
|
|
|
268
|
|
|
269
|
|
|
270
|
29
|
public int getYear() {
|
271
|
29
|
return year;
|
272
|
|
}
|
273
|
|
|
274
|
0
|
public SimpleDate prev(int length, int unit) {
|
275
|
0
|
return next(-length, unit);
|
276
|
|
}
|
277
|
|
|
278
|
0
|
public SimpleDate next(int length, int unit) {
|
279
|
0
|
final Calendar calendar = Calendar.getInstance();
|
280
|
0
|
calendar.setTime(this.toDate());
|
281
|
0
|
calendar.add(unit, length);
|
282
|
0
|
return new SimpleDate(calendar.getTime());
|
283
|
|
}
|
284
|
|
|
285
|
|
}
|