51 |
* integer fields which represent <code>YEAR</code>, |
* integer fields which represent <code>YEAR</code>, |
52 |
* <code>MONTH</code>, <code>DAY</code>, etc. The <code>Date</code> |
* <code>MONTH</code>, <code>DAY</code>, etc. The <code>Date</code> |
53 |
* object represents a time in milliseconds since the Epoch. <br> |
* object represents a time in milliseconds since the Epoch. <br> |
54 |
* |
* |
55 |
* This class is locale sensitive. To get the Object matching the |
* This class is locale sensitive. To get the Object matching the |
56 |
* current locale you can use <code>getInstance</code>. You can even provide |
* current locale you can use <code>getInstance</code>. You can even provide |
57 |
* a locale or a timezone. <code>getInstance</code> returns currently |
* a locale or a timezone. <code>getInstance</code> returns currently |
78 |
* and for the first line all fields are set, that line is used to |
* and for the first line all fields are set, that line is used to |
79 |
* compute the day. <br> |
* compute the day. <br> |
80 |
* |
* |
81 |
* |
* |
82 |
<pre>month + day_of_month |
<pre>month + day_of_month |
83 |
month + week_of_month + day_of_week |
month + week_of_month + day_of_week |
84 |
month + day_of_week_of_month + day_of_week |
month + day_of_week_of_month + day_of_week |
85 |
day_of_year |
day_of_year |
86 |
day_of_week + week_of_year</pre> |
day_of_week + week_of_year</pre> |
87 |
* |
* |
88 |
* The hour_of_day-field takes precedence over the ampm and |
* The hour_of_day-field takes precedence over the ampm and |
89 |
* hour_of_ampm fields. <br> |
* hour_of_ampm fields. <br> |
90 |
* |
* |
92 |
* |
* |
93 |
* To convert a calendar to a human readable form and vice versa, use |
* To convert a calendar to a human readable form and vice versa, use |
94 |
* the <code>java.text.DateFormat</code> class. <br> |
* the <code>java.text.DateFormat</code> class. <br> |
95 |
* |
* |
96 |
* Other useful things you can do with an calendar, is |
* Other useful things you can do with an calendar, is |
97 |
* <code>roll</code>ing fields (that means increase/decrease a |
* <code>roll</code>ing fields (that means increase/decrease a |
98 |
* specific field by one, propagating overflows), or |
* specific field by one, propagating overflows), or |
101 |
* @see Date |
* @see Date |
102 |
* @see GregorianCalendar |
* @see GregorianCalendar |
103 |
* @see TimeZone |
* @see TimeZone |
104 |
* @see java.text.DateFormat |
* @see java.text.DateFormat |
105 |
*/ |
*/ |
106 |
public abstract class Calendar implements Serializable, Cloneable |
public abstract class Calendar implements Serializable, Cloneable |
107 |
{ |
{ |
109 |
* Constant representing the era time field. |
* Constant representing the era time field. |
110 |
*/ |
*/ |
111 |
public static final int ERA = 0; |
public static final int ERA = 0; |
112 |
|
|
113 |
/** |
/** |
114 |
* Constant representing the year time field. |
* Constant representing the year time field. |
115 |
*/ |
*/ |
116 |
public static final int YEAR = 1; |
public static final int YEAR = 1; |
117 |
|
|
118 |
/** |
/** |
119 |
* Constant representing the month time field. This field |
* Constant representing the month time field. This field |
120 |
* should contain one of the JANUARY,...,DECEMBER constants below. |
* should contain one of the JANUARY,...,DECEMBER constants below. |
121 |
*/ |
*/ |
122 |
public static final int MONTH = 2; |
public static final int MONTH = 2; |
123 |
|
|
124 |
/** |
/** |
125 |
* Constant representing the week of the year field. |
* Constant representing the week of the year field. |
126 |
* @see #setFirstDayOfWeek(int) |
* @see #setFirstDayOfWeek(int) |
127 |
*/ |
*/ |
128 |
public static final int WEEK_OF_YEAR = 3; |
public static final int WEEK_OF_YEAR = 3; |
129 |
|
|
130 |
/** |
/** |
131 |
* Constant representing the week of the month time field. |
* Constant representing the week of the month time field. |
132 |
* @see #setFirstDayOfWeek(int) |
* @see #setFirstDayOfWeek(int) |
133 |
*/ |
*/ |
134 |
public static final int WEEK_OF_MONTH = 4; |
public static final int WEEK_OF_MONTH = 4; |
135 |
|
|
136 |
/** |
/** |
137 |
* Constant representing the day time field, synonym for DAY_OF_MONTH. |
* Constant representing the day time field, synonym for DAY_OF_MONTH. |
138 |
*/ |
*/ |
139 |
public static final int DATE = 5; |
public static final int DATE = 5; |
140 |
|
|
141 |
/** |
/** |
142 |
* Constant representing the day time field. |
* Constant representing the day time field. |
143 |
*/ |
*/ |
144 |
public static final int DAY_OF_MONTH = 5; |
public static final int DAY_OF_MONTH = 5; |
145 |
|
|
146 |
/** |
/** |
147 |
* Constant representing the day of year time field. This is |
* Constant representing the day of year time field. This is |
148 |
* 1 for the first day in month. |
* 1 for the first day in month. |
149 |
*/ |
*/ |
150 |
public static final int DAY_OF_YEAR = 6; |
public static final int DAY_OF_YEAR = 6; |
151 |
|
|
152 |
/** |
/** |
153 |
* Constant representing the day of week time field. This field |
* Constant representing the day of week time field. This field |
154 |
* should contain one of the SUNDAY,...,SATURDAY constants below. |
* should contain one of the SUNDAY,...,SATURDAY constants below. |
155 |
*/ |
*/ |
156 |
public static final int DAY_OF_WEEK = 7; |
public static final int DAY_OF_WEEK = 7; |
157 |
|
|
158 |
/** |
/** |
159 |
* Constant representing the day-of-week-in-month field. For |
* Constant representing the day-of-week-in-month field. For |
160 |
* instance this field contains 2 for the second thursday in a |
* instance this field contains 2 for the second thursday in a |
162 |
* from the end of the month. |
* from the end of the month. |
163 |
*/ |
*/ |
164 |
public static final int DAY_OF_WEEK_IN_MONTH = 8; |
public static final int DAY_OF_WEEK_IN_MONTH = 8; |
165 |
|
|
166 |
/** |
/** |
167 |
* Constant representing the part of the day for 12-hour clock. This |
* Constant representing the part of the day for 12-hour clock. This |
168 |
* should be one of AM or PM. |
* should be one of AM or PM. |
169 |
*/ |
*/ |
170 |
public static final int AM_PM = 9; |
public static final int AM_PM = 9; |
171 |
|
|
172 |
/** |
/** |
173 |
* Constant representing the hour time field for 12-hour clock. |
* Constant representing the hour time field for 12-hour clock. |
174 |
*/ |
*/ |
175 |
public static final int HOUR = 10; |
public static final int HOUR = 10; |
176 |
|
|
177 |
/** |
/** |
178 |
* Constant representing the hour of day time field for 24-hour clock. |
* Constant representing the hour of day time field for 24-hour clock. |
179 |
*/ |
*/ |
180 |
public static final int HOUR_OF_DAY = 11; |
public static final int HOUR_OF_DAY = 11; |
181 |
|
|
182 |
/** |
/** |
183 |
* Constant representing the minute of hour time field. |
* Constant representing the minute of hour time field. |
184 |
*/ |
*/ |
185 |
public static final int MINUTE = 12; |
public static final int MINUTE = 12; |
186 |
|
|
187 |
/** |
/** |
188 |
* Constant representing the second time field. |
* Constant representing the second time field. |
189 |
*/ |
*/ |
190 |
public static final int SECOND = 13; |
public static final int SECOND = 13; |
191 |
|
|
192 |
/** |
/** |
193 |
* Constant representing the millisecond time field. |
* Constant representing the millisecond time field. |
194 |
*/ |
*/ |
195 |
public static final int MILLISECOND = 14; |
public static final int MILLISECOND = 14; |
196 |
|
|
197 |
/** |
/** |
198 |
* Constant representing the time zone offset time field for the |
* Constant representing the time zone offset time field for the |
199 |
* time given in the other fields. It is measured in |
* time given in the other fields. It is measured in |
200 |
* milliseconds. The default is the offset of the time zone. |
* milliseconds. The default is the offset of the time zone. |
201 |
*/ |
*/ |
202 |
public static final int ZONE_OFFSET = 15; |
public static final int ZONE_OFFSET = 15; |
203 |
|
|
204 |
/** |
/** |
205 |
* Constant representing the daylight saving time offset in |
* Constant representing the daylight saving time offset in |
206 |
* milliseconds. The default is the value given by the time zone. |
* milliseconds. The default is the value given by the time zone. |
207 |
*/ |
*/ |
208 |
public static final int DST_OFFSET = 16; |
public static final int DST_OFFSET = 16; |
209 |
|
|
210 |
/** |
/** |
211 |
* Number of time fields. |
* Number of time fields. |
212 |
*/ |
*/ |
216 |
* Constant representing Sunday. |
* Constant representing Sunday. |
217 |
*/ |
*/ |
218 |
public static final int SUNDAY = 1; |
public static final int SUNDAY = 1; |
219 |
|
|
220 |
/** |
/** |
221 |
* Constant representing Monday. |
* Constant representing Monday. |
222 |
*/ |
*/ |
223 |
public static final int MONDAY = 2; |
public static final int MONDAY = 2; |
224 |
|
|
225 |
/** |
/** |
226 |
* Constant representing Tuesday. |
* Constant representing Tuesday. |
227 |
*/ |
*/ |
228 |
public static final int TUESDAY = 3; |
public static final int TUESDAY = 3; |
229 |
|
|
230 |
/** |
/** |
231 |
* Constant representing Wednesday. |
* Constant representing Wednesday. |
232 |
*/ |
*/ |
233 |
public static final int WEDNESDAY = 4; |
public static final int WEDNESDAY = 4; |
234 |
|
|
235 |
/** |
/** |
236 |
* Constant representing Thursday. |
* Constant representing Thursday. |
237 |
*/ |
*/ |
238 |
public static final int THURSDAY = 5; |
public static final int THURSDAY = 5; |
239 |
|
|
240 |
/** |
/** |
241 |
* Constant representing Friday. |
* Constant representing Friday. |
242 |
*/ |
*/ |
243 |
public static final int FRIDAY = 6; |
public static final int FRIDAY = 6; |
244 |
|
|
245 |
/** |
/** |
246 |
* Constant representing Saturday. |
* Constant representing Saturday. |
247 |
*/ |
*/ |
251 |
* Constant representing January. |
* Constant representing January. |
252 |
*/ |
*/ |
253 |
public static final int JANUARY = 0; |
public static final int JANUARY = 0; |
254 |
|
|
255 |
/** |
/** |
256 |
* Constant representing February. |
* Constant representing February. |
257 |
*/ |
*/ |
258 |
public static final int FEBRUARY = 1; |
public static final int FEBRUARY = 1; |
259 |
|
|
260 |
/** |
/** |
261 |
* Constant representing March. |
* Constant representing March. |
262 |
*/ |
*/ |
263 |
public static final int MARCH = 2; |
public static final int MARCH = 2; |
264 |
|
|
265 |
/** |
/** |
266 |
* Constant representing April. |
* Constant representing April. |
267 |
*/ |
*/ |
268 |
public static final int APRIL = 3; |
public static final int APRIL = 3; |
269 |
|
|
270 |
/** |
/** |
271 |
* Constant representing May. |
* Constant representing May. |
272 |
*/ |
*/ |
273 |
public static final int MAY = 4; |
public static final int MAY = 4; |
274 |
|
|
275 |
/** |
/** |
276 |
* Constant representing June. |
* Constant representing June. |
277 |
*/ |
*/ |
278 |
public static final int JUNE = 5; |
public static final int JUNE = 5; |
279 |
|
|
280 |
/** |
/** |
281 |
* Constant representing July. |
* Constant representing July. |
282 |
*/ |
*/ |
283 |
public static final int JULY = 6; |
public static final int JULY = 6; |
284 |
|
|
285 |
/** |
/** |
286 |
* Constant representing August. |
* Constant representing August. |
287 |
*/ |
*/ |
288 |
public static final int AUGUST = 7; |
public static final int AUGUST = 7; |
289 |
|
|
290 |
/** |
/** |
291 |
* Constant representing September. |
* Constant representing September. |
292 |
*/ |
*/ |
293 |
public static final int SEPTEMBER = 8; |
public static final int SEPTEMBER = 8; |
294 |
|
|
295 |
/** |
/** |
296 |
* Constant representing October. |
* Constant representing October. |
297 |
*/ |
*/ |
298 |
public static final int OCTOBER = 9; |
public static final int OCTOBER = 9; |
299 |
|
|
300 |
/** |
/** |
301 |
* Constant representing November. |
* Constant representing November. |
302 |
*/ |
*/ |
303 |
public static final int NOVEMBER = 10; |
public static final int NOVEMBER = 10; |
304 |
|
|
305 |
/** |
/** |
306 |
* Constant representing December. |
* Constant representing December. |
307 |
*/ |
*/ |
308 |
public static final int DECEMBER = 11; |
public static final int DECEMBER = 11; |
309 |
|
|
310 |
/** |
/** |
311 |
* Constant representing Undecimber. This is an artificial name useful |
* Constant representing Undecimber. This is an artificial name useful |
312 |
* for lunar calendars. |
* for lunar calendars. |
317 |
* Useful constant for 12-hour clock. |
* Useful constant for 12-hour clock. |
318 |
*/ |
*/ |
319 |
public static final int AM = 0; |
public static final int AM = 0; |
320 |
|
|
321 |
/** |
/** |
322 |
* Useful constant for 12-hour clock. |
* Useful constant for 12-hour clock. |
323 |
*/ |
*/ |
329 |
* @serial |
* @serial |
330 |
*/ |
*/ |
331 |
protected int[] fields = new int[FIELD_COUNT]; |
protected int[] fields = new int[FIELD_COUNT]; |
332 |
|
|
333 |
/** |
/** |
334 |
* The flags which tell if the fields above have a value. |
* The flags which tell if the fields above have a value. |
335 |
* @serial |
* @serial |
336 |
*/ |
*/ |
337 |
protected boolean[] isSet = new boolean[FIELD_COUNT]; |
protected boolean[] isSet = new boolean[FIELD_COUNT]; |
338 |
|
|
339 |
/** |
/** |
340 |
* The time in milliseconds since the epoch. |
* The time in milliseconds since the epoch. |
341 |
* @serial |
* @serial |
342 |
*/ |
*/ |
343 |
protected long time; |
protected long time; |
344 |
|
|
345 |
/** |
/** |
346 |
* Tells if the above field has a valid value. |
* Tells if the above field has a valid value. |
347 |
* @serial |
* @serial |
348 |
*/ |
*/ |
349 |
protected boolean isTimeSet; |
protected boolean isTimeSet; |
350 |
|
|
351 |
/** |
/** |
352 |
* Tells if the fields have a valid value. This superseeds the isSet |
* Tells if the fields have a valid value. This superseeds the isSet |
353 |
* array. |
* array. |
373 |
|
|
374 |
/** |
/** |
375 |
* Sets what the first day of week is. This is used for |
* Sets what the first day of week is. This is used for |
376 |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
377 |
* @serial |
* @serial |
378 |
*/ |
*/ |
379 |
private int firstDayOfWeek; |
private int firstDayOfWeek; |
390 |
/** |
/** |
391 |
* Is set to true if DST_OFFSET is explicitly set. In that case |
* Is set to true if DST_OFFSET is explicitly set. In that case |
392 |
* it's value overrides the value computed from the current |
* it's value overrides the value computed from the current |
393 |
* time and the timezone. |
* time and the timezone. |
394 |
*/ |
*/ |
395 |
private boolean explicitDSTOffset = false; |
private boolean explicitDSTOffset = false; |
396 |
|
|
397 |
/** |
/** |
398 |
* The version of the serialized data on the stream. |
* The version of the serialized data on the stream. |
399 |
* <dl><dt>0 or not present</dt> |
* <dl><dt>0 or not present</dt> |
400 |
* <dd> JDK 1.1.5 or later.</dd> |
* <dd> JDK 1.1.5 or later.</dd> |
401 |
* <dl><dt>1</dt> |
* <dl><dt>1</dt> |
419 |
private static final String bundleName = "gnu.java.locale.Calendar"; |
private static final String bundleName = "gnu.java.locale.Calendar"; |
420 |
|
|
421 |
/** |
/** |
422 |
* get resource bundle: |
* get resource bundle: |
423 |
* The resources should be loaded via this method only. Iff an application |
* The resources should be loaded via this method only. Iff an application |
424 |
* uses this method, the resourcebundle is required. |
* uses this method, the resourcebundle is required. |
425 |
*/ |
*/ |
426 |
private static ResourceBundle getBundle(Locale locale) |
private static ResourceBundle getBundle(Locale locale) |
427 |
{ |
{ |
428 |
return ResourceBundle.getBundle(bundleName, locale, |
return ResourceBundle.getBundle(bundleName, locale, |
429 |
ClassLoader.getSystemClassLoader()); |
ClassLoader.getSystemClassLoader()); |
430 |
} |
} |
431 |
|
|
432 |
/** |
/** |
452 |
ResourceBundle rb = getBundle(locale); |
ResourceBundle rb = getBundle(locale); |
453 |
|
|
454 |
firstDayOfWeek = ((Integer) rb.getObject("firstDayOfWeek")).intValue(); |
firstDayOfWeek = ((Integer) rb.getObject("firstDayOfWeek")).intValue(); |
455 |
minimalDaysInFirstWeek = |
minimalDaysInFirstWeek = ((Integer) rb.getObject("minimalDaysInFirstWeek")) |
456 |
((Integer) rb.getObject("minimalDaysInFirstWeek")).intValue(); |
.intValue(); |
457 |
} |
} |
458 |
|
|
459 |
/** |
/** |
485 |
return getInstance(TimeZone.getDefault(), locale); |
return getInstance(TimeZone.getDefault(), locale); |
486 |
} |
} |
487 |
|
|
488 |
/** |
/** |
489 |
* Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle |
* Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle |
490 |
* lookup for every getInstance call. |
* lookup for every getInstance call. |
491 |
*/ |
*/ |
492 |
private static HashMap cache = new HashMap(); |
private static HashMap cache = new HashMap(); |
493 |
|
|
494 |
/** Preset argument types for calendar-class constructor lookup. */ |
/** Preset argument types for calendar-class constructor lookup. */ |
495 |
private static Class[] ctorArgTypes |
private static Class[] ctorArgTypes = new Class[] |
496 |
= new Class[] {TimeZone.class, Locale.class}; |
{ |
497 |
|
TimeZone.class, Locale.class |
498 |
|
}; |
499 |
|
|
500 |
/** |
/** |
501 |
* Creates a calendar representing the actual time, using the given |
* Creates a calendar representing the actual time, using the given |
523 |
} |
} |
524 |
} |
} |
525 |
|
|
526 |
// GregorianCalendar is by far the most common case. Optimize by |
// GregorianCalendar is by far the most common case. Optimize by |
527 |
// avoiding reflection. |
// avoiding reflection. |
528 |
if (calendarClass == GregorianCalendar.class) |
if (calendarClass == GregorianCalendar.class) |
529 |
return new GregorianCalendar(zone, locale); |
return new GregorianCalendar(zone, locale); |
531 |
if (Calendar.class.isAssignableFrom(calendarClass)) |
if (Calendar.class.isAssignableFrom(calendarClass)) |
532 |
{ |
{ |
533 |
Constructor ctor = calendarClass.getConstructor(ctorArgTypes); |
Constructor ctor = calendarClass.getConstructor(ctorArgTypes); |
534 |
return (Calendar) ctor.newInstance(new Object[] {zone, locale}); |
return (Calendar) ctor.newInstance(new Object[] { zone, locale }); |
535 |
} |
} |
536 |
} |
} |
537 |
catch (ClassNotFoundException ex) |
catch (ClassNotFoundException ex) |
554 |
{ |
{ |
555 |
exception = ex; |
exception = ex; |
556 |
} |
} |
557 |
|
|
558 |
throw new RuntimeException("Error instantiating calendar for locale " + |
throw new RuntimeException("Error instantiating calendar for locale " |
559 |
locale, exception); |
+ locale, exception); |
560 |
} |
} |
561 |
|
|
562 |
/** |
/** |
580 |
* Converts the milliseconds since the epoch UTC |
* Converts the milliseconds since the epoch UTC |
581 |
* (<code>time</code>) to time fields |
* (<code>time</code>) to time fields |
582 |
* (<code>fields</code>). Override this method if you write your |
* (<code>fields</code>). Override this method if you write your |
583 |
* own Calendar. |
* own Calendar. |
584 |
*/ |
*/ |
585 |
protected abstract void computeFields(); |
protected abstract void computeFields(); |
586 |
|
|
591 |
*/ |
*/ |
592 |
public final Date getTime() |
public final Date getTime() |
593 |
{ |
{ |
594 |
if (!isTimeSet) |
if (! isTimeSet) |
595 |
computeTime(); |
computeTime(); |
596 |
return new Date(time); |
return new Date(time); |
597 |
} |
} |
612 |
*/ |
*/ |
613 |
public long getTimeInMillis() |
public long getTimeInMillis() |
614 |
{ |
{ |
615 |
if (!isTimeSet) |
if (! isTimeSet) |
616 |
computeTime(); |
computeTime(); |
617 |
return time; |
return time; |
618 |
} |
} |
643 |
public int get(int field) |
public int get(int field) |
644 |
{ |
{ |
645 |
// If the requested field is invalid, force all fields to be recomputed. |
// If the requested field is invalid, force all fields to be recomputed. |
646 |
if (!isSet[field]) |
if (! isSet[field]) |
647 |
areFieldsSet = false; |
areFieldsSet = false; |
648 |
complete(); |
complete(); |
649 |
return fields[field]; |
return fields[field]; |
650 |
} |
} |
651 |
|
|
652 |
/** |
/** |
653 |
* Gets the value of the specified field. This method doesn't |
* Gets the value of the specified field. This method doesn't |
654 |
* recompute the fields, if they are invalid. |
* recompute the fields, if they are invalid. |
655 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
656 |
* @return the value of the specified field, undefined if |
* @return the value of the specified field, undefined if |
701 |
isSet[HOUR_OF_DAY] = false; |
isSet[HOUR_OF_DAY] = false; |
702 |
break; |
break; |
703 |
case DST_OFFSET: |
case DST_OFFSET: |
704 |
explicitDSTOffset = true; |
explicitDSTOffset = true; |
705 |
} |
} |
706 |
|
|
707 |
// May have crossed over a DST boundary. |
// May have crossed over a DST boundary. |
708 |
if (!explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET)) |
if (! explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET)) |
709 |
isSet[DST_OFFSET] = false; |
isSet[DST_OFFSET] = false; |
710 |
} |
} |
711 |
|
|
728 |
isSet[DAY_OF_WEEK] = false; |
isSet[DAY_OF_WEEK] = false; |
729 |
isSet[DAY_OF_WEEK_IN_MONTH] = false; |
isSet[DAY_OF_WEEK_IN_MONTH] = false; |
730 |
|
|
731 |
if (!explicitDSTOffset) |
if (! explicitDSTOffset) |
732 |
isSet[DST_OFFSET] = false; // May have crossed a DST boundary. |
isSet[DST_OFFSET] = false; // May have crossed a DST boundary. |
733 |
} |
} |
734 |
|
|
735 |
/** |
/** |
759 |
* @param minute the minute. |
* @param minute the minute. |
760 |
* @param second the second. |
* @param second the second. |
761 |
*/ |
*/ |
762 |
public final void set(int year, int month, int date, |
public final void set(int year, int month, int date, int hour, int minute, |
763 |
int hour, int minute, int second) |
int second) |
764 |
{ |
{ |
765 |
set(year, month, date, hour, minute); |
set(year, month, date, hour, minute); |
766 |
fields[SECOND] = second; |
fields[SECOND] = second; |
810 |
|
|
811 |
/** |
/** |
812 |
* Fills any unset fields in the time field list |
* Fills any unset fields in the time field list |
813 |
* @return true if the specified field has a value. |
* @return true if the specified field has a value. |
814 |
*/ |
*/ |
815 |
protected void complete() |
protected void complete() |
816 |
{ |
{ |
817 |
if (!isTimeSet) |
if (! isTimeSet) |
818 |
computeTime(); |
computeTime(); |
819 |
if (!areFieldsSet) |
if (! areFieldsSet) |
820 |
computeFields(); |
computeFields(); |
821 |
} |
} |
822 |
|
|
823 |
/** |
/** |
824 |
* Compares the given calendar with this. |
* Compares the given calendar with this. |
825 |
* @param o the object to that we should compare. |
* @param o the object to that we should compare. |
826 |
* @return true, if the given object is a calendar, that represents |
* @return true, if the given object is a calendar, that represents |
827 |
* the same time (but doesn't necessary have the same fields). |
* the same time (but doesn't necessary have the same fields). |
829 |
public boolean equals(Object o) |
public boolean equals(Object o) |
830 |
{ |
{ |
831 |
return (o instanceof Calendar) |
return (o instanceof Calendar) |
832 |
&& getTimeInMillis() == ((Calendar) o).getTimeInMillis(); |
&& getTimeInMillis() == ((Calendar) o).getTimeInMillis(); |
833 |
} |
} |
834 |
|
|
835 |
/** |
/** |
836 |
* Returns a hash code for this calendar. |
* Returns a hash code for this calendar. |
837 |
* @return a hash code, which fullfits the general contract of |
* @return a hash code, which fullfits the general contract of |
838 |
* <code>hashCode()</code> |
* <code>hashCode()</code> |
839 |
*/ |
*/ |
840 |
public int hashCode() |
public int hashCode() |
844 |
} |
} |
845 |
|
|
846 |
/** |
/** |
847 |
* Compares the given calendar with this. |
* Compares the given calendar with this. |
848 |
* @param o the object to that we should compare. |
* @param o the object to that we should compare. |
849 |
* @return true, if the given object is a calendar, and this calendar |
* @return true, if the given object is a calendar, and this calendar |
850 |
* represents a smaller time than the calendar o. |
* represents a smaller time than the calendar o. |
857 |
} |
} |
858 |
|
|
859 |
/** |
/** |
860 |
* Compares the given calendar with this. |
* Compares the given calendar with this. |
861 |
* @param o the object to that we should compare. |
* @param o the object to that we should compare. |
862 |
* @return true, if the given object is a calendar, and this calendar |
* @return true, if the given object is a calendar, and this calendar |
863 |
* represents a bigger time than the calendar o. |
* represents a bigger time than the calendar o. |
884 |
/** |
/** |
885 |
* Rolls the specified time field up or down. This means add one |
* Rolls the specified time field up or down. This means add one |
886 |
* to the specified field, but don't change the other fields. If |
* to the specified field, but don't change the other fields. If |
887 |
* the maximum for this field is reached, start over with the |
* the maximum for this field is reached, start over with the |
888 |
* minimum value. <br> |
* minimum value. <br> |
889 |
* |
* |
890 |
* <strong>Note:</strong> There may be situation, where the other |
* <strong>Note:</strong> There may be situation, where the other |
891 |
* fields must be changed, e.g rolling the month on May, 31. |
* fields must be changed, e.g rolling the month on May, 31. |
892 |
* The date June, 31 is automatically converted to July, 1. |
* The date June, 31 is automatically converted to July, 1. |
893 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
894 |
* @param up the direction, true for up, false for down. |
* @param up the direction, true for up, false for down. |
907 |
* |
* |
908 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
909 |
* @param amount the amount to roll by, positive for rolling up, |
* @param amount the amount to roll by, positive for rolling up, |
910 |
* negative for rolling down. |
* negative for rolling down. |
911 |
* @throws ArrayIndexOutOfBoundsException if the field is outside |
* @throws ArrayIndexOutOfBoundsException if the field is outside |
912 |
* the valid range. The value of field must be >= 0 and |
* the valid range. The value of field must be >= 0 and |
913 |
* <= <code>FIELD_COUNT</code>. |
* <= <code>FIELD_COUNT</code>. |
927 |
} |
} |
928 |
} |
} |
929 |
|
|
|
|
|
930 |
/** |
/** |
931 |
* Sets the time zone to the specified value. |
* Sets the time zone to the specified value. |
932 |
* @param zone the new time zone |
* @param zone the new time zone |
970 |
|
|
971 |
/** |
/** |
972 |
* Sets what the first day of week is. This is used for |
* Sets what the first day of week is. This is used for |
973 |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
974 |
* @param value the first day of week. One of SUNDAY to SATURDAY. |
* @param value the first day of week. One of SUNDAY to SATURDAY. |
975 |
*/ |
*/ |
976 |
public void setFirstDayOfWeek(int value) |
public void setFirstDayOfWeek(int value) |
980 |
|
|
981 |
/** |
/** |
982 |
* Gets what the first day of week is. This is used for |
* Gets what the first day of week is. This is used for |
983 |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
* WEEK_OF_MONTH and WEEK_OF_YEAR fields. |
984 |
* @return the first day of week. One of SUNDAY to SATURDAY. |
* @return the first day of week. One of SUNDAY to SATURDAY. |
985 |
*/ |
*/ |
986 |
public int getFirstDayOfWeek() |
public int getFirstDayOfWeek() |
1024 |
*/ |
*/ |
1025 |
public abstract int getMaximum(int field); |
public abstract int getMaximum(int field); |
1026 |
|
|
|
|
|
1027 |
/** |
/** |
1028 |
* Gets the greatest minimum value that is allowed for the specified field. |
* Gets the greatest minimum value that is allowed for the specified field. |
1029 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
1035 |
* Gets the smallest maximum value that is allowed for the |
* Gets the smallest maximum value that is allowed for the |
1036 |
* specified field. For example this is 28 for DAY_OF_MONTH. |
* specified field. For example this is 28 for DAY_OF_MONTH. |
1037 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
1038 |
* @return the least maximum value. |
* @return the least maximum value. |
1039 |
*/ |
*/ |
1040 |
public abstract int getLeastMaximum(int field); |
public abstract int getLeastMaximum(int field); |
1041 |
|
|
1051 |
*/ |
*/ |
1052 |
public int getActualMinimum(int field) |
public int getActualMinimum(int field) |
1053 |
{ |
{ |
1054 |
Calendar tmp = (Calendar)clone(); // To avoid restoring state |
Calendar tmp = (Calendar) clone(); // To avoid restoring state |
1055 |
int min = tmp.getGreatestMinimum(field); |
int min = tmp.getGreatestMinimum(field); |
1056 |
int end = tmp.getMinimum(field); |
int end = tmp.getMinimum(field); |
1057 |
tmp.set(field, min); |
tmp.set(field, min); |
1058 |
for (; min > end; min--) |
for (; min > end; min--) |
1059 |
{ |
{ |
1060 |
tmp.add(field, -1); // Try to get smaller |
tmp.add(field, -1); // Try to get smaller |
1061 |
if (tmp.get(field) != min - 1) |
if (tmp.get(field) != min - 1) |
1062 |
break; // Done if not successful |
break; // Done if not successful |
|
|
|
1063 |
} |
} |
1064 |
return min; |
return min; |
1065 |
} |
} |
1068 |
* Gets the actual maximum value that is allowed for the specified field. |
* Gets the actual maximum value that is allowed for the specified field. |
1069 |
* This value is dependent on the values of the other fields. |
* This value is dependent on the values of the other fields. |
1070 |
* @param field the time field. One of the time field constants. |
* @param field the time field. One of the time field constants. |
1071 |
* @return the actual maximum value. |
* @return the actual maximum value. |
1072 |
* @throws ArrayIndexOutOfBoundsException if the field is outside |
* @throws ArrayIndexOutOfBoundsException if the field is outside |
1073 |
* the valid range. The value of field must be >= 0 and |
* the valid range. The value of field must be >= 0 and |
1074 |
* <= <code>FIELD_COUNT</code>. |
* <= <code>FIELD_COUNT</code>. |
1076 |
*/ |
*/ |
1077 |
public int getActualMaximum(int field) |
public int getActualMaximum(int field) |
1078 |
{ |
{ |
1079 |
Calendar tmp = (Calendar)clone(); // To avoid restoring state |
Calendar tmp = (Calendar) clone(); // To avoid restoring state |
1080 |
int max = tmp.getLeastMaximum(field); |
int max = tmp.getLeastMaximum(field); |
1081 |
int end = tmp.getMaximum(field); |
int end = tmp.getMaximum(field); |
1082 |
tmp.set(field, max); |
tmp.set(field, max); |
1098 |
{ |
{ |
1099 |
Calendar cal = (Calendar) super.clone(); |
Calendar cal = (Calendar) super.clone(); |
1100 |
cal.fields = (int[]) fields.clone(); |
cal.fields = (int[]) fields.clone(); |
1101 |
cal.isSet = (boolean[])isSet.clone(); |
cal.isSet = (boolean[]) isSet.clone(); |
1102 |
return cal; |
return cal; |
1103 |
} |
} |
1104 |
catch (CloneNotSupportedException ex) |
catch (CloneNotSupportedException ex) |
1107 |
} |
} |
1108 |
} |
} |
1109 |
|
|
1110 |
private static final String[] fieldNames = { |
private static final String[] fieldNames = |
1111 |
",ERA=", ",YEAR=", ",MONTH=", |
{ |
1112 |
",WEEK_OF_YEAR=", ",WEEK_OF_MONTH=", |
",ERA=", ",YEAR=", ",MONTH=", |
1113 |
",DAY_OF_MONTH=", ",DAY_OF_YEAR=", ",DAY_OF_WEEK=", |
",WEEK_OF_YEAR=", |
1114 |
",DAY_OF_WEEK_IN_MONTH=", |
",WEEK_OF_MONTH=", |
1115 |
",AM_PM=", ",HOUR=", ",HOUR_OF_DAY=", |
",DAY_OF_MONTH=", |
1116 |
",MINUTE=", ",SECOND=", ",MILLISECOND=", |
",DAY_OF_YEAR=", ",DAY_OF_WEEK=", |
1117 |
",ZONE_OFFSET=", ",DST_OFFSET=" |
",DAY_OF_WEEK_IN_MONTH=", |
1118 |
}; |
",AM_PM=", ",HOUR=", |
1119 |
|
",HOUR_OF_DAY=", ",MINUTE=", |
1120 |
|
",SECOND=", ",MILLISECOND=", |
1121 |
|
",ZONE_OFFSET=", ",DST_OFFSET=" |
1122 |
|
}; |
1123 |
|
|
1124 |
/** |
/** |
1125 |
* Returns a string representation of this object. It is mainly |
* Returns a string representation of this object. It is mainly |
1162 |
* says, that it could be omitted. */ |
* says, that it could be omitted. */ |
1163 |
private void writeObject(ObjectOutputStream stream) throws IOException |
private void writeObject(ObjectOutputStream stream) throws IOException |
1164 |
{ |
{ |
1165 |
if (!isTimeSet) |
if (! isTimeSet) |
1166 |
computeTime(); |
computeTime(); |
1167 |
stream.defaultWriteObject(); |
stream.defaultWriteObject(); |
1168 |
} |
} |
1174 |
throws IOException, ClassNotFoundException |
throws IOException, ClassNotFoundException |
1175 |
{ |
{ |
1176 |
stream.defaultReadObject(); |
stream.defaultReadObject(); |
1177 |
if (!isTimeSet) |
if (! isTimeSet) |
1178 |
computeTime(); |
computeTime(); |
1179 |
|
|
1180 |
if (serialVersionOnStream > 1) |
if (serialVersionOnStream > 1) |
1183 |
// Sun wants to remove all fields from the stream someday |
// Sun wants to remove all fields from the stream someday |
1184 |
// and will then increase the serialVersion number again. |
// and will then increase the serialVersion number again. |
1185 |
// We prepare to be compatible. |
// We prepare to be compatible. |
|
|
|
1186 |
fields = new int[FIELD_COUNT]; |
fields = new int[FIELD_COUNT]; |
1187 |
isSet = new boolean[FIELD_COUNT]; |
isSet = new boolean[FIELD_COUNT]; |
1188 |
areFieldsSet = false; |
areFieldsSet = false; |