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
515 /*protected Calendar()
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 = "1";//properties.getProperty("minDays." + country);
535 min = properties.getProperty("minDays.DEFAULT");*/
536 String first = "sun";//properties.getProperty("firstDay." + country);
538 first = properties.getProperty("firstDay.DEFAULT");*/
542 minimalDaysInFirstWeek = Integer.parseInt(min);
544 catch (/*NumberFormat*/Exception 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 new GregorianCalendar/*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 new GregorianCalendar/*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 = (Class)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 // TODO always false now
1014 public boolean equals(Object o)
1016 if (! (o instanceof Calendar))
1018 Calendar cal = (Calendar) o;
1019 /*if (getTimeInMillis() == ((Calendar) o).getTimeInMillis()
1020 && cal.getFirstDayOfWeek() == getFirstDayOfWeek()
1021 && cal.isLenient() == isLenient()
1022 && cal.getMinimalDaysInFirstWeek() == getMinimalDaysInFirstWeek())
1024 TimeZone self = getTimeZone();
1025 TimeZone oth = cal.getTimeZone();
1026 return self == null ? oth == null : self.equals(oth);
1032 * Returns a hash code for this calendar.
1033 * @return a hash code, which fullfits the general contract of
1034 * <code>hashCode()</code>
1036 public int hashCode()
1038 long time = getTimeInMillis();
1039 int val = (int) ((time & 0xffffffffL) ^ (time >> 32));
1040 /*val += (getFirstDayOfWeek() + (isLenient() ? 1230 : 1237)
1041 + getMinimalDaysInFirstWeek());
1042 TimeZone self = getTimeZone();
1044 val ^= self.hashCode();*/
1049 * Compares the given calendar with this.
1050 * @param o the object to that we should compare.
1051 * @return true, if the given object is a calendar, and this calendar
1052 * represents a smaller time than the calendar o.
1053 * @exception ClassCastException if o is not an calendar.
1054 * @since JDK1.2 you don't need to override this method
1056 /*public boolean before(Object o)
1058 return getTimeInMillis() < ((Calendar) o).getTimeInMillis();
1062 * Compares the given calendar with this.
1063 * @param o the object to that we should compare.
1064 * @return true, if the given object is a calendar, and this calendar
1065 * represents a bigger time than the calendar o.
1066 * @exception ClassCastException if o is not an calendar.
1067 * @since JDK1.2 you don't need to override this method
1069 /*public boolean after(Object o)
1071 return getTimeInMillis() > ((Calendar) o).getTimeInMillis();
1075 * Adds the specified amount of time to the given time field. The
1076 * amount may be negative to subtract the time. If the field overflows
1077 * it does what you expect: Jan, 25 + 10 Days is Feb, 4.
1078 * @param field the time field. One of the time field constants.
1079 * @param amount the amount of time.
1080 * @throws ArrayIndexOutOfBoundsException if the field is outside
1081 * the valid range. The value of field must be >= 0 and
1082 * <= <code>FIELD_COUNT</code>.
1084 public /*abstract*/ void add(int field, int amount);
1087 * Rolls the specified time field up or down. This means add one
1088 * to the specified field, but don't change the other fields. If
1089 * the maximum for this field is reached, start over with the
1090 * minimum value. <br>
1092 * <strong>Note:</strong> There may be situation, where the other
1093 * fields must be changed, e.g rolling the month on May, 31.
1094 * The date June, 31 is automatically converted to July, 1.
1095 * @param field the time field. One of the time field constants.
1096 * @param up the direction, true for up, false for down.
1097 * @throws ArrayIndexOutOfBoundsException if the field is outside
1098 * the valid range. The value of field must be >= 0 and
1099 * <= <code>FIELD_COUNT</code>.
1101 public /*abstract*/ void roll(int field, boolean up);
1104 * Rolls up or down the specified time field by the given amount.
1105 * A negative amount rolls down. The default implementation is
1106 * call <code>roll(int, boolean)</code> for the specified amount.
1108 * Subclasses should override this method to do more intuitiv things.
1110 * @param field the time field. One of the time field constants.
1111 * @param amount the amount to roll by, positive for rolling up,
1112 * negative for rolling down.
1113 * @throws ArrayIndexOutOfBoundsException if the field is outside
1114 * the valid range. The value of field must be >= 0 and
1115 * <= <code>FIELD_COUNT</code>.
1118 public void roll(int field, int amount)
1133 * Sets the time zone to the specified value.
1134 * @param zone the new time zone
1136 /*public void setTimeZone(TimeZone zone)
1144 * Gets the time zone of this calendar
1145 * @return the current time zone.
1147 public TimeZone getTimeZone()
1153 * Specifies if the date/time interpretation should be lenient.
1154 * If the flag is set, a date such as "February 30, 1996" will be
1155 * treated as the 29th day after the February 1. If this flag
1156 * is false, such dates will cause an exception.
1157 * @param lenient true, if the date should be interpreted linient,
1158 * false if it should be interpreted strict.
1160 public void setLenient(boolean lenient)
1162 this.lenient = lenient;
1166 * Tells if the date/time interpretation is lenient.
1167 * @return true, if the date should be interpreted linient,
1168 * false if it should be interpreted strict.
1170 public boolean isLenient()
1176 * Sets what the first day of week is. This is used for
1177 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1178 * @param value the first day of week. One of SUNDAY to SATURDAY.
1180 public void setFirstDayOfWeek(int value)
1182 firstDayOfWeek = value;
1186 * Gets what the first day of week is. This is used for
1187 * WEEK_OF_MONTH and WEEK_OF_YEAR fields.
1188 * @return the first day of week. One of SUNDAY to SATURDAY.
1190 public int getFirstDayOfWeek()
1192 return firstDayOfWeek;
1196 * Sets how many days are required in the first week of the year.
1197 * If the first day of the year should be the first week you should
1198 * set this value to 1. If the first week must be a full week, set
1200 * @param value the minimal days required in the first week.
1202 public void setMinimalDaysInFirstWeek(int value)
1204 minimalDaysInFirstWeek = value;
1208 * Gets how many days are required in the first week of the year.
1209 * @return the minimal days required in the first week.
1210 * @see #setMinimalDaysInFirstWeek
1212 public int getMinimalDaysInFirstWeek()
1214 return minimalDaysInFirstWeek;
1218 * Gets the smallest value that is allowed for the specified field.
1219 * @param field the time field. One of the time field constants.
1220 * @return the smallest value.
1222 public /*abstract*/ int getMinimum(int field);
1225 * Gets the biggest value that is allowed for the specified field.
1226 * @param field the time field. One of the time field constants.
1227 * @return the biggest value.
1229 public /*abstract*/ int getMaximum(int field);
1232 * Gets the greatest minimum value that is allowed for the specified field.
1233 * @param field the time field. One of the time field constants.
1234 * @return the greatest minimum value.
1236 public /*abstract*/ int getGreatestMinimum(int field);
1239 * Gets the smallest maximum value that is allowed for the
1240 * specified field. For example this is 28 for DAY_OF_MONTH.
1241 * @param field the time field. One of the time field constants.
1242 * @return the least maximum value.
1244 public /*abstract*/ int getLeastMaximum(int field);
1247 * Gets the actual minimum value that is allowed for the specified field.
1248 * This value is dependent on the values of the other fields.
1249 * @param field the time field. One of the time field constants.
1250 * @return the actual minimum value.
1251 * @throws ArrayIndexOutOfBoundsException if the field is outside
1252 * the valid range. The value of field must be >= 0 and
1253 * <= <code>FIELD_COUNT</code>.
1256 /*public int getActualMinimum(int field)
1258 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1259 int min = tmp.getGreatestMinimum(field);
1260 int end = tmp.getMinimum(field);
1261 tmp.set(field, min);
1262 for (; min > end; min--)
1264 tmp.add(field, -1); // Try to get smaller
1265 if (tmp.get(field) != min - 1)
1266 break; // Done if not successful
1272 * Gets the actual maximum value that is allowed for the specified field.
1273 * This value is dependent on the values of the other fields.
1274 * @param field the time field. One of the time field constants.
1275 * @return the actual maximum value.
1276 * @throws ArrayIndexOutOfBoundsException if the field is outside
1277 * the valid range. The value of field must be >= 0 and
1278 * <= <code>FIELD_COUNT</code>.
1281 /*public int getActualMaximum(int field)
1283 Calendar tmp = (Calendar) clone(); // To avoid restoring state
1284 int max = tmp.getLeastMaximum(field);
1285 int end = tmp.getMaximum(field);
1286 tmp.set(field, max);
1287 for (; max < end; max++)
1290 if (tmp.get(field) != max + 1)
1297 * Compares the time of two calendar instances.
1298 * @param cal the calendar to which the time should be compared.
1299 * @return 0 if the two calendars are set to the same time,
1300 * less than 0 if the time of this calendar is before that of
1301 * <code>cal</code>, or more than 0 if the time of this calendar is after
1302 * that of <code>cal</code>.
1304 * @param cal the calendar to compare this instance with.
1305 * @throws NullPointerException if <code>cal</code> is null.
1306 * @throws IllegalArgumentException if either calendar has fields set to
1310 /*public int compareTo(Calendar cal)
1312 long t1 = getTimeInMillis();
1313 long t2 = cal.getTimeInMillis();
1322 * Return a clone of this object.
1324 /*public Object clone()
1328 Calendar cal = super.clone();
1329 cal.fields = (int[]) fields.clone();
1330 cal.isSet = (boolean[]) isSet.clone();
1333 catch (CloneNotSupportedException ex)
1339 private static final String[] fieldNames =
1341 ",ERA=", ",YEAR=", ",MONTH=",
1345 ",DAY_OF_YEAR=", ",DAY_OF_WEEK=",
1346 ",DAY_OF_WEEK_IN_MONTH=",
1347 ",AM_PM=", ",HOUR=",
1348 ",HOUR_OF_DAY=", ",MINUTE=",
1349 ",SECOND=", ",MILLISECOND=",
1350 ",ZONE_OFFSET=", ",DST_OFFSET="
1354 * Returns a string representation of this object. It is mainly
1355 * for debugging purposes and its content is implementation
1358 /*public String toString()
1360 CPStringBuilder sb = new CPStringBuilder(getClass().getName());
1367 sb.append(",zone=" + zone);
1368 sb.append(",areFieldsSet=" + areFieldsSet);
1369 for (int i = 0; i < FIELD_COUNT; i++)
1371 sb.append(fieldNames[i]);
1373 sb.append(fields[i]);
1377 sb.append(",lenient=").append(lenient);
1378 sb.append(",firstDayOfWeek=").append(firstDayOfWeek);
1379 sb.append(",minimalDaysInFirstWeek=").append(minimalDaysInFirstWeek);
1381 return sb.toString();
1385 * Saves the state of the object to the stream. Ideally we would
1386 * only write the time field, but we need to be compatible with
1387 * earlier versions. <br>
1389 * This doesn't write the JDK1.1 field nextStamp to the stream, as
1390 * I don't know what it is good for, and because the documentation
1391 * says, that it could be omitted. */
1392 /*private void writeObject(ObjectOutputStream stream) throws IOException
1396 stream.defaultWriteObject();
1400 * Reads the object back from stream (deserialization).
1402 /*private void readObject(ObjectInputStream stream)
1403 throws IOException, ClassNotFoundException
1405 stream.defaultReadObject();
1409 if (serialVersionOnStream > 1)
1411 // This is my interpretation of the serial number:
1412 // Sun wants to remove all fields from the stream someday
1413 // and will then increase the serialVersion number again.
1414 // We prepare to be compatible.
1415 fields = new int[FIELD_COUNT];
1416 isSet = new boolean[FIELD_COUNT];
1417 areFieldsSet = false;
1422 * Returns a localised textual representation of the current value
1423 * of the given field using the specified style. If there is no
1424 * applicable textual representation (e.g. the field has a numeric
1425 * value), then <code>null</code> is returned. If one does exist,
1426 * then the value is obtained from {@link #get(int)} and converted
1427 * appropriately. For example, if the <code>MONTH</code> field is
1428 * requested, then <code>get(MONTH)</code> is called. This is then
1429 * converted to a textual representation based on its value and
1430 * the style requested; if the <code>LONG</code> style is requested
1431 * and the returned value is <code>11</code> from a
1432 * {@link GregorianCalendar} implementation, then <code>"December"</code>
1433 * is returned. By default, a textual representation is available
1434 * for all fields which have an applicable value obtainable from
1435 * {@link java.text.DateFormatSymbols}.
1437 * @param field the calendar field whose textual representation should
1439 * @param style the style to use; either {@link #LONG} or {@link #SHORT}.
1440 * @param locale the locale to use for translation.
1441 * @return the textual representation of the given field in the specified
1442 * style, or <code>null</code> if none is applicable.
1443 * @throws IllegalArgumentException if <code>field</code> or <code>style</code>
1444 * or invalid, or the calendar is non-lenient
1445 * and has invalid values.
1446 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
1449 /*public String getDisplayName(int field, int style, Locale locale)
1451 if (field < 0 || field >= FIELD_COUNT)
1452 throw new IllegalArgumentException("The field value, " + field +
1454 if (style != SHORT && style != LONG)
1455 throw new IllegalArgumentException("The style must be either " +
1457 if (field == YEAR || field == WEEK_OF_YEAR ||
1458 field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
1459 field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
1460 field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
1461 field == SECOND || field == MILLISECOND)
1464 int value = get(field);
1465 DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
1467 return syms.getEras()[value];
1470 return syms.getMonths()[value];
1472 return syms.getShortMonths()[value];
1473 if (field == DAY_OF_WEEK)
1475 return syms.getWeekdays()[value];
1477 return syms.getShortWeekdays()[value];
1479 return syms.getAmPmStrings()[value];
1480 if (field == ZONE_OFFSET)
1482 return syms.getZoneStrings()[value][1];
1484 return syms.getZoneStrings()[value][2];
1485 if (field == DST_OFFSET)
1487 return syms.getZoneStrings()[value][3];
1489 return syms.getZoneStrings()[value][4];
1491 throw new InternalError("Failed to resolve field " + field +
1492 " with style " + style + " for locale " +
1497 * Returns a map linking all specified textual representations
1498 * of the given field to their numerical values. The textual
1499 * representations included are determined by the specified
1500 * style and locale. For example, if the style <code>LONG</code>
1501 * is specified and the German locale, then the map will
1502 * contain "Montag" to {@link #MONDAY}, "Dienstag" to
1503 * {@link #TUESDAY}, "Mittwoch" to {@link #WEDNESDAY} and
1504 * so on. The default implementation uses the values returned
1505 * by {@link DateFormatSymbols} so, for example, the style
1506 * {@link #ALL_STYLES} and the field {@link #MONTH} will return
1507 * a map filled with the values returned from
1508 * {@link DateFormatSymbols#getMonths()} and
1509 * {@link DateFormatSymbols#getShortMonths()}. If there are
1510 * no textual representations for a given field (usually because
1511 * it is purely numeric, such as the year in the
1512 * {@link GregorianCalendar}), <code>null</code> is returned.
1514 * @param field the calendar field whose textual representation should
1516 * @param style the style to use; either {@link #LONG}, {@link #SHORT}
1517 * or {@link ALL_STYLES}.
1518 * @param locale the locale to use for translation.
1519 * @return a map of the textual representations of the given field in the
1520 * specified style to their numeric values, or <code>null</code>
1521 * if none is applicable.
1522 * @throws IllegalArgumentException if <code>field</code> or <code>style</code>
1523 * or invalid, or the calendar is non-lenient
1524 * and has invalid values.
1525 * @throws NullPointerException if <code>locale</code> is <code>null</code>.
1528 /*public Map<String,Integer> getDisplayNames(int field, int style, Locale locale)
1530 if (field < 0 || field >= FIELD_COUNT)
1531 throw new IllegalArgumentException("The field value, " + field +
1533 if (style != SHORT && style != LONG && style != ALL_STYLES)
1534 throw new IllegalArgumentException("The style must be either " +
1535 "short, long or all styles.");
1536 if (field == YEAR || field == WEEK_OF_YEAR ||
1537 field == WEEK_OF_MONTH || field == DAY_OF_MONTH ||
1538 field == DAY_OF_YEAR || field == DAY_OF_WEEK_IN_MONTH ||
1539 field == HOUR || field == HOUR_OF_DAY || field == MINUTE ||
1540 field == SECOND || field == MILLISECOND)
1543 DateFormatSymbols syms = DateFormatSymbols.getInstance(locale);
1544 Map<String,Integer> map = new HashMap<String,Integer>();
1547 String[] eras = syms.getEras();
1548 for (int a = 0; a < eras.length; ++a)
1549 map.put(eras[a], a);
1554 if (style == LONG || style == ALL_STYLES)
1556 String[] months = syms.getMonths();
1557 for (int a = 0; a < months.length; ++a)
1558 map.put(months[a], a);
1560 if (style == SHORT || style == ALL_STYLES)
1562 String[] months = syms.getShortMonths();
1563 for (int a = 0; a < months.length; ++a)
1564 map.put(months[a], a);
1568 if (field == DAY_OF_WEEK)
1570 if (style == LONG || style == ALL_STYLES)
1572 String[] weekdays = syms.getWeekdays();
1573 for (int a = SUNDAY; a < weekdays.length; ++a)
1574 map.put(weekdays[a], a);
1576 if (style == SHORT || style == ALL_STYLES)
1578 String[] weekdays = syms.getShortWeekdays();
1579 for (int a = SUNDAY; a < weekdays.length; ++a)
1580 map.put(weekdays[a], a);
1586 String[] ampms = syms.getAmPmStrings();
1587 for (int a = 0; a < ampms.length; ++a)
1588 map.put(ampms[a], a);
1591 if (field == ZONE_OFFSET)
1593 String[][] zones = syms.getZoneStrings();
1594 for (int a = 0; a < zones.length; ++a)
1596 if (style == LONG || style == ALL_STYLES)
1597 map.put(zones[a][1], a);
1598 if (style == SHORT || style == ALL_STYLES)
1599 map.put(zones[a][2], a);
1603 if (field == DST_OFFSET)
1605 String[][] zones = syms.getZoneStrings();
1606 for (int a = 0; a < zones.length; ++a)
1608 if (style == LONG || style == ALL_STYLES)
1609 map.put(zones[a][3], a);
1610 if (style == SHORT || style == ALL_STYLES)
1611 map.put(zones[a][4], a);
1616 throw new InternalError("Failed to resolve field " + field +
1617 " with style " + style + " for locale " +