1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.lum.common.client.widgets; |
17 | |
|
18 | |
import java.util.Date; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class GregorianCalendar extends Calendar |
31 | |
{ |
32 | |
public static final int BC = 0; |
33 | |
public static final int AD = 1; |
34 | |
|
35 | |
public GregorianCalendar() { |
36 | 0 | super(); |
37 | 0 | } |
38 | 0 | public GregorianCalendar(Date date) { |
39 | 0 | setTime(date); |
40 | 0 | } |
41 | 0 | public GregorianCalendar(int year, int month, int date) { |
42 | 0 | set(year, month, date); |
43 | 0 | } |
44 | 0 | public GregorianCalendar(int year, int month, int date, int hour) { |
45 | 0 | set(year, month, date, hour); |
46 | 0 | } |
47 | 0 | public GregorianCalendar(int year, int month, int date, int hour, int minute) { |
48 | 0 | set(year, month, date, hour, minute); |
49 | 0 | } |
50 | 0 | public GregorianCalendar(int year, int month, int date, int hour, int minute, int second) { |
51 | 0 | set(year, month, date, hour, minute, second); |
52 | 0 | } |
53 | |
|
54 | 0 | protected static int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31 ,30, 31, 30, 31}; |
55 | |
public static int getMaxDaysInMonth(int year, int month) { |
56 | 0 | if (month == Calendar.FEBRUARY && isLeapYear(year)) |
57 | 0 | return daysInMonth[month] + 1; |
58 | |
else |
59 | 0 | return daysInMonth[month]; |
60 | |
} |
61 | |
public int getMaxDaysInMonth() { |
62 | 0 | return getMaxDaysInMonth(this.year, this.month); |
63 | |
} |
64 | |
|
65 | |
public static int getNumOfWeeksInMonth(int year, int month, int firstDayOfWeek) { |
66 | 0 | int day1 = getWeekDay(year, month, 1); |
67 | 0 | int maxDays = getMaxDaysInMonth(year, month); |
68 | 0 | if (month == Calendar.FEBRUARY && !isLeapYear(year) && day1 == firstDayOfWeek) |
69 | 0 | return 4; |
70 | 0 | if (maxDays == 30 && (day1 > firstDayOfWeek + 5 || day1 < firstDayOfWeek)) |
71 | 0 | return 6; |
72 | 0 | if (maxDays == 31 && (day1 > firstDayOfWeek + 4 || day1 < firstDayOfWeek)) |
73 | 0 | return 6; |
74 | 0 | return 5; |
75 | |
} |
76 | |
public int getNumOfWeeksInMonth() { |
77 | 0 | return getNumOfWeeksInMonth(this.year, this.month, this.firstWeekDayOfWeek); |
78 | |
} |
79 | |
|
80 | |
public static int getWeekDay(int year, int month, int dayOfMonth) { |
81 | 0 | int weekDay = year - 1900; |
82 | 0 | weekDay += weekDay/4; |
83 | 0 | weekDay %= 7; |
84 | 0 | if (month <= Calendar.FEBRUARY && isLeapYear(weekDay)) |
85 | 0 | weekDay -= 1; |
86 | 0 | weekDay += dayOfMonth; |
87 | 0 | weekDay %= 7; |
88 | 0 | if (month == Calendar.MAY) |
89 | 0 | weekDay += 1; |
90 | 0 | else if (month == Calendar.AUGUST) |
91 | 0 | weekDay += 2; |
92 | 0 | else if (month == Calendar.FEBRUARY || month == Calendar.MARCH || month == Calendar.NOVEMBER) |
93 | 0 | weekDay += 3; |
94 | 0 | else if (month == Calendar.JUNE) |
95 | 0 | weekDay += 4; |
96 | 0 | else if (month == Calendar.SEPTEMBER || month == Calendar.DECEMBER) |
97 | 0 | weekDay += 5; |
98 | 0 | else if (month == Calendar.APRIL || month == Calendar.JULY) |
99 | 0 | weekDay += 6; |
100 | 0 | weekDay %= 7; |
101 | |
|
102 | 0 | return Calendar.SUNDAY + weekDay; |
103 | |
} |
104 | |
public int getWeekDay() { |
105 | 0 | return getWeekDay(this.year, this.month, this.dayOfMonth); |
106 | |
} |
107 | |
public int getFirstWeekDayOfMonth() { |
108 | 0 | return getWeekDay(this.year, this.month, 1); |
109 | |
} |
110 | |
|
111 | |
|
112 | |
public int getMinimum(int fieldCode) { |
113 | 0 | switch (fieldCode) { |
114 | |
case Calendar.MILLISECOND: |
115 | 0 | return 0; |
116 | |
case Calendar.SECOND: |
117 | 0 | return 0; |
118 | |
case Calendar.MINUTE: |
119 | 0 | return 0; |
120 | |
case Calendar.HOUR: |
121 | 0 | return 0; |
122 | |
case Calendar.HOUR_OF_DAY: |
123 | 0 | return 0; |
124 | |
case Calendar.DATE: |
125 | 0 | return 1; |
126 | |
case Calendar.MONTH: |
127 | 0 | return Calendar.JANUARY; |
128 | |
default: |
129 | 0 | return -1; |
130 | |
} |
131 | |
} |
132 | |
|
133 | |
public int getActualMinimum(int fieldCode) { |
134 | 0 | return getMinimum(fieldCode); |
135 | |
} |
136 | |
|
137 | |
public int getGreatestMinimum(int fieldCode) { |
138 | 0 | return getMinimum(fieldCode); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
public int getMaximum(int fieldCode) { |
143 | 0 | switch (fieldCode) { |
144 | |
case Calendar.MILLISECOND: |
145 | 0 | return 999; |
146 | |
case Calendar.SECOND: |
147 | 0 | return 59; |
148 | |
case Calendar.MINUTE: |
149 | 0 | return 59; |
150 | |
case Calendar.HOUR: |
151 | 0 | return 11; |
152 | |
case Calendar.HOUR_OF_DAY: |
153 | 0 | return 23; |
154 | |
case Calendar.DATE: |
155 | 0 | return 31; |
156 | |
case Calendar.MONTH: |
157 | 0 | return Calendar.DECEMBER; |
158 | |
default: |
159 | 0 | return -1; |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
public int getActualMaximum(int fieldCode) { |
164 | 0 | switch (fieldCode) { |
165 | |
case Calendar.MILLISECOND: |
166 | 0 | return 999; |
167 | |
case Calendar.SECOND: |
168 | 0 | return 59; |
169 | |
case Calendar.MINUTE: |
170 | 0 | return 59; |
171 | |
case Calendar.HOUR: |
172 | 0 | return 11; |
173 | |
case Calendar.HOUR_OF_DAY: |
174 | 0 | return 23; |
175 | |
case Calendar.DATE: |
176 | 0 | return getMaxDaysInMonth(); |
177 | |
case Calendar.MONTH: |
178 | 0 | return Calendar.DECEMBER; |
179 | |
default: |
180 | 0 | return -1; |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
public int getLeastMaximum(int fieldCode) { |
185 | 0 | switch (fieldCode) { |
186 | |
case Calendar.MILLISECOND: |
187 | 0 | return 999; |
188 | |
case Calendar.SECOND: |
189 | 0 | return 59; |
190 | |
case Calendar.MINUTE: |
191 | 0 | return 59; |
192 | |
case Calendar.HOUR: |
193 | 0 | return 11; |
194 | |
case Calendar.HOUR_OF_DAY: |
195 | 0 | return 23; |
196 | |
case Calendar.DATE: |
197 | 0 | return 28; |
198 | |
case Calendar.MONTH: |
199 | 0 | return Calendar.DECEMBER; |
200 | |
default: |
201 | 0 | return -1; |
202 | |
} |
203 | |
} |
204 | |
|
205 | |
|
206 | |
public void set(int fieldCode, int value) { |
207 | 0 | switch (fieldCode) { |
208 | |
case Calendar.SECOND: |
209 | 0 | this.second = value; |
210 | 0 | break; |
211 | |
case Calendar.MINUTE: |
212 | 0 | this.minute = value; |
213 | 0 | break; |
214 | |
case Calendar.HOUR: |
215 | 0 | this.hour = value; |
216 | 0 | break; |
217 | |
case Calendar.HOUR_OF_DAY: |
218 | 0 | if (value <= 12) { |
219 | 0 | this.hour = value; |
220 | 0 | this.amPm = Calendar.AM; |
221 | |
} else { |
222 | 0 | this.hour = value - 12; |
223 | 0 | this.amPm = Calendar.PM; |
224 | |
} |
225 | 0 | break; |
226 | |
case Calendar.DATE: |
227 | 0 | this.dayOfMonth = value; |
228 | 0 | break; |
229 | |
case Calendar.MONTH: |
230 | 0 | this.month = value; |
231 | 0 | if (this.dayOfMonth > getMaxDaysInMonth()) |
232 | 0 | this.dayOfMonth = getMaxDaysInMonth(); |
233 | |
break; |
234 | |
case Calendar.YEAR: |
235 | 0 | this.year = value; |
236 | 0 | break; |
237 | |
case Calendar.AM_PM: |
238 | 0 | this.amPm = value; |
239 | |
break; |
240 | |
} |
241 | 0 | computeTime(); |
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
|
246 | |
public void add(int fieldCode, int amount) { |
247 | 0 | switch (fieldCode) { |
248 | |
case Calendar.DATE: |
249 | 0 | amount *= 24; |
250 | |
case Calendar.HOUR: |
251 | 0 | amount *= 60; |
252 | |
case Calendar.MINUTE: |
253 | 0 | amount *= 60; |
254 | |
case Calendar.SECOND: |
255 | 0 | amount *= 1000; |
256 | |
case Calendar.MILLISECOND: |
257 | 0 | this.date.setTime(this.date.getTime() + amount); |
258 | 0 | computeFields(); |
259 | 0 | break; |
260 | |
case Calendar.MONTH: |
261 | 0 | this.year += (this.month + amount) / 12; |
262 | 0 | this.month = (this.month + amount) % 12; |
263 | 0 | if (this.month < 0) { |
264 | 0 | this.year -= 1; |
265 | 0 | this.month += 12; |
266 | |
} |
267 | 0 | if (this.dayOfMonth > getMaxDaysInMonth()) |
268 | 0 | this.dayOfMonth = getMaxDaysInMonth(); |
269 | 0 | computeTime(); |
270 | 0 | break; |
271 | |
case Calendar.YEAR: |
272 | 0 | this.year += amount; |
273 | 0 | computeTime(); |
274 | |
break; |
275 | |
} |
276 | 0 | } |
277 | |
|
278 | |
|
279 | |
public void roll(int fieldCode, int amount) { |
280 | 0 | switch (fieldCode) { |
281 | |
case Calendar.SECOND: |
282 | 0 | this.second = (this.second + amount) % 60; |
283 | 0 | if (this.second < 0) |
284 | 0 | this.second += 60; |
285 | |
break; |
286 | |
case Calendar.MINUTE: |
287 | 0 | this.minute = (this.minute + amount) % 60; |
288 | 0 | if (this.minute < 0) |
289 | 0 | this.minute += 60; |
290 | |
break; |
291 | |
case Calendar.HOUR: |
292 | 0 | this.hour = (this.hour + amount) % 24; |
293 | 0 | if (this.hour < 0) |
294 | 0 | this.hour += 24; |
295 | |
break; |
296 | |
case Calendar.DATE: { |
297 | 0 | this.dayOfMonth = (this.dayOfMonth + amount) % getActualMaximum(Calendar.DATE); |
298 | 0 | if (this.dayOfMonth < 0) |
299 | 0 | this.dayOfMonth += getActualMaximum(Calendar.DATE); |
300 | |
break; |
301 | |
} |
302 | |
case Calendar.MONTH: { |
303 | 0 | this.month = (this.month + amount) % 12; |
304 | 0 | if (this.month < 0) |
305 | 0 | this.month += 12; |
306 | 0 | if (this.dayOfMonth > getActualMaximum(Calendar.DATE)) |
307 | 0 | this.dayOfMonth = getActualMaximum(Calendar.DATE); |
308 | |
break; |
309 | |
} |
310 | |
case Calendar.YEAR: |
311 | 0 | this.year += amount; |
312 | |
break; |
313 | |
} |
314 | 0 | computeTime(); |
315 | 0 | } |
316 | |
|
317 | |
|
318 | |
|
319 | |
public boolean before(GregorianCalendar when) { |
320 | 0 | return this.date.before(when.getTime()); |
321 | |
} |
322 | |
public boolean after(GregorianCalendar when) { |
323 | 0 | return this.date.after(when.getTime()); |
324 | |
} |
325 | |
public boolean equals(GregorianCalendar obj) { |
326 | 0 | return this.date.equals(obj.getTime()); |
327 | |
} |
328 | |
|
329 | |
public boolean isLeapYear() { |
330 | 0 | return isLeapYear(this.year); |
331 | |
} |
332 | |
public static boolean isLeapYear(int year) { |
333 | 0 | if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) |
334 | 0 | return true; |
335 | 0 | return false; |
336 | |
} |
337 | |
|
338 | |
|
339 | |
|
340 | |
protected void computeFields() { |
341 | 0 | this.second = this.date.getSeconds(); |
342 | 0 | this.minute = this.date.getMinutes(); |
343 | 0 | this.hour = this.date.getHours(); |
344 | 0 | if (this.hour < 12) { |
345 | 0 | this.amPm = Calendar.AM; |
346 | |
} else { |
347 | 0 | this.hour -= 12; |
348 | 0 | this.amPm = Calendar.PM; |
349 | |
} |
350 | 0 | this.dayOfMonth = this.date.getDate(); |
351 | 0 | this.month = this.date.getMonth(); |
352 | 0 | this.year = this.date.getYear() + 1900; |
353 | |
|
354 | 0 | } |
355 | |
|
356 | |
protected void computeTime() { |
357 | 0 | this.date = new Date(this.year - 1900, this.month, this.dayOfMonth, this.hour, this.minute, this.second); |
358 | 0 | } |
359 | |
|
360 | |
|
361 | |
public Object clone() { |
362 | 0 | GregorianCalendar newCal = new GregorianCalendar(); |
363 | 0 | newCal.setTime(this.date); |
364 | 0 | return newCal; |
365 | |
} |
366 | |
|
367 | |
} |