Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / util / Calendar.java
blob0e9284c7c212479e8fe8fe26b616d55a39f1ead6
1 /* Calendar.java --
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.util;
41 import java.io.IOException;
42 import java.io.ObjectInputStream;
43 import java.io.ObjectOutputStream;
44 import java.io.Serializable;
45 import java.lang.reflect.Constructor;
46 import java.lang.reflect.InvocationTargetException;
48 /**
49 * This class is an abstract base class for Calendars, which can be
50 * used to convert between <code>Date</code> objects and a set of
51 * integer fields which represent <code>YEAR</code>,
52 * <code>MONTH</code>, <code>DAY</code>, etc. The <code>Date</code>
53 * object represents a time in milliseconds since the Epoch. <br>
55 * This class is locale sensitive. To get the Object matching the
56 * current locale you can use <code>getInstance</code>. You can even provide
57 * a locale or a timezone. <code>getInstance</code> returns currently
58 * a <code>GregorianCalendar</code> for the current date. <br>
60 * If you want to convert a date from the Year, Month, Day, DayOfWeek,
61 * etc. Representation to a <code>Date</code>-Object, you can create
62 * a new Calendar with <code>getInstance()</code>,
63 * <code>clear()</code> all fields, <code>set(int,int)</code> the
64 * fields you need and convert it with <code>getTime()</code>. <br>
66 * If you want to convert a <code>Date</code>-object to the Calendar
67 * representation, create a new Calendar, assign the
68 * <code>Date</code>-Object with <code>setTime()</code>, and read the
69 * fields with <code>get(int)</code>. <br>
71 * When computing the date from time fields, it may happen, that there
72 * are either two few fields set, or some fields are inconsistent. This
73 * cases will handled in a calendar specific way. Missing fields are
74 * replaced by the fields of the epoch: 1970 January 1 00:00. <br>
76 * To understand, how the day of year is computed out of the fields
77 * look at the following table. It is traversed from top to bottom,
78 * and for the first line all fields are set, that line is used to
79 * compute the day. <br>
82 <pre>month + day_of_month
83 month + week_of_month + day_of_week
84 month + day_of_week_of_month + day_of_week
85 day_of_year
86 day_of_week + week_of_year</pre>
88 * The hour_of_day-field takes precedence over the ampm and
89 * hour_of_ampm fields. <br>
91 * <STRONG>Note:</STRONG> This can differ for non-Gregorian calendar. <br>
93 * To convert a calendar to a human readable form and vice versa, use
94 * the <code>java.text.DateFormat</code> class. <br>
96 * Other useful things you can do with an calendar, is
97 * <code>roll</code>ing fields (that means increase/decrease a
98 * specific field by one, propagating overflows), or
99 * <code>add</code>ing/substracting a fixed amount to a field.
101 * @see Date
102 * @see GregorianCalendar
103 * @see TimeZone
104 * @see java.text.DateFormat
106 public abstract class Calendar implements Serializable, Cloneable
109 * Constant representing the era time field.
111 public static final int ERA = 0;
114 * Constant representing the year time field.
116 public static final int YEAR = 1;
119 * Constant representing the month time field. This field
120 * should contain one of the JANUARY,...,DECEMBER constants below.
122 public static final int MONTH = 2;
125 * Constant representing the week of the year field.
126 * @see #setFirstDayOfWeek(int)
128 public static final int WEEK_OF_YEAR = 3;
131 * Constant representing the week of the month time field.
132 * @see #setFirstDayOfWeek(int)
134 public static final int WEEK_OF_MONTH = 4;
137 * Constant representing the day time field, synonym for DAY_OF_MONTH.
139 public static final int DATE = 5;
142 * Constant representing the day time field.
144 public static final int DAY_OF_MONTH = 5;
147 * Constant representing the day of year time field. This is
148 * 1 for the first day in month.
150 public static final int DAY_OF_YEAR = 6;
153 * Constant representing the day of week time field. This field
154 * should contain one of the SUNDAY,...,SATURDAY constants below.
156 public static final int DAY_OF_WEEK = 7;
159 * Constant representing the day-of-week-in-month field. For
160 * instance this field contains 2 for the second thursday in a
161 * month. If you give a negative number here, the day will count
162 * from the end of the month.
164 public static final int DAY_OF_WEEK_IN_MONTH = 8;
167 * Constant representing the part of the day for 12-hour clock. This
168 * should be one of AM or PM.
170 public static final int AM_PM = 9;
173 * Constant representing the hour time field for 12-hour clock.
175 public static final int HOUR = 10;
178 * Constant representing the hour of day time field for 24-hour clock.
180 public static final int HOUR_OF_DAY = 11;
183 * Constant representing the minute of hour time field.
185 public static final int MINUTE = 12;
188 * Constant representing the second time field.
190 public static final int SECOND = 13;
193 * Constant representing the millisecond time field.
195 public static final int MILLISECOND = 14;
198 * Constant representing the time zone offset time field for the
199 * time given in the other fields. It is measured in
200 * milliseconds. The default is the offset of the time zone.
202 public static final int ZONE_OFFSET = 15;
205 * Constant representing the daylight saving time offset in
206 * milliseconds. The default is the value given by the time zone.
208 public static final int DST_OFFSET = 16;
211 * Number of time fields.
213 public static final int FIELD_COUNT = 17;
216 * Constant representing Sunday.
218 public static final int SUNDAY = 1;
221 * Constant representing Monday.
223 public static final int MONDAY = 2;
226 * Constant representing Tuesday.
228 public static final int TUESDAY = 3;
231 * Constant representing Wednesday.
233 public static final int WEDNESDAY = 4;
236 * Constant representing Thursday.
238 public static final int THURSDAY = 5;
241 * Constant representing Friday.
243 public static final int FRIDAY = 6;
246 * Constant representing Saturday.
248 public static final int SATURDAY = 7;
251 * Constant representing January.
253 public static final int JANUARY = 0;
256 * Constant representing February.
258 public static final int FEBRUARY = 1;
261 * Constant representing March.
263 public static final int MARCH = 2;
266 * Constant representing April.
268 public static final int APRIL = 3;
271 * Constant representing May.
273 public static final int MAY = 4;
276 * Constant representing June.
278 public static final int JUNE = 5;
281 * Constant representing July.
283 public static final int JULY = 6;
286 * Constant representing August.
288 public static final int AUGUST = 7;
291 * Constant representing September.
293 public static final int SEPTEMBER = 8;
296 * Constant representing October.
298 public static final int OCTOBER = 9;
301 * Constant representing November.
303 public static final int NOVEMBER = 10;
306 * Constant representing December.
308 public static final int DECEMBER = 11;
311 * Constant representing Undecimber. This is an artificial name useful
312 * for lunar calendars.
314 public static final int UNDECIMBER = 12;
317 * Useful constant for 12-hour clock.
319 public static final int AM = 0;
322 * Useful constant for 12-hour clock.
324 public static final int PM = 1;
327 * The time fields. The array is indexed by the constants YEAR to
328 * DST_OFFSET.
329 * @serial
331 protected int[] fields = new int[FIELD_COUNT];
334 * The flags which tell if the fields above have a value.
335 * @serial
337 protected boolean[] isSet = new boolean[FIELD_COUNT];
340 * The time in milliseconds since the epoch.
341 * @serial
343 protected long time;
346 * Tells if the above field has a valid value.
347 * @serial
349 protected boolean isTimeSet;
352 * Tells if the fields have a valid value. This superseeds the isSet
353 * array.
354 * @serial
356 protected boolean areFieldsSet;
359 * The time zone of this calendar. Used by sub classes to do UTC / local
360 * time conversion. Sub classes can access this field with getTimeZone().
361 * @serial
363 private TimeZone zone;
366 * Specifies if the date/time interpretation should be lenient.
367 * If the flag is set, a date such as "February 30, 1996" will be
368 * treated as the 29th day after the February 1. If this flag
369 * is false, such dates will cause an exception.
370 * @serial
372 private boolean lenient;
375 * Sets what the first day of week is. This is used for
376 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
377 * @serial
379 private int firstDayOfWeek;
382 * Sets how many days are required in the first week of the year.
383 * If the first day of the year should be the first week you should
384 * set this value to 1. If the first week must be a full week, set
385 * it to 7.
386 * @serial
388 private int minimalDaysInFirstWeek;
391 * Is set to true if DST_OFFSET is explicitly set. In that case
392 * it's value overrides the value computed from the current
393 * time and the timezone.
395 private boolean explicitDSTOffset = false;
398 * The version of the serialized data on the stream.
399 * <dl><dt>0 or not present</dt>
400 * <dd> JDK 1.1.5 or later.</dd>
401 * <dt>1</dt>
402 * <dd>JDK 1.1.6 or later. This always writes a correct `time' value
403 * on the stream, as well as the other fields, to be compatible with
404 * earlier versions</dd></dl>
405 * @since JDK1.1.6
406 * @serial
408 private int serialVersionOnStream = 1;
411 * XXX - I have not checked the compatibility. The documentation of
412 * the serialized-form is quite hairy...
414 static final long serialVersionUID = -1807547505821590642L;
417 * The name of the resource bundle. Used only by getBundle()
419 private static final String bundleName = "gnu.java.locale.Calendar";
422 * get resource bundle:
423 * The resources should be loaded via this method only. Iff an application
424 * uses this method, the resourcebundle is required.
426 private static ResourceBundle getBundle(Locale locale)
428 return ResourceBundle.getBundle(bundleName, locale,
429 ClassLoader.getSystemClassLoader());
433 * Constructs a new Calendar with the default time zone and the default
434 * locale.
436 protected Calendar()
438 this(TimeZone.getDefault(), Locale.getDefault());
442 * Constructs a new Calendar with the given time zone and the given
443 * locale.
444 * @param zone a time zone.
445 * @param locale a locale.
447 protected Calendar(TimeZone zone, Locale locale)
449 this.zone = zone;
450 lenient = true;
452 ResourceBundle rb = getBundle(locale);
454 firstDayOfWeek = ((Integer) rb.getObject("firstDayOfWeek")).intValue();
455 minimalDaysInFirstWeek = ((Integer) rb.getObject("minimalDaysInFirstWeek"))
456 .intValue();
457 clear();
461 * Creates a calendar representing the actual time, using the default
462 * time zone and locale.
464 public static synchronized Calendar getInstance()
466 return getInstance(TimeZone.getDefault(), Locale.getDefault());
470 * Creates a calendar representing the actual time, using the given
471 * time zone and the default locale.
472 * @param zone a time zone.
474 public static synchronized Calendar getInstance(TimeZone zone)
476 return getInstance(zone, Locale.getDefault());
480 * Creates a calendar representing the actual time, using the default
481 * time zone and the given locale.
482 * @param locale a locale.
484 public static synchronized Calendar getInstance(Locale locale)
486 return getInstance(TimeZone.getDefault(), locale);
490 * Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle
491 * lookup for every getInstance call.
493 private static HashMap cache = new HashMap();
495 /** Preset argument types for calendar-class constructor lookup. */
496 private static Class[] ctorArgTypes = new Class[]
498 TimeZone.class, Locale.class
502 * Creates a calendar representing the actual time, using the given
503 * time zone and locale.
504 * @param zone a time zone.
505 * @param locale a locale.
507 public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
509 Class calendarClass = (Class) cache.get(locale);
510 Throwable exception = null;
514 if (calendarClass == null)
516 ResourceBundle rb = getBundle(locale);
517 String calendarClassName = rb.getString("calendarClass");
519 if (calendarClassName != null)
521 calendarClass = Class.forName(calendarClassName);
522 if (Calendar.class.isAssignableFrom(calendarClass))
523 cache.put(locale, calendarClass);
527 // GregorianCalendar is by far the most common case. Optimize by
528 // avoiding reflection.
529 if (calendarClass == GregorianCalendar.class)
530 return new GregorianCalendar(zone, locale);
532 if (Calendar.class.isAssignableFrom(calendarClass))
534 Constructor ctor = calendarClass.getConstructor(ctorArgTypes);
535 return (Calendar) ctor.newInstance(new Object[] { zone, locale });
538 catch (ClassNotFoundException ex)
540 exception = ex;
542 catch (IllegalAccessException ex)
544 exception = ex;
546 catch (NoSuchMethodException ex)
548 exception = ex;
550 catch (InstantiationException ex)
552 exception = ex;
554 catch (InvocationTargetException ex)
556 exception = ex;
559 throw new RuntimeException("Error instantiating calendar for locale "
560 + locale, exception);
564 * Gets the set of locales for which a Calendar is available.
565 * @exception MissingResourceException if locale data couldn't be found.
566 * @return the set of locales.
568 public static synchronized Locale[] getAvailableLocales()
570 ResourceBundle rb = getBundle(new Locale("", ""));
571 return (Locale[]) rb.getObject("availableLocales");
575 * Converts the time field values (<code>fields</code>) to
576 * milliseconds since the epoch UTC (<code>time</code>). Override
577 * this method if you write your own Calendar. */
578 protected abstract void computeTime();
581 * Converts the milliseconds since the epoch UTC
582 * (<code>time</code>) to time fields
583 * (<code>fields</code>). Override this method if you write your
584 * own Calendar.
586 protected abstract void computeFields();
589 * Converts the time represented by this object to a
590 * <code>Date</code>-Object.
591 * @return the Date.
593 public final Date getTime()
595 if (! isTimeSet)
596 computeTime();
597 return new Date(time);
601 * Sets this Calendar's time to the given Date. All time fields
602 * are invalidated by this method.
604 public final void setTime(Date date)
606 setTimeInMillis(date.getTime());
610 * Returns the time represented by this Calendar.
611 * @return the time in milliseconds since the epoch.
612 * @specnote This was made public in 1.4.
614 public long getTimeInMillis()
616 if (! isTimeSet)
617 computeTime();
618 return time;
622 * Sets this Calendar's time to the given Time. All time fields
623 * are invalidated by this method.
624 * @param time the time in milliseconds since the epoch
625 * @specnote This was made public in 1.4.
627 public void setTimeInMillis(long time)
629 clear();
630 this.time = time;
631 isTimeSet = true;
635 * Gets the value of the specified field. They are recomputed
636 * if they are invalid.
637 * @param field the time field. One of the time field constants.
638 * @return the value of the specified field
639 * @throws ArrayIndexOutOfBoundsException if the field is outside
640 * the valid range. The value of field must be >= 0 and
641 * <= <code>FIELD_COUNT</code>.
642 * @specnote Not final since JDK 1.4
644 public int get(int field)
646 // If the requested field is invalid, force all fields to be recomputed.
647 if (! isSet[field])
648 areFieldsSet = false;
649 complete();
650 return fields[field];
654 * Gets the value of the specified field. This method doesn't
655 * recompute the fields, if they are invalid.
656 * @param field the time field. One of the time field constants.
657 * @return the value of the specified field, undefined if
658 * <code>areFieldsSet</code> or <code>isSet[field]</code> is false.
659 * @throws ArrayIndexOutOfBoundsException if the field is outside
660 * the valid range. The value of field must be >= 0 and
661 * <= <code>FIELD_COUNT</code>.
663 protected final int internalGet(int field)
665 return fields[field];
669 * Sets the time field with the given value. This does invalidate
670 * the time in milliseconds.
671 * @param field the time field. One of the time field constants
672 * @param value the value to be set.
673 * @throws ArrayIndexOutOfBoundsException if field is outside
674 * the valid range. The value of field must be >= 0 and
675 * <= <code>FIELD_COUNT</code>.
676 * @specnote Not final since JDK 1.4
678 public void set(int field, int value)
680 if (isTimeSet)
681 for (int i = 0; i < FIELD_COUNT; i++)
682 isSet[i] = false;
683 isTimeSet = false;
684 fields[field] = value;
685 isSet[field] = true;
687 // The five valid date patterns, in order of priority
688 // 1 YEAR + MONTH + DAY_OF_MONTH
689 // 2 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
690 // 3 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
691 // 4 YEAR + DAY_OF_YEAR
692 // 5 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
693 switch (field)
695 case MONTH: // pattern 1,2 or 3
696 isSet[DAY_OF_YEAR] = false;
697 isSet[WEEK_OF_YEAR] = false;
698 break;
699 case DAY_OF_MONTH: // pattern 1
700 isSet[YEAR] = true;
701 isSet[MONTH] = true;
702 isSet[WEEK_OF_MONTH] = true;
703 isSet[DAY_OF_WEEK] = false;
704 isSet[DAY_OF_WEEK_IN_MONTH] = false;
705 isSet[DAY_OF_YEAR] = false;
706 isSet[WEEK_OF_YEAR] = false;
707 break;
708 case WEEK_OF_MONTH: // pattern 2
709 isSet[YEAR] = true;
710 isSet[MONTH] = true;
711 isSet[DAY_OF_WEEK] = true;
712 isSet[DAY_OF_MONTH] = false;
713 isSet[DAY_OF_WEEK_IN_MONTH] = false;
714 isSet[DAY_OF_YEAR] = false;
715 isSet[WEEK_OF_YEAR] = false;
716 break;
717 case DAY_OF_WEEK_IN_MONTH: // pattern 3
718 isSet[YEAR] = true;
719 isSet[MONTH] = true;
720 isSet[DAY_OF_WEEK] = true;
721 isSet[DAY_OF_YEAR] = false;
722 isSet[DAY_OF_MONTH] = false;
723 isSet[WEEK_OF_MONTH] = false;
724 isSet[WEEK_OF_YEAR] = false;
725 break;
726 case DAY_OF_YEAR: // pattern 4
727 isSet[YEAR] = true;
728 isSet[MONTH] = false;
729 isSet[WEEK_OF_MONTH] = false;
730 isSet[DAY_OF_MONTH] = false;
731 isSet[DAY_OF_WEEK] = false;
732 isSet[WEEK_OF_YEAR] = false;
733 isSet[DAY_OF_WEEK_IN_MONTH] = false;
734 break;
735 case WEEK_OF_YEAR: // pattern 5
736 isSet[YEAR] = true;
737 isSet[DAY_OF_WEEK] = true;
738 isSet[MONTH] = false;
739 isSet[DAY_OF_MONTH] = false;
740 isSet[WEEK_OF_MONTH] = false;
741 isSet[DAY_OF_YEAR] = false;
742 isSet[DAY_OF_WEEK_IN_MONTH] = false;
743 break;
744 case AM_PM:
745 isSet[HOUR] = true;
746 isSet[HOUR_OF_DAY] = false;
747 break;
748 case HOUR_OF_DAY:
749 isSet[AM_PM] = false;
750 isSet[HOUR] = false;
751 break;
752 case HOUR:
753 isSet[AM_PM] = true;
754 isSet[HOUR_OF_DAY] = false;
755 break;
756 case DST_OFFSET:
757 explicitDSTOffset = true;
760 // May have crossed over a DST boundary.
761 if (! explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET))
762 isSet[DST_OFFSET] = false;
766 * Sets the fields for year, month, and date
767 * @param year the year.
768 * @param month the month, one of the constants JANUARY..UNDICEMBER.
769 * @param date the day of the month
771 public final void set(int year, int month, int date)
773 isTimeSet = false;
774 fields[YEAR] = year;
775 fields[MONTH] = month;
776 fields[DATE] = date;
777 isSet[YEAR] = isSet[MONTH] = isSet[DATE] = true;
778 isSet[WEEK_OF_YEAR] = false;
779 isSet[DAY_OF_YEAR] = false;
780 isSet[WEEK_OF_MONTH] = false;
781 isSet[DAY_OF_WEEK] = false;
782 isSet[DAY_OF_WEEK_IN_MONTH] = false;
783 isSet[ERA] = false;
785 if (! explicitDSTOffset)
786 isSet[DST_OFFSET] = false; // May have crossed a DST boundary.
790 * Sets the fields for year, month, date, hour, and minute
791 * @param year the year.
792 * @param month the month, one of the constants JANUARY..UNDICEMBER.
793 * @param date the day of the month
794 * @param hour the hour of day.
795 * @param minute the minute.
797 public final void set(int year, int month, int date, int hour, int minute)
799 set(year, month, date);
800 fields[HOUR_OF_DAY] = hour;
801 fields[MINUTE] = minute;
802 isSet[HOUR_OF_DAY] = isSet[MINUTE] = true;
803 isSet[AM_PM] = false;
804 isSet[HOUR] = false;
808 * Sets the fields for year, month, date, hour, and minute
809 * @param year the year.
810 * @param month the month, one of the constants JANUARY..UNDICEMBER.
811 * @param date the day of the month
812 * @param hour the hour of day.
813 * @param minute the minute.
814 * @param second the second.
816 public final void set(int year, int month, int date, int hour, int minute,
817 int second)
819 set(year, month, date, hour, minute);
820 fields[SECOND] = second;
821 isSet[SECOND] = true;
825 * Clears the values of all the time fields.
827 public final void clear()
829 isTimeSet = false;
830 areFieldsSet = false;
831 int zoneOffs = zone.getRawOffset();
832 int[] tempFields =
834 1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0,
835 0, 0, zoneOffs, 0
837 fields = tempFields;
838 for (int i = 0; i < FIELD_COUNT; i++)
839 isSet[i] = false;
843 * Clears the values of the specified time field.
844 * @param field the time field. One of the time field constants.
845 * @throws ArrayIndexOutOfBoundsException if field is outside
846 * the valid range. The value of field must be >= 0 and
847 * <= <code>FIELD_COUNT</code>.
849 public final void clear(int field)
851 int[] tempFields =
853 1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0,
854 0, 0, zone.getRawOffset(), 0
856 isTimeSet = false;
857 areFieldsSet = false;
858 isSet[field] = false;
859 fields[field] = tempFields[field];
863 * Determines if the specified field has a valid value.
864 * @return true if the specified field has a value.
865 * @throws ArrayIndexOutOfBoundsException if the field is outside
866 * the valid range. The value of field must be >= 0 and
867 * <= <code>FIELD_COUNT</code>.
869 public final boolean isSet(int field)
871 return isSet[field];
875 * Fills any unset fields in the time field list
876 * @return true if the specified field has a value.
878 protected void complete()
880 if (! isTimeSet)
881 computeTime();
882 if (! areFieldsSet)
883 computeFields();
887 * Compares the given calendar with this.
888 * @param o the object to that we should compare.
889 * @return true, if the given object is a calendar, that represents
890 * the same time (but doesn't necessary have the same fields).
892 public boolean equals(Object o)
894 return (o instanceof Calendar)
895 && getTimeInMillis() == ((Calendar) o).getTimeInMillis();
899 * Returns a hash code for this calendar.
900 * @return a hash code, which fullfits the general contract of
901 * <code>hashCode()</code>
903 public int hashCode()
905 long time = getTimeInMillis();
906 return (int) ((time & 0xffffffffL) ^ (time >> 32));
910 * Compares the given calendar with this.
911 * @param o the object to that we should compare.
912 * @return true, if the given object is a calendar, and this calendar
913 * represents a smaller time than the calendar o.
914 * @exception ClassCastException if o is not an calendar.
915 * @since JDK1.2 you don't need to override this method
917 public boolean before(Object o)
919 return getTimeInMillis() < ((Calendar) o).getTimeInMillis();
923 * Compares the given calendar with this.
924 * @param o the object to that we should compare.
925 * @return true, if the given object is a calendar, and this calendar
926 * represents a bigger time than the calendar o.
927 * @exception ClassCastException if o is not an calendar.
928 * @since JDK1.2 you don't need to override this method
930 public boolean after(Object o)
932 return getTimeInMillis() > ((Calendar) o).getTimeInMillis();
936 * Adds the specified amount of time to the given time field. The
937 * amount may be negative to subtract the time. If the field overflows
938 * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
939 * @param field the time field. One of the time field constants.
940 * @param amount the amount of time.
941 * @throws ArrayIndexOutOfBoundsException if the field is outside
942 * the valid range. The value of field must be >= 0 and
943 * <= <code>FIELD_COUNT</code>.
945 public abstract void add(int field, int amount);
948 * Rolls the specified time field up or down. This means add one
949 * to the specified field, but don't change the other fields. If
950 * the maximum for this field is reached, start over with the
951 * minimum value. <br>
953 * <strong>Note:</strong> There may be situation, where the other
954 * fields must be changed, e.g rolling the month on May, 31.
955 * The date June, 31 is automatically converted to July, 1.
956 * @param field the time field. One of the time field constants.
957 * @param up the direction, true for up, false for down.
958 * @throws ArrayIndexOutOfBoundsException if the field is outside
959 * the valid range. The value of field must be >= 0 and
960 * <= <code>FIELD_COUNT</code>.
962 public abstract void roll(int field, boolean up);
965 * Rolls up or down the specified time field by the given amount.
966 * A negative amount rolls down. The default implementation is
967 * call <code>roll(int, boolean)</code> for the specified amount.
969 * Subclasses should override this method to do more intuitiv things.
971 * @param field the time field. One of the time field constants.
972 * @param amount the amount to roll by, positive for rolling up,
973 * negative for rolling down.
974 * @throws ArrayIndexOutOfBoundsException if the field is outside
975 * the valid range. The value of field must be >= 0 and
976 * <= <code>FIELD_COUNT</code>.
977 * @since JDK1.2
979 public void roll(int field, int amount)
981 while (amount > 0)
983 roll(field, true);
984 amount--;
986 while (amount < 0)
988 roll(field, false);
989 amount++;
994 * Sets the time zone to the specified value.
995 * @param zone the new time zone
997 public void setTimeZone(TimeZone zone)
999 this.zone = zone;
1003 * Gets the time zone of this calendar
1004 * @return the current time zone.
1006 public TimeZone getTimeZone()
1008 return zone;
1012 * Specifies if the date/time interpretation should be lenient.
1013 * If the flag is set, a date such as "February 30, 1996" will be
1014 * treated as the 29th day after the February 1. If this flag
1015 * is false, such dates will cause an exception.
1016 * @param lenient true, if the date should be interpreted linient,
1017 * false if it should be interpreted strict.
1019 public void setLenient(boolean lenient)
1021 this.lenient = lenient;
1025 * Tells if the date/time interpretation is lenient.
1026 * @return true, if the date should be interpreted linient,
1027 * false if it should be interpreted strict.
1029 public boolean isLenient()
1031 return lenient;
1035 * Sets what the first day of week is. This is used for
1036 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1037 * @param value the first day of week. One of SUNDAY to SATURDAY.
1039 public void setFirstDayOfWeek(int value)
1041 firstDayOfWeek = value;
1045 * Gets what the first day of week is. This is used for
1046 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1047 * @return the first day of week. One of SUNDAY to SATURDAY.
1049 public int getFirstDayOfWeek()
1051 return firstDayOfWeek;
1055 * Sets how many days are required in the first week of the year.
1056 * If the first day of the year should be the first week you should
1057 * set this value to 1. If the first week must be a full week, set
1058 * it to 7.
1059 * @param value the minimal days required in the first week.
1061 public void setMinimalDaysInFirstWeek(int value)
1063 minimalDaysInFirstWeek = value;
1067 * Gets how many days are required in the first week of the year.
1068 * @return the minimal days required in the first week.
1069 * @see #setMinimalDaysInFirstWeek
1071 public int getMinimalDaysInFirstWeek()
1073 return minimalDaysInFirstWeek;
1077 * Gets the smallest value that is allowed for the specified field.
1078 * @param field the time field. One of the time field constants.
1079 * @return the smallest value.
1081 public abstract int getMinimum(int field);
1084 * Gets the biggest value that is allowed for the specified field.
1085 * @param field the time field. One of the time field constants.
1086 * @return the biggest value.
1088 public abstract int getMaximum(int field);
1091 * Gets the greatest minimum value that is allowed for the specified field.
1092 * @param field the time field. One of the time field constants.
1093 * @return the greatest minimum value.
1095 public abstract int getGreatestMinimum(int field);
1098 * Gets the smallest maximum value that is allowed for the
1099 * specified field. For example this is 28 for DAY_OF_MONTH.
1100 * @param field the time field. One of the time field constants.
1101 * @return the least maximum value.
1103 public abstract int getLeastMaximum(int field);
1106 * Gets the actual minimum value that is allowed for the specified field.
1107 * This value is dependent on the values of the other fields.
1108 * @param field the time field. One of the time field constants.
1109 * @return the actual minimum value.
1110 * @throws ArrayIndexOutOfBoundsException if the field is outside
1111 * the valid range. The value of field must be >= 0 and
1112 * <= <code>FIELD_COUNT</code>.
1113 * @since jdk1.2
1115 public int getActualMinimum(int field)
1117 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1118 int min = tmp.getGreatestMinimum(field);
1119 int end = tmp.getMinimum(field);
1120 tmp.set(field, min);
1121 for (; min > end; min--)
1123 tmp.add(field, -1); // Try to get smaller
1124 if (tmp.get(field) != min - 1)
1125 break; // Done if not successful
1127 return min;
1131 * Gets the actual maximum value that is allowed for the specified field.
1132 * This value is dependent on the values of the other fields.
1133 * @param field the time field. One of the time field constants.
1134 * @return the actual maximum value.
1135 * @throws ArrayIndexOutOfBoundsException if the field is outside
1136 * the valid range. The value of field must be >= 0 and
1137 * <= <code>FIELD_COUNT</code>.
1138 * @since jdk1.2
1140 public int getActualMaximum(int field)
1142 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1143 int max = tmp.getLeastMaximum(field);
1144 int end = tmp.getMaximum(field);
1145 tmp.set(field, max);
1146 for (; max < end; max++)
1148 tmp.add(field, 1);
1149 if (tmp.get(field) != max + 1)
1150 break;
1152 return max;
1156 * Return a clone of this object.
1158 public Object clone()
1162 Calendar cal = (Calendar) super.clone();
1163 cal.fields = (int[]) fields.clone();
1164 cal.isSet = (boolean[]) isSet.clone();
1165 return cal;
1167 catch (CloneNotSupportedException ex)
1169 return null;
1173 private static final String[] fieldNames =
1175 ",ERA=", ",YEAR=", ",MONTH=",
1176 ",WEEK_OF_YEAR=",
1177 ",WEEK_OF_MONTH=",
1178 ",DAY_OF_MONTH=",
1179 ",DAY_OF_YEAR=", ",DAY_OF_WEEK=",
1180 ",DAY_OF_WEEK_IN_MONTH=",
1181 ",AM_PM=", ",HOUR=",
1182 ",HOUR_OF_DAY=", ",MINUTE=",
1183 ",SECOND=", ",MILLISECOND=",
1184 ",ZONE_OFFSET=", ",DST_OFFSET="
1188 * Returns a string representation of this object. It is mainly
1189 * for debugging purposes and its content is implementation
1190 * specific.
1192 public String toString()
1194 StringBuffer sb = new StringBuffer();
1195 sb.append(getClass().getName()).append('[');
1196 sb.append("time=");
1197 if (isTimeSet)
1198 sb.append(time);
1199 else
1200 sb.append("?");
1201 sb.append(",zone=" + zone);
1202 sb.append(",areFieldsSet=" + areFieldsSet);
1203 for (int i = 0; i < FIELD_COUNT; i++)
1205 sb.append(fieldNames[i]);
1206 if (isSet[i])
1207 sb.append(fields[i]);
1208 else
1209 sb.append("?");
1211 sb.append(",lenient=").append(lenient);
1212 sb.append(",firstDayOfWeek=").append(firstDayOfWeek);
1213 sb.append(",minimalDaysInFirstWeek=").append(minimalDaysInFirstWeek);
1214 sb.append("]");
1215 return sb.toString();
1219 * Saves the state of the object to the stream. Ideally we would
1220 * only write the time field, but we need to be compatible with
1221 * earlier versions. <br>
1223 * This doesn't write the JDK1.1 field nextStamp to the stream, as
1224 * I don't know what it is good for, and because the documentation
1225 * says, that it could be omitted. */
1226 private void writeObject(ObjectOutputStream stream) throws IOException
1228 if (! isTimeSet)
1229 computeTime();
1230 stream.defaultWriteObject();
1234 * Reads the object back from stream (deserialization).
1236 private void readObject(ObjectInputStream stream)
1237 throws IOException, ClassNotFoundException
1239 stream.defaultReadObject();
1240 if (! isTimeSet)
1241 computeTime();
1243 if (serialVersionOnStream > 1)
1245 // This is my interpretation of the serial number:
1246 // Sun wants to remove all fields from the stream someday
1247 // and will then increase the serialVersion number again.
1248 // We prepare to be compatible.
1249 fields = new int[FIELD_COUNT];
1250 isSet = new boolean[FIELD_COUNT];
1251 areFieldsSet = false;