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