2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import gnu.java.lang.CPStringBuilder;
44 import java.io.IOException;
45 import java.io.ObjectInputStream;
46 import java.io.ObjectOutputStream;
47 import java.io.Serializable;
49 import java.lang.reflect.Constructor;
50 import java.lang.reflect.InvocationTargetException;
52 import java.text.DateFormatSymbols;
55 * This class is an abstract base class for Calendars, which can be
56 * used to convert between <code>Date</code> objects and a set of
57 * integer fields which represent <code>YEAR</code>,
58 * <code>MONTH</code>, <code>DAY</code>, etc. The <code>Date</code>
59 * object represents a time in milliseconds since the Epoch. <br>
61 * This class is locale sensitive. To get the Object matching the
62 * current locale you can use <code>getInstance</code>. You can even provide
63 * a locale or a timezone. <code>getInstance</code> returns currently
64 * a <code>GregorianCalendar</code> for the current date. <br>
66 * If you want to convert a date from the Year, Month, Day, DayOfWeek,
67 * etc. Representation to a <code>Date</code>-Object, you can create
68 * a new Calendar with <code>getInstance()</code>,
69 * <code>clear()</code> all fields, <code>set(int,int)</code> the
70 * fields you need and convert it with <code>getTime()</code>. <br>
72 * If you want to convert a <code>Date</code>-object to the Calendar
73 * representation, create a new Calendar, assign the
74 * <code>Date</code>-Object with <code>setTime()</code>, and read the
75 * fields with <code>get(int)</code>. <br>
77 * When computing the date from time fields, it may happen, that there
78 * are either two few fields set, or some fields are inconsistent. This
79 * cases will handled in a calendar specific way. Missing fields are
80 * replaced by the fields of the epoch: 1970 January 1 00:00. <br>
82 * To understand, how the day of year is computed out of the fields
83 * look at the following table. It is traversed from top to bottom,
84 * and for the first line all fields are set, that line is used to
85 * compute the day. <br>
88 <pre>month + day_of_month
89 month + week_of_month + day_of_week
90 month + day_of_week_of_month + day_of_week
92 day_of_week + week_of_year</pre>
94 * The hour_of_day-field takes precedence over the ampm and
95 * hour_of_ampm fields. <br>
97 * <STRONG>Note:</STRONG> This can differ for non-Gregorian calendar. <br>
99 * To convert a calendar to a human readable form and vice versa, use
100 * the <code>java.text.DateFormat</code> class. <br>
102 * Other useful things you can do with an calendar, is
103 * <code>roll</code>ing fields (that means increase/decrease a
104 * specific field by one, propagating overflows), or
105 * <code>add</code>ing/substracting a fixed amount to a field.
107 * @author Aaron M. Renn (arenn@urbanophile.com)
108 * @author Jochen Hoenicke (Jochen.Hoenicke@Informatik.Uni-Oldenburg.de)
109 * @author Warren Levy (warrenl@cygnus.com)
110 * @author Jeff Sturm (jsturm@one-point.com)
111 * @author Tom Tromey (tromey@redhat.com)
112 * @author Bryce McKinlay (mckinlay@redhat.com)
113 * @author Ingo Proetel (proetel@aicas.com)
114 * @author Jerry Quinn (jlquinn@optonline.net)
115 * @author Jeroen Frijters (jeroen@frijters.net)
116 * @author Noa Resare (noa@resare.com)
117 * @author Sven de Marothy (sven@physto.se)
118 * @author David Gilbert (david.gilbert@object-refinery.com)
119 * @author Olivier Jolly (olivier.jolly@pcedev.com)
120 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
122 * @see GregorianCalendar
124 * @see java.text.DateFormat
126 public abstract class Calendar
127 implements Serializable, Cloneable, Comparable<Calendar>
130 * Constant representing the era time field.
132 public static final int ERA = 0;
135 * Constant representing the year time field.
137 public static final int YEAR = 1;
140 * Constant representing the month time field. This field
141 * should contain one of the JANUARY,...,DECEMBER constants below.
143 public static final int MONTH = 2;
146 * Constant representing the week of the year field.
147 * @see #setFirstDayOfWeek(int)
149 public static final int WEEK_OF_YEAR = 3;
152 * Constant representing the week of the month time field.
153 * @see #setFirstDayOfWeek(int)
155 public static final int WEEK_OF_MONTH = 4;
158 * Constant representing the day time field, synonym for DAY_OF_MONTH.
160 public static final int DATE = 5;
163 * Constant representing the day time field.
165 public static final int DAY_OF_MONTH = 5;
168 * Constant representing the day of year time field. This is
169 * 1 for the first day in month.
171 public static final int DAY_OF_YEAR = 6;
174 * Constant representing the day of week time field. This field
175 * should contain one of the SUNDAY,...,SATURDAY constants below.
177 public static final int DAY_OF_WEEK = 7;
180 * Constant representing the day-of-week-in-month field. For
181 * instance this field contains 2 for the second thursday in a
182 * month. If you give a negative number here, the day will count
183 * from the end of the month.
185 public static final int DAY_OF_WEEK_IN_MONTH = 8;
188 * Constant representing the part of the day for 12-hour clock. This
189 * should be one of AM or PM.
191 public static final int AM_PM = 9;
194 * Constant representing the hour time field for 12-hour clock.
196 public static final int HOUR = 10;
199 * Constant representing the hour of day time field for 24-hour clock.
201 public static final int HOUR_OF_DAY = 11;
204 * Constant representing the minute of hour time field.
206 public static final int MINUTE = 12;
209 * Constant representing the second time field.
211 public static final int SECOND = 13;
214 * Constant representing the millisecond time field.
216 public static final int MILLISECOND = 14;
219 * Constant representing the time zone offset time field for the
220 * time given in the other fields. It is measured in
221 * milliseconds. The default is the offset of the time zone.
223 public static final int ZONE_OFFSET = 15;
226 * Constant representing the daylight saving time offset in
227 * milliseconds. The default is the value given by the time zone.
229 public static final int DST_OFFSET = 16;
232 * Number of time fields.
234 public static final int FIELD_COUNT = 17;
237 * Constant representing Sunday.
239 public static final int SUNDAY = 1;
242 * Constant representing Monday.
244 public static final int MONDAY = 2;
247 * Constant representing Tuesday.
249 public static final int TUESDAY = 3;
252 * Constant representing Wednesday.
254 public static final int WEDNESDAY = 4;
257 * Constant representing Thursday.
259 public static final int THURSDAY = 5;
262 * Constant representing Friday.
264 public static final int FRIDAY = 6;
267 * Constant representing Saturday.
269 public static final int SATURDAY = 7;
272 * Constant representing January.
274 public static final int JANUARY = 0;
277 * Constant representing February.
279 public static final int FEBRUARY = 1;
282 * Constant representing March.
284 public static final int MARCH = 2;
287 * Constant representing April.
289 public static final int APRIL = 3;
292 * Constant representing May.
294 public static final int MAY = 4;
297 * Constant representing June.
299 public static final int JUNE = 5;
302 * Constant representing July.
304 public static final int JULY = 6;
307 * Constant representing August.
309 public static final int AUGUST = 7;
312 * Constant representing September.
314 public static final int SEPTEMBER = 8;
317 * Constant representing October.
319 public static final int OCTOBER = 9;
322 * Constant representing November.
324 public static final int NOVEMBER = 10;
327 * Constant representing December.
329 public static final int DECEMBER = 11;
332 * Constant representing Undecimber. This is an artificial name useful
333 * for lunar calendars.
335 public static final int UNDECIMBER = 12;
338 * Useful constant for 12-hour clock.
340 public static final int AM = 0;
343 * Useful constant for 12-hour clock.
345 public static final int PM = 1;
348 * A style specifier for {@link #getDisplayNames(int,int,Locale)}
349 * stating that names should be returned in both long and short variants.
355 public static final int ALL_STYLES = 0;
358 * A style specifier for {@link #getDisplayName(int,int,Locale)}
359 * and {@link #getDisplayNames(int,int,Locale)} stating that names
360 * should be returned in their short variant if applicable.
364 public static final int SHORT = 1;
367 * A style specifier for {@link #getDisplayName(int,int,Locale)}
368 * and {@link #getDisplayNames(int,int,Locale)} stating that names
369 * should be returned in their long variant if applicable.
373 public static final int LONG = 2;
376 * The time fields. The array is indexed by the constants YEAR to
380 protected int[] fields = new int[FIELD_COUNT];
383 * The flags which tell if the fields above have a value.
386 protected boolean[] isSet = new boolean[FIELD_COUNT];
389 * The time in milliseconds since the epoch.
395 * Tells if the above field has a valid value.
398 protected boolean isTimeSet;
401 * Tells if the fields have a valid value. This superseeds the isSet
405 protected boolean areFieldsSet;
408 * The time zone of this calendar. Used by sub classes to do UTC / local
409 * time conversion. Sub classes can access this field with getTimeZone().
412 private TimeZone zone;
415 * This is the default calendar class, that is returned on
416 * java.util.Calendar.getInstance().
417 * XXX - this isn't localized anywhere, is it?
418 * @see java.util.Calendar#getInstance()
420 private static final String calendarClassName = "java.util.GregorianCalendar";
423 * Specifies if the date/time interpretation should be lenient.
424 * If the flag is set, a date such as "February 30, 1996" will be
425 * treated as the 29th day after the February 1. If this flag
426 * is false, such dates will cause an exception.
429 private boolean lenient;
432 * Sets what the first day of week is. This is used for
433 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
436 private int firstDayOfWeek;
439 * Sets how many days are required in the first week of the year.
440 * If the first day of the year should be the first week you should
441 * set this value to 1. If the first week must be a full week, set
445 private int minimalDaysInFirstWeek;
448 * Is set to true if DST_OFFSET is explicitly set. In that case
449 * it's value overrides the value computed from the current
450 * time and the timezone.
452 private boolean explicitDSTOffset = false;
455 * The version of the serialized data on the stream.
456 * <dl><dt>0 or not present</dt>
457 * <dd> JDK 1.1.5 or later.</dd>
459 * <dd>JDK 1.1.6 or later. This always writes a correct `time' value
460 * on the stream, as well as the other fields, to be compatible with
461 * earlier versions</dd></dl>
465 private int serialVersionOnStream = 1;
468 * XXX - I have not checked the compatibility. The documentation of
469 * the serialized-form is quite hairy...
471 static final long serialVersionUID = -1807547505821590642L;
474 * The name of the resource bundle. Used only by getBundle()
476 private static final String bundleName = "gnu.java.locale.LocaleInformation";
479 * get resource bundle:
480 * The resources should be loaded via this method only. Iff an application
481 * uses this method, the resourcebundle is required.
483 private static ResourceBundle getBundle(Locale locale)
485 return ResourceBundle.getBundle(bundleName, locale,
486 ClassLoader.getSystemClassLoader());
490 * The set of properties for obtaining the minimum number of days in
493 private static transient final Properties properties;
496 * Reads in the properties.
500 properties = new Properties();
503 properties.load(Calendar.class.getResourceAsStream("weeks.properties"));
505 catch (IOException exception)
507 System.out.println("Failed to load weeks resource: " + exception);
512 * Constructs a new Calendar with the default time zone and the default
517 this(TimeZone.getDefault(), Locale.getDefault());
521 * Constructs a new Calendar with the given time zone and the given
523 * @param zone a time zone.
524 * @param locale a locale.
526 protected Calendar(TimeZone zone, Locale locale)
530 String[] days = { "", "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
532 String country = locale.getCountry();
533 String min = properties.getProperty("minDays." + country);
535 min = properties.getProperty("minDays.DEFAULT");
536 String first = properties.getProperty("firstDay." + country);
538 first = properties.getProperty("firstDay.DEFAULT");
542 minimalDaysInFirstWeek = Integer.parseInt(min);
544 catch (NumberFormatException ex)
546 minimalDaysInFirstWeek = 1;
551 for (int i = 0; i < 8; i++)
552 if (days[i].equals(first))
559 * Creates a calendar representing the actual time, using the default
560 * time zone and locale.
562 * @return The new calendar.
564 public static synchronized Calendar getInstance()
566 return getInstance(TimeZone.getDefault(), Locale.getDefault());
570 * Creates a calendar representing the actual time, using the given
571 * time zone and the default locale.
573 * @param zone a time zone (<code>null</code> not permitted).
575 * @return The new calendar.
577 * @throws NullPointerException if <code>zone</code> is <code>null</code>.
579 public static synchronized Calendar getInstance(TimeZone zone)
581 return getInstance(zone, Locale.getDefault());
585 * Creates a calendar representing the actual time, using the default
586 * time zone and the given locale.
588 * @param locale a locale (<code>null</code> not permitted).
590 * @return The new calendar.
592 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
594 public static synchronized Calendar getInstance(Locale locale)
596 return getInstance(TimeZone.getDefault(), locale);
600 * Cache of locale->calendar-class mappings. This avoids having to do a ResourceBundle
601 * lookup for every getInstance call.
603 private static final HashMap<Locale,Class> cache = new HashMap<Locale,Class>();
605 /** Preset argument types for calendar-class constructor lookup. */
606 private static Class[] ctorArgTypes = new Class[]
608 TimeZone.class, Locale.class
612 * Creates a calendar representing the actual time, using the given
613 * time zone and locale.
615 * @param zone a time zone (<code>null</code> not permitted).
616 * @param locale a locale (<code>null</code> not permitted).
618 * @return The new calendar.
620 * @throws NullPointerException if <code>zone</code> or <code>locale</code>
621 * is <code>null</code>.
623 public static synchronized Calendar getInstance(TimeZone zone, Locale locale)
625 Class calendarClass = cache.get(locale);
626 Throwable exception = null;
630 if (calendarClass == null)
632 calendarClass = Class.forName(calendarClassName);
633 if (Calendar.class.isAssignableFrom(calendarClass))
634 cache.put(locale, calendarClass);
637 // GregorianCalendar is by far the most common case. Optimize by
638 // avoiding reflection.
639 if (calendarClass == GregorianCalendar.class)
640 return new GregorianCalendar(zone, locale);
642 if (Calendar.class.isAssignableFrom(calendarClass))
644 Constructor ctor = calendarClass.getConstructor(ctorArgTypes);
645 return (Calendar) ctor.newInstance(new Object[] { zone, locale });
648 catch (ClassNotFoundException ex)
652 catch (IllegalAccessException ex)
656 catch (NoSuchMethodException ex)
660 catch (InstantiationException ex)
664 catch (InvocationTargetException ex)
669 throw new RuntimeException("Error instantiating calendar for locale "
670 + locale, exception);
674 * Gets the set of locales for which a Calendar is available.
675 * @exception MissingResourceException if locale data couldn't be found.
676 * @return the set of locales.
678 public static synchronized Locale[] getAvailableLocales()
680 ResourceBundle rb = getBundle(new Locale("", ""));
681 return (Locale[]) rb.getObject("availableLocales");
685 * Converts the time field values (<code>fields</code>) to
686 * milliseconds since the epoch UTC (<code>time</code>). Override
687 * this method if you write your own Calendar. */
688 protected abstract void computeTime();
691 * Converts the milliseconds since the epoch UTC
692 * (<code>time</code>) to time fields
693 * (<code>fields</code>). Override this method if you write your
696 protected abstract void computeFields();
699 * Converts the time represented by this object to a
700 * <code>Date</code>-Object.
703 public final Date getTime()
707 return new Date(time);
711 * Sets this Calendar's time to the given Date. All time fields
712 * are invalidated by this method.
714 * @param date the date (<code>null</code> not permitted).
716 * @throws NullPointerException if <code>date</code> is <code>null</code>.
718 public final void setTime(Date date)
720 setTimeInMillis(date.getTime());
724 * Returns the time represented by this Calendar.
725 * @return the time in milliseconds since the epoch.
726 * @specnote This was made public in 1.4.
728 public long getTimeInMillis()
736 * Sets this Calendar's time to the given Time. All time fields
737 * are invalidated by this method.
738 * @param time the time in milliseconds since the epoch
739 * @specnote This was made public in 1.4.
741 public void setTimeInMillis(long time)
750 * Gets the value of the specified field. They are recomputed
751 * if they are invalid.
752 * @param field the time field. One of the time field constants.
753 * @return the value of the specified field
754 * @throws ArrayIndexOutOfBoundsException if the field is outside
755 * the valid range. The value of field must be >= 0 and
756 * <= <code>FIELD_COUNT</code>.
757 * @specnote Not final since JDK 1.4
759 public int get(int field)
761 // If the requested field is invalid, force all fields to be recomputed.
763 areFieldsSet = false;
765 return fields[field];
769 * Gets the value of the specified field. This method doesn't
770 * recompute the fields, if they are invalid.
771 * @param field the time field. One of the time field constants.
772 * @return the value of the specified field, undefined if
773 * <code>areFieldsSet</code> or <code>isSet[field]</code> is false.
774 * @throws ArrayIndexOutOfBoundsException if the field is outside
775 * the valid range. The value of field must be >= 0 and
776 * <= <code>FIELD_COUNT</code>.
778 protected final int internalGet(int field)
780 return fields[field];
784 * Sets the time field with the given value. This does invalidate
785 * the time in milliseconds.
786 * @param field the time field. One of the time field constants
787 * @param value the value to be set.
788 * @throws ArrayIndexOutOfBoundsException if field is outside
789 * the valid range. The value of field must be >= 0 and
790 * <= <code>FIELD_COUNT</code>.
791 * @specnote Not final since JDK 1.4
793 public void set(int field, int value)
796 for (int i = 0; i < FIELD_COUNT; i++)
799 fields[field] = value;
802 // The five valid date patterns, in order of priority
803 // 1 YEAR + MONTH + DAY_OF_MONTH
804 // 2 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
805 // 3 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
806 // 4 YEAR + DAY_OF_YEAR
807 // 5 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
810 case MONTH: // pattern 1,2 or 3
811 isSet[DAY_OF_YEAR] = false;
812 isSet[WEEK_OF_YEAR] = false;
814 case DAY_OF_MONTH: // pattern 1
817 isSet[WEEK_OF_MONTH] = true;
818 isSet[DAY_OF_WEEK] = false;
819 isSet[DAY_OF_WEEK_IN_MONTH] = false;
820 isSet[DAY_OF_YEAR] = false;
821 isSet[WEEK_OF_YEAR] = false;
823 case WEEK_OF_MONTH: // pattern 2
824 if (! isSet[DAY_OF_WEEK])
825 fields[DAY_OF_WEEK] = getFirstDayOfWeek();
828 isSet[DAY_OF_WEEK] = true;
829 isSet[DAY_OF_MONTH] = false;
830 isSet[DAY_OF_WEEK_IN_MONTH] = false;
831 isSet[DAY_OF_YEAR] = false;
832 isSet[WEEK_OF_YEAR] = false;
834 case DAY_OF_WEEK_IN_MONTH: // pattern 3
835 if (! isSet[DAY_OF_WEEK])
836 fields[DAY_OF_WEEK] = getFirstDayOfWeek();
839 isSet[DAY_OF_WEEK] = true;
840 isSet[DAY_OF_YEAR] = false;
841 isSet[DAY_OF_MONTH] = false;
842 isSet[WEEK_OF_MONTH] = false;
843 isSet[WEEK_OF_YEAR] = false;
845 case DAY_OF_YEAR: // pattern 4
847 isSet[MONTH] = false;
848 isSet[WEEK_OF_MONTH] = false;
849 isSet[DAY_OF_MONTH] = false;
850 isSet[DAY_OF_WEEK] = false;
851 isSet[WEEK_OF_YEAR] = false;
852 isSet[DAY_OF_WEEK_IN_MONTH] = false;
854 case WEEK_OF_YEAR: // pattern 5
855 if (! isSet[DAY_OF_WEEK])
856 fields[DAY_OF_WEEK] = getFirstDayOfWeek();
858 isSet[DAY_OF_WEEK] = true;
859 isSet[MONTH] = false;
860 isSet[DAY_OF_MONTH] = false;
861 isSet[WEEK_OF_MONTH] = false;
862 isSet[DAY_OF_YEAR] = false;
863 isSet[DAY_OF_WEEK_IN_MONTH] = false;
867 isSet[HOUR_OF_DAY] = false;
870 isSet[AM_PM] = false;
875 isSet[HOUR_OF_DAY] = false;
878 explicitDSTOffset = true;
881 // May have crossed over a DST boundary.
882 if (! explicitDSTOffset && (field != DST_OFFSET && field != ZONE_OFFSET))
883 isSet[DST_OFFSET] = false;
887 * Sets the fields for year, month, and date
888 * @param year the year.
889 * @param month the month, one of the constants JANUARY..UNDICEMBER.
890 * @param date the day of the month
892 public final void set(int year, int month, int date)
896 fields[MONTH] = month;
898 isSet[YEAR] = isSet[MONTH] = isSet[DATE] = true;
899 isSet[WEEK_OF_YEAR] = false;
900 isSet[DAY_OF_YEAR] = false;
901 isSet[WEEK_OF_MONTH] = false;
902 isSet[DAY_OF_WEEK] = false;
903 isSet[DAY_OF_WEEK_IN_MONTH] = false;
906 if (! explicitDSTOffset)
907 isSet[DST_OFFSET] = false; // May have crossed a DST boundary.
911 * Sets the fields for year, month, date, hour, and minute
912 * @param year the year.
913 * @param month the month, one of the constants JANUARY..UNDICEMBER.
914 * @param date the day of the month
915 * @param hour the hour of day.
916 * @param minute the minute.
918 public final void set(int year, int month, int date, int hour, int minute)
920 set(year, month, date);
921 fields[HOUR_OF_DAY] = hour;
922 fields[MINUTE] = minute;
923 isSet[HOUR_OF_DAY] = isSet[MINUTE] = true;
924 isSet[AM_PM] = false;
929 * Sets the fields for year, month, date, hour, and minute
930 * @param year the year.
931 * @param month the month, one of the constants JANUARY..UNDICEMBER.
932 * @param date the day of the month
933 * @param hour the hour of day.
934 * @param minute the minute.
935 * @param second the second.
937 public final void set(int year, int month, int date, int hour, int minute,
940 set(year, month, date, hour, minute);
941 fields[SECOND] = second;
942 isSet[SECOND] = true;
946 * Clears the values of all the time fields.
948 public final void clear()
951 areFieldsSet = false;
952 int zoneOffs = zone.getRawOffset();
955 1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0,
959 for (int i = 0; i < FIELD_COUNT; i++)
964 * Clears the values of the specified time field.
965 * @param field the time field. One of the time field constants.
966 * @throws ArrayIndexOutOfBoundsException if field is outside
967 * the valid range. The value of field must be >= 0 and
968 * <= <code>FIELD_COUNT</code>.
970 public final void clear(int field)
974 1, 1970, JANUARY, 1, 1, 1, 1, THURSDAY, 1, AM, 0, 0, 0,
975 0, 0, zone.getRawOffset(), 0
979 areFieldsSet = false;
980 isSet[field] = false;
981 fields[field] = tempFields[field];
985 * Determines if the specified field has a valid value.
986 * @return true if the specified field has a value.
987 * @throws ArrayIndexOutOfBoundsException if the field is outside
988 * the valid range. The value of field must be >= 0 and
989 * <= <code>FIELD_COUNT</code>.
991 public final boolean isSet(int field)
997 * Fills any unset fields in the time field list
999 protected void complete()
1008 * Compares the given calendar with this.
1009 * @param o the object to that we should compare.
1010 * @return true, if the given object is a calendar, that represents
1011 * the same time (but doesn't necessary have the same fields).
1013 public boolean equals(Object o)
1015 if (! (o instanceof Calendar))
1017 Calendar cal = (Calendar) o;
1018 if (getTimeInMillis() == ((Calendar) o).getTimeInMillis()
1019 && cal.getFirstDayOfWeek() == getFirstDayOfWeek()
1020 && cal.isLenient() == isLenient()
1021 && cal.getMinimalDaysInFirstWeek() == getMinimalDaysInFirstWeek())
1023 TimeZone self = getTimeZone();
1024 TimeZone oth = cal.getTimeZone();
1025 return self == null ? oth == null : self.equals(oth);
1031 * Returns a hash code for this calendar.
1032 * @return a hash code, which fullfits the general contract of
1033 * <code>hashCode()</code>
1035 public int hashCode()
1037 long time = getTimeInMillis();
1038 int val = (int) ((time & 0xffffffffL) ^ (time >> 32));
1039 val += (getFirstDayOfWeek() + (isLenient() ? 1230 : 1237)
1040 + getMinimalDaysInFirstWeek());
1041 TimeZone self = getTimeZone();
1043 val ^= self.hashCode();
1048 * Compares the given calendar with this.
1049 * @param o the object to that we should compare.
1050 * @return true, if the given object is a calendar, and this calendar
1051 * represents a smaller time than the calendar o.
1052 * @exception ClassCastException if o is not an calendar.
1053 * @since JDK1.2 you don't need to override this method
1055 public boolean before(Object o)
1057 return getTimeInMillis() < ((Calendar) o).getTimeInMillis();
1061 * Compares the given calendar with this.
1062 * @param o the object to that we should compare.
1063 * @return true, if the given object is a calendar, and this calendar
1064 * represents a bigger time than the calendar o.
1065 * @exception ClassCastException if o is not an calendar.
1066 * @since JDK1.2 you don't need to override this method
1068 public boolean after(Object o)
1070 return getTimeInMillis() > ((Calendar) o).getTimeInMillis();
1074 * Adds the specified amount of time to the given time field. The
1075 * amount may be negative to subtract the time. If the field overflows
1076 * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
1077 * @param field the time field. One of the time field constants.
1078 * @param amount the amount of time.
1079 * @throws ArrayIndexOutOfBoundsException if the field is outside
1080 * the valid range. The value of field must be >= 0 and
1081 * <= <code>FIELD_COUNT</code>.
1083 public abstract void add(int field, int amount);
1086 * Rolls the specified time field up or down. This means add one
1087 * to the specified field, but don't change the other fields. If
1088 * the maximum for this field is reached, start over with the
1089 * minimum value. <br>
1091 * <strong>Note:</strong> There may be situation, where the other
1092 * fields must be changed, e.g rolling the month on May, 31.
1093 * The date June, 31 is automatically converted to July, 1.
1094 * @param field the time field. One of the time field constants.
1095 * @param up the direction, true for up, false for down.
1096 * @throws ArrayIndexOutOfBoundsException if the field is outside
1097 * the valid range. The value of field must be >= 0 and
1098 * <= <code>FIELD_COUNT</code>.
1100 public abstract void roll(int field, boolean up);
1103 * Rolls up or down the specified time field by the given amount.
1104 * A negative amount rolls down. The default implementation is
1105 * call <code>roll(int, boolean)</code> for the specified amount.
1107 * Subclasses should override this method to do more intuitiv things.
1109 * @param field the time field. One of the time field constants.
1110 * @param amount the amount to roll by, positive for rolling up,
1111 * negative for rolling down.
1112 * @throws ArrayIndexOutOfBoundsException if the field is outside
1113 * the valid range. The value of field must be >= 0 and
1114 * <= <code>FIELD_COUNT</code>.
1117 public void roll(int field, int amount)
1132 * Sets the time zone to the specified value.
1133 * @param zone the new time zone
1135 public void setTimeZone(TimeZone zone)
1143 * Gets the time zone of this calendar
1144 * @return the current time zone.
1146 public TimeZone getTimeZone()
1152 * Specifies if the date/time interpretation should be lenient.
1153 * If the flag is set, a date such as "February 30, 1996" will be
1154 * treated as the 29th day after the February 1. If this flag
1155 * is false, such dates will cause an exception.
1156 * @param lenient true, if the date should be interpreted linient,
1157 * false if it should be interpreted strict.
1159 public void setLenient(boolean lenient)
1161 this.lenient = lenient;
1165 * Tells if the date/time interpretation is lenient.
1166 * @return true, if the date should be interpreted linient,
1167 * false if it should be interpreted strict.
1169 public boolean isLenient()
1175 * Sets what the first day of week is. This is used for
1176 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1177 * @param value the first day of week. One of SUNDAY to SATURDAY.
1179 public void setFirstDayOfWeek(int value)
1181 firstDayOfWeek = value;
1185 * Gets what the first day of week is. This is used for
1186 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1187 * @return the first day of week. One of SUNDAY to SATURDAY.
1189 public int getFirstDayOfWeek()
1191 return firstDayOfWeek;
1195 * Sets how many days are required in the first week of the year.
1196 * If the first day of the year should be the first week you should
1197 * set this value to 1. If the first week must be a full week, set
1199 * @param value the minimal days required in the first week.
1201 public void setMinimalDaysInFirstWeek(int value)
1203 minimalDaysInFirstWeek = value;
1207 * Gets how many days are required in the first week of the year.
1208 * @return the minimal days required in the first week.
1209 * @see #setMinimalDaysInFirstWeek
1211 public int getMinimalDaysInFirstWeek()
1213 return minimalDaysInFirstWeek;
1217 * Gets the smallest value that is allowed for the specified field.
1218 * @param field the time field. One of the time field constants.
1219 * @return the smallest value.
1221 public abstract int getMinimum(int field);
1224 * Gets the biggest value that is allowed for the specified field.
1225 * @param field the time field. One of the time field constants.
1226 * @return the biggest value.
1228 public abstract int getMaximum(int field);
1231 * Gets the greatest minimum value that is allowed for the specified field.
1232 * @param field the time field. One of the time field constants.
1233 * @return the greatest minimum value.
1235 public abstract int getGreatestMinimum(int field);
1238 * Gets the smallest maximum value that is allowed for the
1239 * specified field. For example this is 28 for DAY_OF_MONTH.
1240 * @param field the time field. One of the time field constants.
1241 * @return the least maximum value.
1243 public abstract int getLeastMaximum(int field);
1246 * Gets the actual minimum value that is allowed for the specified field.
1247 * This value is dependent on the values of the other fields.
1248 * @param field the time field. One of the time field constants.
1249 * @return the actual minimum value.
1250 * @throws ArrayIndexOutOfBoundsException if the field is outside
1251 * the valid range. The value of field must be >= 0 and
1252 * <= <code>FIELD_COUNT</code>.
1255 public int getActualMinimum(int field)
1257 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1258 int min = tmp.getGreatestMinimum(field);
1259 int end = tmp.getMinimum(field);
1260 tmp.set(field, min);
1261 for (; min > end; min--)
1263 tmp.add(field, -1); // Try to get smaller
1264 if (tmp.get(field) != min - 1)
1265 break; // Done if not successful
1271 * Gets the actual maximum value that is allowed for the specified field.
1272 * This value is dependent on the values of the other fields.
1273 * @param field the time field. One of the time field constants.
1274 * @return the actual maximum value.
1275 * @throws ArrayIndexOutOfBoundsException if the field is outside
1276 * the valid range. The value of field must be >= 0 and
1277 * <= <code>FIELD_COUNT</code>.
1280 public int getActualMaximum(int field)
1282 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1283 int max = tmp.getLeastMaximum(field);
1284 int end = tmp.getMaximum(field);
1285 tmp.set(field, max);
1286 for (; max < end; max++)
1289 if (tmp.get(field) != max + 1)
1296 * Compares the time of two calendar instances.
1297 * @param cal the calendar to which the time should be compared.
1298 * @return 0 if the two calendars are set to the same time,
1299 * less than 0 if the time of this calendar is before that of
1300 * <code>cal</code>, or more than 0 if the time of this calendar is after
1301 * that of <code>cal</code>.
1303 * @param cal the calendar to compare this instance with.
1304 * @throws NullPointerException if <code>cal</code> is null.
1305 * @throws IllegalArgumentException if either calendar has fields set to
1309 public int compareTo(Calendar cal)
1311 long t1 = getTimeInMillis();
1312 long t2 = cal.getTimeInMillis();
1321 * Return a clone of this object.
1323 public Object clone()
1327 Calendar cal = (Calendar) super.clone();
1328 cal.fields = (int[]) fields.clone();
1329 cal.isSet = (boolean[]) isSet.clone();
1332 catch (CloneNotSupportedException ex)
1338 private static final String[] fieldNames =
1340 ",ERA=", ",YEAR=", ",MONTH=",
1344 ",DAY_OF_YEAR=", ",DAY_OF_WEEK=",
1345 ",DAY_OF_WEEK_IN_MONTH=",
1346 ",AM_PM=", ",HOUR=",
1347 ",HOUR_OF_DAY=", ",MINUTE=",
1348 ",SECOND=", ",MILLISECOND=",
1349 ",ZONE_OFFSET=", ",DST_OFFSET="
1353 * Returns a string representation of this object. It is mainly
1354 * for debugging purposes and its content is implementation
1357 public String toString()
1359 CPStringBuilder sb = new CPStringBuilder(getClass().getName());
1366 sb.append(",zone=" + zone);
1367 sb.append(",areFieldsSet=" + areFieldsSet);
1368 for (int i = 0; i < FIELD_COUNT; i++)
1370 sb.append(fieldNames[i]);
1372 sb.append(fields[i]);
1376 sb.append(",lenient=").append(lenient);
1377 sb.append(",firstDayOfWeek=").append(firstDayOfWeek);
1378 sb.append(",minimalDaysInFirstWeek=").append(minimalDaysInFirstWeek);
1380 return sb.toString();
1384 * Saves the state of the object to the stream. Ideally we would
1385 * only write the time field, but we need to be compatible with
1386 * earlier versions. <br>
1388 * This doesn't write the JDK1.1 field nextStamp to the stream, as
1389 * I don't know what it is good for, and because the documentation
1390 * says, that it could be omitted. */
1391 private void writeObject(ObjectOutputStream stream) throws IOException
1395 stream.defaultWriteObject();
1399 * Reads the object back from stream (deserialization).
1401 private void readObject(ObjectInputStream stream)
1402 throws IOException, ClassNotFoundException
1404 stream.defaultReadObject();
1408 if (serialVersionOnStream > 1)
1410 // This is my interpretation of the serial number:
1411 // Sun wants to remove all fields from the stream someday
1412 // and will then increase the serialVersion number again.
1413 // We prepare to be compatible.
1414 fields = new int[FIELD_COUNT];
1415 isSet = new boolean[FIELD_COUNT];
1416 areFieldsSet = false;
1421 * Returns a localised textual representation of the current value
1422 * of the given field using the specified style. If there is no
1423 * applicable textual representation (e.g. the field has a numeric
1424 * value), then <code>null</code> is returned. If one does exist,
1425 * then the value is obtained from {@link #get(int)} and converted
1426 * appropriately. For example, if the <code>MONTH</code> field is
1427 * requested, then <code>get(MONTH)</code> is called. This is then
1428 * converted to a textual representation based on its value and
1429 * the style requested; if the <code>LONG</code> style is requested
1430 * and the returned value is <code>11</code> from a
1431 * {@link GregorianCalendar} implementation, then <code>"December"</code>
1432 * is returned. By default, a textual representation is available
1433 * for all fields which have an applicable value obtainable from
1434 * {@link java.text.DateFormatSymbols}.
1436 * @param field the calendar field whose textual representation should
1438 * @param style the style to use; either {@link #LONG} or {@link #SHORT}.
1439 * @param locale the locale to use for translation.
1440 * @return the textual representation of the given field in the specified
1441 * style, or <code>null</code> if none is applicable.
1442 * @throws IllegalArgumentException if <code>field</code> or <code>style</code>
1443 * or invalid, or the calendar is non-lenient
1444 * and has invalid values.
1445 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
1448 public String getDisplayName(int field, int style, Locale locale)
1450 if (field < 0 || field >= FIELD_COUNT)
1451 throw new IllegalArgumentException("The field value, " + field +
1453 if (style != SHORT && style != LONG)
1454 throw new IllegalArgumentException("The style must be either " +
1456 if (field == YEAR || field == WEEK_OF_YEAR ||
1457 field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
1458 field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
1459 field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
1460 field == SECOND || field == MILLISECOND)
1463 int value = get(field);
1464 DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
1466 return syms.getEras()[value];
1469 return syms.getMonths()[value];
1471 return syms.getShortMonths()[value];
1472 if (field == DAY_OF_WEEK)
1474 return syms.getWeekdays()[value];
1476 return syms.getShortWeekdays()[value];
1478 return syms.getAmPmStrings()[value];
1479 if (field == ZONE_OFFSET)
1481 return syms.getZoneStrings()[value][1];
1483 return syms.getZoneStrings()[value][2];
1484 if (field == DST_OFFSET)
1486 return syms.getZoneStrings()[value][3];
1488 return syms.getZoneStrings()[value][4];
1490 throw new InternalError("Failed to resolve field " + field +
1491 " with style " + style + " for locale " +
1496 * Returns a map linking all specified textual representations
1497 * of the given field to their numerical values. The textual
1498 * representations included are determined by the specified
1499 * style and locale. For example, if the style <code>LONG</code>
1500 * is specified and the German locale, then the map will
1501 * contain "Montag" to {@link #MONDAY}, "Dienstag" to
1502 * {@link #TUESDAY}, "Mittwoch" to {@link #WEDNESDAY} and
1503 * so on. The default implementation uses the values returned
1504 * by {@link DateFormatSymbols} so, for example, the style
1505 * {@link #ALL_STYLES} and the field {@link #MONTH} will return
1506 * a map filled with the values returned from
1507 * {@link DateFormatSymbols#getMonths()} and
1508 * {@link DateFormatSymbols#getShortMonths()}. If there are
1509 * no textual representations for a given field (usually because
1510 * it is purely numeric, such as the year in the
1511 * {@link GregorianCalendar}), <code>null</code> is returned.
1513 * @param field the calendar field whose textual representation should
1515 * @param style the style to use; either {@link #LONG}, {@link #SHORT}
1516 * or {@link ALL_STYLES}.
1517 * @param locale the locale to use for translation.
1518 * @return a map of the textual representations of the given field in the
1519 * specified style to their numeric values, or <code>null</code>
1520 * if none is applicable.
1521 * @throws IllegalArgumentException if <code>field</code> or <code>style</code>
1522 * or invalid, or the calendar is non-lenient
1523 * and has invalid values.
1524 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
1527 public Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
1529 if (field < 0 || field >= FIELD_COUNT)
1530 throw new IllegalArgumentException("The field value, " + field +
1532 if (style != SHORT && style != LONG && style != ALL_STYLES)
1533 throw new IllegalArgumentException("The style must be either " +
1534 "short, long or all styles.");
1535 if (field == YEAR || field == WEEK_OF_YEAR ||
1536 field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
1537 field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
1538 field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
1539 field == SECOND || field == MILLISECOND)
1542 DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
1543 Map<String,Integer> map = new HashMap<String,Integer>();
1546 String[] eras = syms.getEras();
1547 for (int a = 0; a < eras.length; ++a)
1548 map.put(eras[a], a);
1553 if (style == LONG || style == ALL_STYLES)
1555 String[] months = syms.getMonths();
1556 for (int a = 0; a < months.length; ++a)
1557 map.put(months[a], a);
1559 if (style == SHORT || style == ALL_STYLES)
1561 String[] months = syms.getShortMonths();
1562 for (int a = 0; a < months.length; ++a)
1563 map.put(months[a], a);
1567 if (field == DAY_OF_WEEK)
1569 if (style == LONG || style == ALL_STYLES)
1571 String[] weekdays = syms.getWeekdays();
1572 for (int a = SUNDAY; a < weekdays.length; ++a)
1573 map.put(weekdays[a], a);
1575 if (style == SHORT || style == ALL_STYLES)
1577 String[] weekdays = syms.getShortWeekdays();
1578 for (int a = SUNDAY; a < weekdays.length; ++a)
1579 map.put(weekdays[a], a);
1585 String[] ampms = syms.getAmPmStrings();
1586 for (int a = 0; a < ampms.length; ++a)
1587 map.put(ampms[a], a);
1590 if (field == ZONE_OFFSET)
1592 String[][] zones = syms.getZoneStrings();
1593 for (int a = 0; a < zones.length; ++a)
1595 if (style == LONG || style == ALL_STYLES)
1596 map.put(zones[a][1], a);
1597 if (style == SHORT || style == ALL_STYLES)
1598 map.put(zones[a][2], a);
1602 if (field == DST_OFFSET)
1604 String[][] zones = syms.getZoneStrings();
1605 for (int a = 0; a < zones.length; ++a)
1607 if (style == LONG || style == ALL_STYLES)
1608 map.put(zones[a][3], a);
1609 if (style == SHORT || style == ALL_STYLES)
1610 map.put(zones[a][4], a);
1615 throw new InternalError("Failed to resolve field " + field +
1616 " with style " + style + " for locale " +