• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.util;
19 
20 import java.io.IOException;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.ObjectStreamField;
24 import java.io.Serializable;
25 import java.text.DateFormatSymbols;
26 import libcore.icu.ICU;
27 import libcore.icu.LocaleData;
28 
29 /**
30  * {@code Calendar} is an abstract base class for converting between a
31  * {@code Date} object and a set of integer fields such as
32  * {@code YEAR}, {@code MONTH}, {@code DAY},
33  * {@code HOUR}, and so on. (A {@code Date} object represents a
34  * specific instant in time with millisecond precision. See {@link Date} for
35  * information about the {@code Date} class.)
36  *
37  * <p>
38  * Subclasses of {@code Calendar} interpret a {@code Date}
39  * according to the rules of a specific calendar system.
40  *
41  * <p>
42  * Like other locale-sensitive classes, {@code Calendar} provides a class
43  * method, {@code getInstance}, for getting a default instance of
44  * this class for general use. {@code Calendar}'s {@code getInstance} method
45  * returns a calendar whose locale is based on system settings and whose time fields
46  * have been initialized with the current date and time: <blockquote>
47  *
48  * <pre>Calendar rightNow = Calendar.getInstance()</pre>
49  *
50  * </blockquote>
51  *
52  * <p>
53  * A {@code Calendar} object can produce all the time field values needed
54  * to implement the date-time formatting for a particular language and calendar
55  * style (for example, Japanese-Gregorian, Japanese-Traditional).
56  * {@code Calendar} defines the range of values returned by certain
57  * fields, as well as their meaning. For example, the first month of the year
58  * has value {@code MONTH} == {@code JANUARY} for all calendars.
59  * Other values are defined by the concrete subclass, such as {@code ERA}
60  * and {@code YEAR}. See individual field documentation and subclass
61  * documentation for details.
62  *
63  * <p>
64  * When a {@code Calendar} is <em>lenient</em>, it accepts a wider
65  * range of field values than it produces. For example, a lenient
66  * {@code GregorianCalendar} interprets {@code MONTH} ==
67  * {@code JANUARY}, {@code DAY_OF_MONTH} == 32 as February 1. A
68  * non-lenient {@code GregorianCalendar} throws an exception when given
69  * out-of-range field settings. When calendars recompute field values for return
70  * by {@code get()}, they normalize them. For example, a
71  * {@code GregorianCalendar} always produces {@code DAY_OF_MONTH}
72  * values between 1 and the length of the month.
73  *
74  * <p>
75  * {@code Calendar} defines a locale-specific seven day week using two
76  * parameters: the first day of the week and the minimal days in first week
77  * (from 1 to 7). These numbers are taken from the locale resource data when a
78  * {@code Calendar} is constructed. They may also be specified explicitly
79  * through the API.
80  *
81  * <p>
82  * When setting or getting the {@code WEEK_OF_MONTH} or
83  * {@code WEEK_OF_YEAR} fields, {@code Calendar} must determine
84  * the first week of the month or year as a reference point. The first week of a
85  * month or year is defined as the earliest seven day period beginning on
86  * {@code getFirstDayOfWeek()} and containing at least
87  * {@code getMinimalDaysInFirstWeek()} days of that month or year. Weeks
88  * numbered ..., -1, 0 precede the first week; weeks numbered 2, 3,... follow
89  * it. Note that the normalized numbering returned by {@code get()} may
90  * be different. For example, a specific {@code Calendar} subclass may
91  * designate the week before week 1 of a year as week <em>n</em> of the
92  * previous year.
93  *
94  * <p>
95  * When computing a {@code Date} from time fields, two special
96  * circumstances may arise: there may be insufficient information to compute the
97  * {@code Date} (such as only year and month but no day in the month), or
98  * there may be inconsistent information (such as "Tuesday, July 15, 1996" --
99  * July 15, 1996 is actually a Monday).
100  *
101  * <p>
102  * <strong>Insufficient information.</strong> The calendar will use default
103  * information to specify the missing fields. This may vary by calendar; for the
104  * Gregorian calendar, the default for a field is the same as that of the start
105  * of the epoch: i.e., YEAR = 1970, MONTH = JANUARY, DATE = 1, etc.
106  *
107  * <p>
108  * <strong>Inconsistent information.</strong> If fields conflict, the calendar
109  * will give preference to fields set more recently. For example, when
110  * determining the day, the calendar will look for one of the following
111  * combinations of fields. The most recent combination, as determined by the
112  * most recently set single field, will be used.
113  *
114  * <blockquote>
115  *
116  * <pre>
117  * MONTH + DAY_OF_MONTH
118  * MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
119  * MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
120  * DAY_OF_YEAR
121  * DAY_OF_WEEK + WEEK_OF_YEAR</pre>
122  *
123  * </blockquote>
124  *
125  * For the time of day:
126  *
127  * <blockquote>
128  *
129  * <pre>
130  * HOUR_OF_DAY
131  * AM_PM + HOUR</pre>
132  *
133  * </blockquote>
134  *
135  * <p>
136  * <strong>Note:</strong> There are certain possible ambiguities in
137  * interpretation of certain singular times, which are resolved in the following
138  * ways:
139  * <ol>
140  * <li> 24:00:00 "belongs" to the following day. That is, 23:59 on Dec 31, 1969
141  * &lt; 24:00 on Jan 1, 1970 &lt; 24:01:00 on Jan 1, 1970 form a sequence of
142  * three consecutive minutes in time.
143  *
144  * <li> Although historically not precise, midnight also belongs to "am", and
145  * noon belongs to "pm", so on the same day, we have 12:00 am (midnight) &lt; 12:01 am,
146  * and 12:00 pm (noon) &lt; 12:01 pm
147  * </ol>
148  *
149  * <p>
150  * The date or time format strings are not part of the definition of a calendar,
151  * as those must be modifiable or overridable by the user at runtime. Use
152  * {@link java.text.DateFormat} to format dates.
153  *
154  * <p>
155  * <strong>Field manipulation methods</strong>
156  *
157  * <p>
158  * {@code Calendar} fields can be changed using three methods:
159  * {@code set()}, {@code add()}, and {@code roll()}.
160  *
161  * <p>
162  * <strong>{@code set(f, value)}</strong> changes field {@code f}
163  * to {@code value}. In addition, it sets an internal member variable to
164  * indicate that field {@code f} has been changed. Although field
165  * {@code f} is changed immediately, the calendar's milliseconds is not
166  * recomputed until the next call to {@code get()},
167  * {@code getTime()}, or {@code getTimeInMillis()} is made. Thus,
168  * multiple calls to {@code set()} do not trigger multiple, unnecessary
169  * computations. As a result of changing a field using {@code set()},
170  * other fields may also change, depending on the field, the field value, and
171  * the calendar system. In addition, {@code get(f)} will not necessarily
172  * return {@code value} after the fields have been recomputed. The
173  * specifics are determined by the concrete calendar class.
174  *
175  * <p>
176  * <em>Example</em>: Consider a {@code GregorianCalendar} originally
177  * set to August 31, 1999. Calling <code>set(Calendar.MONTH,
178  * Calendar.SEPTEMBER)</code>
179  * sets the calendar to September 31, 1999. This is a temporary internal
180  * representation that resolves to October 1, 1999 if {@code getTime()}is
181  * then called. However, a call to {@code set(Calendar.DAY_OF_MONTH, 30)}
182  * before the call to {@code getTime()} sets the calendar to September
183  * 30, 1999, since no recomputation occurs after {@code set()} itself.
184  *
185  * <p>
186  * <strong>{@code add(f, delta)}</strong> adds {@code delta} to
187  * field {@code f}. This is equivalent to calling <code>set(f,
188  * get(f) + delta)</code>
189  * with two adjustments:
190  *
191  * <blockquote>
192  * <p>
193  * <strong>Add rule 1</strong>. The value of field {@code f} after the
194  * call minus the value of field {@code f} before the call is
195  * {@code delta}, modulo any overflow that has occurred in field
196  * {@code f}. Overflow occurs when a field value exceeds its range and,
197  * as a result, the next larger field is incremented or decremented and the
198  * field value is adjusted back into its range.
199  *
200  * <p>
201  * <strong>Add rule 2</strong>. If a smaller field is expected to be invariant,
202  * but &nbsp; it is impossible for it to be equal to its prior value because of
203  * changes in its minimum or maximum after field {@code f} is changed,
204  * then its value is adjusted to be as close as possible to its expected value.
205  * A smaller field represents a smaller unit of time. {@code HOUR} is a
206  * smaller field than {@code DAY_OF_MONTH}. No adjustment is made to
207  * smaller fields that are not expected to be invariant. The calendar system
208  * determines what fields are expected to be invariant.
209  * </blockquote>
210  *
211  * <p>
212  * In addition, unlike {@code set()}, {@code add()} forces an
213  * immediate recomputation of the calendar's milliseconds and all fields.
214  *
215  * <p>
216  * <em>Example</em>: Consider a {@code GregorianCalendar} originally
217  * set to August 31, 1999. Calling {@code add(Calendar.MONTH, 13)} sets
218  * the calendar to September 30, 2000. <strong>Add rule 1</strong> sets the
219  * {@code MONTH} field to September, since adding 13 months to August
220  * gives September of the next year. Since {@code DAY_OF_MONTH} cannot be
221  * 31 in September in a {@code GregorianCalendar}, <strong>add rule 2</strong>
222  * sets the {@code DAY_OF_MONTH} to 30, the closest possible value.
223  * Although it is a smaller field, {@code DAY_OF_WEEK} is not adjusted by
224  * rule 2, since it is expected to change when the month changes in a
225  * {@code GregorianCalendar}.
226  *
227  * <p>
228  * <strong>{@code roll(f, delta)}</strong> adds {@code delta} to
229  * field {@code f} without changing larger fields. This is equivalent to
230  * calling {@code add(f, delta)} with the following adjustment:
231  *
232  * <blockquote>
233  * <p>
234  * <strong>Roll rule</strong>. Larger fields are unchanged after the call. A
235  * larger field represents a larger unit of time. {@code DAY_OF_MONTH} is
236  * a larger field than {@code HOUR}.
237  * </blockquote>
238  *
239  * <p>
240  * <em>Example</em>: Consider a {@code GregorianCalendar} originally
241  * set to August 31, 1999. Calling <code>roll(Calendar.MONTH,
242  * 8)</code> sets
243  * the calendar to April 30, <strong>1999</strong>. Add rule 1 sets the
244  * {@code MONTH} field to April. Using a {@code GregorianCalendar},
245  * the {@code DAY_OF_MONTH} cannot be 31 in the month April. Add rule 2
246  * sets it to the closest possible value, 30. Finally, the <strong>roll rule</strong>
247  * maintains the {@code YEAR} field value of 1999.
248  *
249  * <p>
250  * <em>Example</em>: Consider a {@code GregorianCalendar} originally
251  * set to Sunday June 6, 1999. Calling
252  * {@code roll(Calendar.WEEK_OF_MONTH, -1)} sets the calendar to Tuesday
253  * June 1, 1999, whereas calling {@code add(Calendar.WEEK_OF_MONTH, -1)}
254  * sets the calendar to Sunday May 30, 1999. This is because the roll rule
255  * imposes an additional constraint: The {@code MONTH} must not change
256  * when the {@code WEEK_OF_MONTH} is rolled. Taken together with add rule
257  * 1, the resultant date must be between Tuesday June 1 and Saturday June 5.
258  * According to add rule 2, the {@code DAY_OF_WEEK}, an invariant when
259  * changing the {@code WEEK_OF_MONTH}, is set to Tuesday, the closest
260  * possible value to Sunday (where Sunday is the first day of the week).
261  *
262  * <p>
263  * <strong>Usage model</strong>. To motivate the behavior of {@code add()}
264  * and {@code roll()}, consider a user interface component with
265  * increment and decrement buttons for the month, day, and year, and an
266  * underlying {@code GregorianCalendar}. If the interface reads January
267  * 31, 1999 and the user presses the month increment button, what should it
268  * read? If the underlying implementation uses {@code set()}, it might
269  * read March 3, 1999. A better result would be February 28, 1999. Furthermore,
270  * if the user presses the month increment button again, it should read March
271  * 31, 1999, not March 28, 1999. By saving the original date and using either
272  * {@code add()} or {@code roll()}, depending on whether larger
273  * fields should be affected, the user interface can behave as most users will
274  * intuitively expect.
275  *
276  * <p>
277  * <b>Note:</b> You should always use {@code roll} and {@code add} rather than
278  * attempting to perform arithmetic operations directly on the fields of a
279  * <tt>Calendar</tt>. It is quite possible for <tt>Calendar</tt> subclasses
280  * to have fields with non-linear behavior, for example missing months or days
281  * during non-leap years. The subclasses' <tt>add</tt> and <tt>roll</tt>
282  * methods will take this into account, while simple arithmetic manipulations
283  * may give invalid results.
284  *
285  * @see Date
286  * @see GregorianCalendar
287  * @see TimeZone
288  */
289 public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar> {
290 
291     private static final long serialVersionUID = -1807547505821590642L;
292 
293     /**
294      * True iff the values in {@code fields[]} correspond to {@code time}. Despite the name, this
295      * is effectively "are the values in fields[] up-to-date?" --- {@code fields[]} may contain
296      * non-zero values and {@code isSet[]} may contain {@code true} values even when
297      * {@code areFieldsSet} is false.
298      * Accessing the fields via {@code get} will ensure the fields are up-to-date.
299      */
300     protected boolean areFieldsSet;
301 
302     /**
303      * Contains broken-down field values for the current value of {@code time} if
304      * {@code areFieldsSet} is true, or stale data corresponding to some previous value otherwise.
305      * Accessing the fields via {@code get} will ensure the fields are up-to-date.
306      * The array length is always {@code FIELD_COUNT}.
307      */
308     protected int[] fields;
309 
310     /**
311      * Whether the corresponding element in {@code field[]} has been set. Initially, these are all
312      * false. The first time the fields are computed, these are set to true and remain set even if
313      * the data becomes stale: you <i>must</i> check {@code areFieldsSet} if you want to know
314      * whether the value is up-to-date.
315      * Note that {@code isSet} is <i>not</i> a safe alternative to accessing this array directly,
316      * and will likewise return stale data!
317      * The array length is always {@code FIELD_COUNT}.
318      */
319     protected boolean[] isSet;
320 
321     /**
322      * Whether {@code time} corresponds to the values in {@code fields[]}. If false, {@code time}
323      * is out-of-date with respect to changes {@code fields[]}.
324      * Accessing the time via {@code getTimeInMillis} will always return the correct value.
325      */
326     protected boolean isTimeSet;
327 
328     /**
329      * A time in milliseconds since January 1, 1970. See {@code isTimeSet}.
330      * Accessing the time via {@code getTimeInMillis} will always return the correct value.
331      */
332     protected long time;
333 
334     transient int lastTimeFieldSet;
335 
336     transient int lastDateFieldSet;
337 
338     private boolean lenient;
339 
340     private int firstDayOfWeek;
341 
342     private int minimalDaysInFirstWeek;
343 
344     private TimeZone zone;
345 
346     /**
347      * Value of the {@code MONTH} field indicating the first month of the
348      * year.
349      */
350     public static final int JANUARY = 0;
351 
352     /**
353      * Value of the {@code MONTH} field indicating the second month of
354      * the year.
355      */
356     public static final int FEBRUARY = 1;
357 
358     /**
359      * Value of the {@code MONTH} field indicating the third month of the
360      * year.
361      */
362     public static final int MARCH = 2;
363 
364     /**
365      * Value of the {@code MONTH} field indicating the fourth month of
366      * the year.
367      */
368     public static final int APRIL = 3;
369 
370     /**
371      * Value of the {@code MONTH} field indicating the fifth month of the
372      * year.
373      */
374     public static final int MAY = 4;
375 
376     /**
377      * Value of the {@code MONTH} field indicating the sixth month of the
378      * year.
379      */
380     public static final int JUNE = 5;
381 
382     /**
383      * Value of the {@code MONTH} field indicating the seventh month of
384      * the year.
385      */
386     public static final int JULY = 6;
387 
388     /**
389      * Value of the {@code MONTH} field indicating the eighth month of
390      * the year.
391      */
392     public static final int AUGUST = 7;
393 
394     /**
395      * Value of the {@code MONTH} field indicating the ninth month of the
396      * year.
397      */
398     public static final int SEPTEMBER = 8;
399 
400     /**
401      * Value of the {@code MONTH} field indicating the tenth month of the
402      * year.
403      */
404     public static final int OCTOBER = 9;
405 
406     /**
407      * Value of the {@code MONTH} field indicating the eleventh month of
408      * the year.
409      */
410     public static final int NOVEMBER = 10;
411 
412     /**
413      * Value of the {@code MONTH} field indicating the twelfth month of
414      * the year.
415      */
416     public static final int DECEMBER = 11;
417 
418     /**
419      * Value of the {@code MONTH} field indicating the thirteenth month
420      * of the year. Although {@code GregorianCalendar} does not use this
421      * value, lunar calendars do.
422      */
423     public static final int UNDECIMBER = 12;
424 
425     /**
426      * Value of the {@code DAY_OF_WEEK} field indicating Sunday.
427      */
428     public static final int SUNDAY = 1;
429 
430     /**
431      * Value of the {@code DAY_OF_WEEK} field indicating Monday.
432      */
433     public static final int MONDAY = 2;
434 
435     /**
436      * Value of the {@code DAY_OF_WEEK} field indicating Tuesday.
437      */
438     public static final int TUESDAY = 3;
439 
440     /**
441      * Value of the {@code DAY_OF_WEEK} field indicating Wednesday.
442      */
443     public static final int WEDNESDAY = 4;
444 
445     /**
446      * Value of the {@code DAY_OF_WEEK} field indicating Thursday.
447      */
448     public static final int THURSDAY = 5;
449 
450     /**
451      * Value of the {@code DAY_OF_WEEK} field indicating Friday.
452      */
453     public static final int FRIDAY = 6;
454 
455     /**
456      * Value of the {@code DAY_OF_WEEK} field indicating Saturday.
457      */
458     public static final int SATURDAY = 7;
459 
460     /**
461      * Field number for {@code get} and {@code set} indicating the
462      * era, e.g., AD or BC in the Julian calendar. This is a calendar-specific
463      * value; see subclass documentation.
464      *
465      * @see GregorianCalendar#AD
466      * @see GregorianCalendar#BC
467      */
468     public static final int ERA = 0;
469 
470     /**
471      * Field number for {@code get} and {@code set} indicating the
472      * year. This is a calendar-specific value; see subclass documentation.
473      */
474     public static final int YEAR = 1;
475 
476     /**
477      * Field number for {@code get} and {@code set} indicating the
478      * month. This is a calendar-specific value. The first month of the year is
479      * {@code JANUARY}; the last depends on the number of months in a
480      * year.
481      *
482      * @see #JANUARY
483      * @see #FEBRUARY
484      * @see #MARCH
485      * @see #APRIL
486      * @see #MAY
487      * @see #JUNE
488      * @see #JULY
489      * @see #AUGUST
490      * @see #SEPTEMBER
491      * @see #OCTOBER
492      * @see #NOVEMBER
493      * @see #DECEMBER
494      * @see #UNDECIMBER
495      */
496     public static final int MONTH = 2;
497 
498     /**
499      * Field number for {@code get} and {@code set} indicating the
500      * week number within the current year. The first week of the year, as
501      * defined by {@code getFirstDayOfWeek()} and
502      * {@code getMinimalDaysInFirstWeek()}, has value 1. Subclasses
503      * define the value of {@code WEEK_OF_YEAR} for days before the first
504      * week of the year.
505      *
506      * @see #getFirstDayOfWeek
507      * @see #getMinimalDaysInFirstWeek
508      */
509     public static final int WEEK_OF_YEAR = 3;
510 
511     /**
512      * Field number for {@code get} and {@code set} indicating the
513      * week number within the current month. The first week of the month, as
514      * defined by {@code getFirstDayOfWeek()} and
515      * {@code getMinimalDaysInFirstWeek()}, has value 1. Subclasses
516      * define the value of {@code WEEK_OF_MONTH} for days before the
517      * first week of the month.
518      *
519      * @see #getFirstDayOfWeek
520      * @see #getMinimalDaysInFirstWeek
521      */
522     public static final int WEEK_OF_MONTH = 4;
523 
524     /**
525      * Field number for {@code get} and {@code set} indicating the
526      * day of the month. This is a synonym for {@code DAY_OF_MONTH}. The
527      * first day of the month has value 1.
528      *
529      * @see #DAY_OF_MONTH
530      */
531     public static final int DATE = 5;
532 
533     /**
534      * Field number for {@code get} and {@code set} indicating the
535      * day of the month. This is a synonym for {@code DATE}. The first
536      * day of the month has value 1.
537      *
538      * @see #DATE
539      */
540     public static final int DAY_OF_MONTH = 5;
541 
542     /**
543      * Field number for {@code get} and {@code set} indicating the
544      * day number within the current year. The first day of the year has value
545      * 1.
546      */
547     public static final int DAY_OF_YEAR = 6;
548 
549     /**
550      * Field number for {@code get} and {@code set} indicating the
551      * day of the week. This field takes values {@code SUNDAY},
552      * {@code MONDAY}, {@code TUESDAY}, {@code WEDNESDAY},
553      * {@code THURSDAY}, {@code FRIDAY}, and
554      * {@code SATURDAY}.
555      *
556      * @see #SUNDAY
557      * @see #MONDAY
558      * @see #TUESDAY
559      * @see #WEDNESDAY
560      * @see #THURSDAY
561      * @see #FRIDAY
562      * @see #SATURDAY
563      */
564     public static final int DAY_OF_WEEK = 7;
565 
566     /**
567      * Field number for {@code get} and {@code set} indicating the
568      * ordinal number of the day of the week within the current month. Together
569      * with the {@code DAY_OF_WEEK} field, this uniquely specifies a day
570      * within a month. Unlike {@code WEEK_OF_MONTH} and
571      * {@code WEEK_OF_YEAR}, this field's value does <em>not</em>
572      * depend on {@code getFirstDayOfWeek()} or
573      * {@code getMinimalDaysInFirstWeek()}. {@code DAY_OF_MONTH 1}
574      * through {@code 7} always correspond to <code>DAY_OF_WEEK_IN_MONTH
575      * 1</code>;
576      * {@code 8} through {@code 15} correspond to
577      * {@code DAY_OF_WEEK_IN_MONTH 2}, and so on.
578      * {@code DAY_OF_WEEK_IN_MONTH 0} indicates the week before
579      * {@code DAY_OF_WEEK_IN_MONTH 1}. Negative values count back from
580      * the end of the month, so the last Sunday of a month is specified as
581      * {@code DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1}. Because
582      * negative values count backward they will usually be aligned differently
583      * within the month than positive values. For example, if a month has 31
584      * days, {@code DAY_OF_WEEK_IN_MONTH -1} will overlap
585      * {@code DAY_OF_WEEK_IN_MONTH 5} and the end of {@code 4}.
586      *
587      * @see #DAY_OF_WEEK
588      * @see #WEEK_OF_MONTH
589      */
590     public static final int DAY_OF_WEEK_IN_MONTH = 8;
591 
592     /**
593      * Field number for {@code get} and {@code set} indicating
594      * whether the {@code HOUR} is before or after noon. E.g., at
595      * 10:04:15.250 PM the {@code AM_PM} is {@code PM}.
596      *
597      * @see #AM
598      * @see #PM
599      * @see #HOUR
600      */
601     public static final int AM_PM = 9;
602 
603     /**
604      * Field number for {@code get} and {@code set} indicating the
605      * hour of the morning or afternoon. {@code HOUR} is used for the
606      * 12-hour clock. E.g., at 10:04:15.250 PM the {@code HOUR} is 10.
607      *
608      * @see #AM_PM
609      * @see #HOUR_OF_DAY
610      */
611     public static final int HOUR = 10;
612 
613     /**
614      * Field number for {@code get} and {@code set} indicating the
615      * hour of the day. {@code HOUR_OF_DAY} is used for the 24-hour
616      * clock. E.g., at 10:04:15.250 PM the {@code HOUR_OF_DAY} is 22.
617      *
618      * @see #HOUR
619      */
620     public static final int HOUR_OF_DAY = 11;
621 
622     /**
623      * Field number for {@code get} and {@code set} indicating the
624      * minute within the hour. E.g., at 10:04:15.250 PM the {@code MINUTE}
625      * is 4.
626      */
627     public static final int MINUTE = 12;
628 
629     /**
630      * Field number for {@code get} and {@code set} indicating the
631      * second within the minute. E.g., at 10:04:15.250 PM the
632      * {@code SECOND} is 15.
633      */
634     public static final int SECOND = 13;
635 
636     /**
637      * Field number for {@code get} and {@code set} indicating the
638      * millisecond within the second. E.g., at 10:04:15.250 PM the
639      * {@code MILLISECOND} is 250.
640      */
641     public static final int MILLISECOND = 14;
642 
643     /**
644      * Field number for {@code get} and {@code set} indicating the
645      * raw offset from GMT in milliseconds.
646      */
647     public static final int ZONE_OFFSET = 15;
648 
649     /**
650      * Field number for {@code get} and {@code set} indicating the
651      * daylight savings offset in milliseconds.
652      */
653     public static final int DST_OFFSET = 16;
654 
655     /**
656      * This is the total number of fields in this calendar.
657      */
658     public static final int FIELD_COUNT = 17;
659 
660     /**
661      * Value of the {@code AM_PM} field indicating the period of the day
662      * from midnight to just before noon.
663      */
664     public static final int AM = 0;
665 
666     /**
667      * Value of the {@code AM_PM} field indicating the period of the day
668      * from noon to just before midnight.
669      */
670     public static final int PM = 1;
671 
672     /**
673      * Requests both {@code SHORT} and {@code LONG} styles in the map returned by
674      * {@link #getDisplayNames}.
675      * @since 1.6
676      */
677     public static final int ALL_STYLES = 0;
678 
679     /**
680      * Requests short names (such as "Jan") from
681      * {@link #getDisplayName} or {@link #getDisplayNames}.
682      * @since 1.6
683      */
684     public static final int SHORT = 1;
685 
686     /**
687      * Requests long names (such as "January") from
688      * {@link #getDisplayName} or {@link #getDisplayNames}.
689      * @since 1.6
690      */
691     public static final int LONG = 2;
692 
693     private static final String[] FIELD_NAMES = { "ERA", "YEAR", "MONTH",
694             "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DAY_OF_MONTH", "DAY_OF_YEAR",
695             "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR",
696             "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND",
697             "ZONE_OFFSET", "DST_OFFSET" };
698 
699     /**
700      * Constructs a {@code Calendar} instance using the default {@code TimeZone} and {@code Locale}.
701      */
Calendar()702     protected Calendar() {
703         this(TimeZone.getDefault(), Locale.getDefault());
704     }
705 
Calendar(TimeZone timezone)706     Calendar(TimeZone timezone) {
707         fields = new int[FIELD_COUNT];
708         isSet = new boolean[FIELD_COUNT];
709         areFieldsSet = isTimeSet = false;
710         setLenient(true);
711         setTimeZone(timezone);
712     }
713 
714     /**
715      * Constructs a {@code Calendar} instance using the specified {@code TimeZone} and {@code Locale}.
716      *
717      * @param timezone
718      *            the timezone.
719      * @param locale
720      *            the locale.
721      */
Calendar(TimeZone timezone, Locale locale)722     protected Calendar(TimeZone timezone, Locale locale) {
723         this(timezone);
724         LocaleData localeData = LocaleData.get(locale);
725         setFirstDayOfWeek(localeData.firstDayOfWeek.intValue());
726         setMinimalDaysInFirstWeek(localeData.minimalDaysInFirstWeek.intValue());
727     }
728 
729 
730     /**
731      * Adds the specified amount to a {@code Calendar} field.
732      *
733      * @param field
734      *            the {@code Calendar} field to modify.
735      * @param value
736      *            the amount to add to the field.
737      * @throws IllegalArgumentException
738      *                if {@code field} is {@code DST_OFFSET} or {@code
739      *                ZONE_OFFSET}.
740      */
add(int field, int value)741     public abstract void add(int field, int value);
742 
743     /**
744      * Returns whether the {@code Date} specified by this {@code Calendar} instance is after the {@code Date}
745      * specified by the parameter. The comparison is not dependent on the time
746      * zones of the {@code Calendar}.
747      *
748      * @param calendar
749      *            the {@code Calendar} instance to compare.
750      * @return {@code true} when this Calendar is after calendar, {@code false} otherwise.
751      * @throws IllegalArgumentException
752      *                if the time is not set and the time cannot be computed
753      *                from the current field values.
754      */
after(Object calendar)755     public boolean after(Object calendar) {
756         if (!(calendar instanceof Calendar)) {
757             return false;
758         }
759         return getTimeInMillis() > ((Calendar) calendar).getTimeInMillis();
760     }
761 
762     /**
763      * Returns whether the {@code Date} specified by this {@code Calendar} instance is before the
764      * {@code Date} specified by the parameter. The comparison is not dependent on the
765      * time zones of the {@code Calendar}.
766      *
767      * @param calendar
768      *            the {@code Calendar} instance to compare.
769      * @return {@code true} when this Calendar is before calendar, {@code false} otherwise.
770      * @throws IllegalArgumentException
771      *                if the time is not set and the time cannot be computed
772      *                from the current field values.
773      */
before(Object calendar)774     public boolean before(Object calendar) {
775         if (!(calendar instanceof Calendar)) {
776             return false;
777         }
778         return getTimeInMillis() < ((Calendar) calendar).getTimeInMillis();
779     }
780 
781     /**
782      * Clears all of the fields of this {@code Calendar}. All fields are initialized to
783      * zero.
784      */
clear()785     public final void clear() {
786         for (int i = 0; i < FIELD_COUNT; i++) {
787             fields[i] = 0;
788             isSet[i] = false;
789         }
790         areFieldsSet = isTimeSet = false;
791     }
792 
793     /**
794      * Clears the specified field to zero and sets the isSet flag to {@code false}.
795      *
796      * @param field
797      *            the field to clear.
798      */
clear(int field)799     public final void clear(int field) {
800         fields[field] = 0;
801         isSet[field] = false;
802         areFieldsSet = isTimeSet = false;
803     }
804 
805     /**
806      * Returns a new {@code Calendar} with the same properties.
807      *
808      * @return a shallow copy of this {@code Calendar}.
809      *
810      * @see java.lang.Cloneable
811      */
812     @Override
clone()813     public Object clone() {
814         try {
815             Calendar clone = (Calendar) super.clone();
816             clone.fields = fields.clone();
817             clone.isSet = isSet.clone();
818             clone.zone = (TimeZone) zone.clone();
819             return clone;
820         } catch (CloneNotSupportedException e) {
821             throw new AssertionError(e);
822         }
823     }
824 
825     /**
826      * Computes the time from the fields if the time has not already been set.
827      * Computes the fields from the time if the fields are not already set.
828      *
829      * @throws IllegalArgumentException
830      *                if the time is not set and the time cannot be computed
831      *                from the current field values.
832      */
complete()833     protected void complete() {
834         if (!isTimeSet) {
835             computeTime();
836             isTimeSet = true;
837         }
838         if (!areFieldsSet) {
839             computeFields();
840             areFieldsSet = true;
841         }
842     }
843 
844     /**
845      * Computes the {@code Calendar} fields from {@code time}.
846      */
computeFields()847     protected abstract void computeFields();
848 
849     /**
850      * Computes {@code time} from the Calendar fields.
851      *
852      * @throws IllegalArgumentException
853      *                if the time cannot be computed from the current field
854      *                values.
855      */
computeTime()856     protected abstract void computeTime();
857 
858     /**
859      * Compares the specified object to this {@code Calendar} and returns whether they are
860      * equal. The object must be an instance of {@code Calendar} and have the same
861      * properties.
862      *
863      * @param object
864      *            the object to compare with this object.
865      * @return {@code true} if the specified object is equal to this {@code Calendar}, {@code false}
866      *         otherwise.
867      */
868     @Override
equals(Object object)869     public boolean equals(Object object) {
870         if (this == object) {
871             return true;
872         }
873         if (!(object instanceof Calendar)) {
874             return false;
875         }
876         Calendar cal = (Calendar) object;
877         return getTimeInMillis() == cal.getTimeInMillis()
878                 && isLenient() == cal.isLenient()
879                 && getFirstDayOfWeek() == cal.getFirstDayOfWeek()
880                 && getMinimalDaysInFirstWeek() == cal
881                         .getMinimalDaysInFirstWeek()
882                 && getTimeZone().equals(cal.getTimeZone());
883     }
884 
885     /**
886      * Gets the value of the specified field after computing the field values by
887      * calling {@code complete()} first.
888      *
889      * @param field
890      *            the field to get.
891      * @return the value of the specified field.
892      *
893      * @throws IllegalArgumentException
894      *                if the fields are not set, the time is not set, and the
895      *                time cannot be computed from the current field values.
896      * @throws ArrayIndexOutOfBoundsException
897      *                if the field is not inside the range of possible fields.
898      *                The range is starting at 0 up to {@code FIELD_COUNT}.
899      */
get(int field)900     public int get(int field) {
901         complete();
902         return fields[field];
903     }
904 
905     /**
906      * Gets the maximum value of the specified field for the current date.
907      *
908      * @param field
909      *            the field.
910      * @return the maximum value of the specified field.
911      */
getActualMaximum(int field)912     public int getActualMaximum(int field) {
913         int value, next;
914         if (getMaximum(field) == (next = getLeastMaximum(field))) {
915             return next;
916         }
917         complete();
918         long orgTime = time;
919         set(field, next);
920         do {
921             value = next;
922             roll(field, true);
923             next = get(field);
924         } while (next > value);
925         time = orgTime;
926         areFieldsSet = false;
927         return value;
928     }
929 
930     /**
931      * Gets the minimum value of the specified field for the current date.
932      *
933      * @param field
934      *            the field.
935      * @return the minimum value of the specified field.
936      */
getActualMinimum(int field)937     public int getActualMinimum(int field) {
938         int value, next;
939         if (getMinimum(field) == (next = getGreatestMinimum(field))) {
940             return next;
941         }
942         complete();
943         long orgTime = time;
944         set(field, next);
945         do {
946             value = next;
947             roll(field, false);
948             next = get(field);
949         } while (next < value);
950         time = orgTime;
951         areFieldsSet = false;
952         return value;
953     }
954 
955     /**
956      * Returns an array of locales for which custom {@code Calendar} instances
957      * are available.
958      * <p>Note that Android does not support user-supplied locale service providers.
959      */
getAvailableLocales()960     public static synchronized Locale[] getAvailableLocales() {
961         return ICU.getAvailableCalendarLocales();
962     }
963 
964     /**
965      * Gets the first day of the week for this {@code Calendar}.
966      *
967      * @return the first day of the week.
968      */
getFirstDayOfWeek()969     public int getFirstDayOfWeek() {
970         return firstDayOfWeek;
971     }
972 
973     /**
974      * Gets the greatest minimum value of the specified field. This is the
975      * biggest value that {@code getActualMinimum} can return for any possible
976      * time.
977      *
978      * @param field
979      *            the field.
980      * @return the greatest minimum value of the specified field.
981      */
getGreatestMinimum(int field)982     public abstract int getGreatestMinimum(int field);
983 
984     /**
985      * Constructs a new instance of the {@code Calendar} subclass appropriate for the
986      * default {@code Locale}.
987      *
988      * @return a {@code Calendar} subclass instance set to the current date and time in
989      *         the default {@code Timezone}.
990      */
getInstance()991     public static synchronized Calendar getInstance() {
992         return new GregorianCalendar();
993     }
994 
995     /**
996      * Constructs a new instance of the {@code Calendar} subclass appropriate for the
997      * specified {@code Locale}.
998      *
999      * @param locale
1000      *            the locale to use.
1001      * @return a {@code Calendar} subclass instance set to the current date and time.
1002      */
getInstance(Locale locale)1003     public static synchronized Calendar getInstance(Locale locale) {
1004         return new GregorianCalendar(locale);
1005     }
1006 
1007     /**
1008      * Constructs a new instance of the {@code Calendar} subclass appropriate for the
1009      * default {@code Locale}, using the specified {@code TimeZone}.
1010      *
1011      * @param timezone
1012      *            the {@code TimeZone} to use.
1013      * @return a {@code Calendar} subclass instance set to the current date and time in
1014      *         the specified timezone.
1015      */
getInstance(TimeZone timezone)1016     public static synchronized Calendar getInstance(TimeZone timezone) {
1017         return new GregorianCalendar(timezone);
1018     }
1019 
1020     /**
1021      * Constructs a new instance of the {@code Calendar} subclass appropriate for the
1022      * specified {@code Locale}.
1023      *
1024      * @param timezone
1025      *            the {@code TimeZone} to use.
1026      * @param locale
1027      *            the {@code Locale} to use.
1028      * @return a {@code Calendar} subclass instance set to the current date and time in
1029      *         the specified timezone.
1030      */
getInstance(TimeZone timezone, Locale locale)1031     public static synchronized Calendar getInstance(TimeZone timezone, Locale locale) {
1032         return new GregorianCalendar(timezone, locale);
1033     }
1034 
1035     /**
1036      * Gets the smallest maximum value of the specified field. This is the
1037      * smallest value that {@code getActualMaximum()} can return for any
1038      * possible time.
1039      *
1040      * @param field
1041      *            the field number.
1042      * @return the smallest maximum value of the specified field.
1043      */
getLeastMaximum(int field)1044     public abstract int getLeastMaximum(int field);
1045 
1046     /**
1047      * Gets the greatest maximum value of the specified field. This returns the
1048      * biggest value that {@code get} can return for the specified field.
1049      *
1050      * @param field
1051      *            the field.
1052      * @return the greatest maximum value of the specified field.
1053      */
getMaximum(int field)1054     public abstract int getMaximum(int field);
1055 
1056     /**
1057      * Gets the minimal days in the first week of the year.
1058      *
1059      * @return the minimal days in the first week of the year.
1060      */
getMinimalDaysInFirstWeek()1061     public int getMinimalDaysInFirstWeek() {
1062         return minimalDaysInFirstWeek;
1063     }
1064 
1065     /**
1066      * Gets the smallest minimum value of the specified field. this returns the
1067      * smallest value thet {@code get} can return for the specified field.
1068      *
1069      * @param field
1070      *            the field number.
1071      * @return the smallest minimum value of the specified field.
1072      */
getMinimum(int field)1073     public abstract int getMinimum(int field);
1074 
1075     /**
1076      * Gets the time of this {@code Calendar} as a {@code Date} object.
1077      *
1078      * @return a new {@code Date} initialized to the time of this {@code Calendar}.
1079      *
1080      * @throws IllegalArgumentException
1081      *                if the time is not set and the time cannot be computed
1082      *                from the current field values.
1083      */
getTime()1084     public final Date getTime() {
1085         return new Date(getTimeInMillis());
1086     }
1087 
1088     /**
1089      * Computes the time from the fields if required and returns the time.
1090      *
1091      * @return the time of this {@code Calendar}.
1092      *
1093      * @throws IllegalArgumentException
1094      *                if the time is not set and the time cannot be computed
1095      *                from the current field values.
1096      */
getTimeInMillis()1097     public long getTimeInMillis() {
1098         if (!isTimeSet) {
1099             computeTime();
1100             isTimeSet = true;
1101         }
1102         return time;
1103     }
1104 
1105     /**
1106      * Gets the timezone of this {@code Calendar}.
1107      *
1108      * @return the {@code TimeZone} used by this {@code Calendar}.
1109      */
getTimeZone()1110     public TimeZone getTimeZone() {
1111         return zone;
1112     }
1113 
1114     /**
1115      * Returns an integer hash code for the receiver. Objects which are equal
1116      * return the same value for this method.
1117      *
1118      * @return the receiver's hash.
1119      *
1120      * @see #equals
1121      */
1122     @Override
hashCode()1123     public int hashCode() {
1124         return (isLenient() ? 1237 : 1231) + getFirstDayOfWeek()
1125                 + getMinimalDaysInFirstWeek() + getTimeZone().hashCode();
1126     }
1127 
1128     /**
1129      * Gets the value of the specified field without recomputing.
1130      *
1131      * @param field
1132      *            the field.
1133      * @return the value of the specified field.
1134      */
internalGet(int field)1135     protected final int internalGet(int field) {
1136         return fields[field];
1137     }
1138 
1139     /**
1140      * Returns if this {@code Calendar} accepts field values which are outside the valid
1141      * range for the field.
1142      *
1143      * @return {@code true} if this {@code Calendar} is lenient, {@code false} otherwise.
1144      */
isLenient()1145     public boolean isLenient() {
1146         return lenient;
1147     }
1148 
1149     /**
1150      * Returns whether the specified field is set. Note that the interpretation of "is set" is
1151      * somewhat technical. In particular, it does <i>not</i> mean that the field's value is up
1152      * to date. If you want to know whether a field contains an up-to-date value, you must also
1153      * check {@code areFieldsSet}, making this method somewhat useless unless you're a subclass,
1154      * in which case you can access the {@code isSet} array directly.
1155      * <p>
1156      * A field remains "set" from the first time its value is computed until it's cleared by one
1157      * of the {@code clear} methods. Thus "set" does not mean "valid". You probably want to call
1158      * {@code get} -- which will update fields as necessary -- rather than try to make use of
1159      * this method.
1160      *
1161      * @param field
1162      *            a {@code Calendar} field number.
1163      * @return {@code true} if the specified field is set, {@code false} otherwise.
1164      */
isSet(int field)1165     public final boolean isSet(int field) {
1166         return isSet[field];
1167     }
1168 
1169     /**
1170      * Adds the specified amount to the specified field and wraps the value of
1171      * the field when it goes beyond the maximum or minimum value for the
1172      * current date. Other fields will be adjusted as required to maintain a
1173      * consistent date.
1174      *
1175      * @param field
1176      *            the field to roll.
1177      * @param value
1178      *            the amount to add.
1179      */
roll(int field, int value)1180     public void roll(int field, int value) {
1181         boolean increment = value >= 0;
1182         int count = increment ? value : -value;
1183         for (int i = 0; i < count; i++) {
1184             roll(field, increment);
1185         }
1186     }
1187 
1188     /**
1189      * Increment or decrement the specified field and wrap the value of the
1190      * field when it goes beyond the maximum or minimum value for the current
1191      * date. Other fields will be adjusted as required to maintain a consistent
1192      * date.
1193      *
1194      * @param field
1195      *            the number indicating the field to roll.
1196      * @param increment
1197      *            {@code true} to increment the field, {@code false} to decrement.
1198      */
roll(int field, boolean increment)1199     public abstract void roll(int field, boolean increment);
1200 
1201     /**
1202      * Sets a field to the specified value.
1203      *
1204      * @param field
1205      *            the code indicating the {@code Calendar} field to modify.
1206      * @param value
1207      *            the value.
1208      */
set(int field, int value)1209     public void set(int field, int value) {
1210         fields[field] = value;
1211         isSet[field] = true;
1212         areFieldsSet = isTimeSet = false;
1213         if (field > MONTH && field < AM_PM) {
1214             lastDateFieldSet = field;
1215         }
1216         if (field == HOUR || field == HOUR_OF_DAY) {
1217             lastTimeFieldSet = field;
1218         }
1219         if (field == AM_PM) {
1220             lastTimeFieldSet = HOUR;
1221         }
1222     }
1223 
1224     /**
1225      * Sets the year, month and day of the month fields. Other fields are not
1226      * changed.
1227      *
1228      * @param year
1229      *            the year.
1230      * @param month
1231      *            the month.
1232      * @param day
1233      *            the day of the month.
1234      */
set(int year, int month, int day)1235     public final void set(int year, int month, int day) {
1236         set(YEAR, year);
1237         set(MONTH, month);
1238         set(DATE, day);
1239     }
1240 
1241     /**
1242      * Sets the year, month, day of the month, hour of day and minute fields.
1243      * Other fields are not changed.
1244      *
1245      * @param year
1246      *            the year.
1247      * @param month
1248      *            the month.
1249      * @param day
1250      *            the day of the month.
1251      * @param hourOfDay
1252      *            the hour of day.
1253      * @param minute
1254      *            the minute.
1255      */
set(int year, int month, int day, int hourOfDay, int minute)1256     public final void set(int year, int month, int day, int hourOfDay,
1257             int minute) {
1258         set(year, month, day);
1259         set(HOUR_OF_DAY, hourOfDay);
1260         set(MINUTE, minute);
1261     }
1262 
1263     /**
1264      * Sets the year, month, day of the month, hour of day, minute and second
1265      * fields. Other fields are not changed.
1266      *
1267      * @param year
1268      *            the year.
1269      * @param month
1270      *            the month.
1271      * @param day
1272      *            the day of the month.
1273      * @param hourOfDay
1274      *            the hour of day.
1275      * @param minute
1276      *            the minute.
1277      * @param second
1278      *            the second.
1279      */
set(int year, int month, int day, int hourOfDay, int minute, int second)1280     public final void set(int year, int month, int day, int hourOfDay,
1281             int minute, int second) {
1282         set(year, month, day, hourOfDay, minute);
1283         set(SECOND, second);
1284     }
1285 
1286     /**
1287      * Sets the first day of the week for this {@code Calendar}.
1288      *
1289      * @param value
1290      *            a {@code Calendar} day of the week.
1291      */
setFirstDayOfWeek(int value)1292     public void setFirstDayOfWeek(int value) {
1293         firstDayOfWeek = value;
1294     }
1295 
1296     /**
1297      * Sets this {@code Calendar} to accept field values which are outside the valid
1298      * range for the field.
1299      *
1300      * @param value
1301      *            a boolean value.
1302      */
setLenient(boolean value)1303     public void setLenient(boolean value) {
1304         lenient = value;
1305     }
1306 
1307     /**
1308      * Sets the minimal days in the first week of the year.
1309      *
1310      * @param value
1311      *            the minimal days in the first week of the year.
1312      */
setMinimalDaysInFirstWeek(int value)1313     public void setMinimalDaysInFirstWeek(int value) {
1314         minimalDaysInFirstWeek = value;
1315     }
1316 
1317     /**
1318      * Sets the time of this {@code Calendar}.
1319      *
1320      * @param date
1321      *            a {@code Date} object.
1322      */
setTime(Date date)1323     public final void setTime(Date date) {
1324         setTimeInMillis(date.getTime());
1325     }
1326 
1327     /**
1328      * Sets the time of this {@code Calendar}.
1329      *
1330      * @param milliseconds
1331      *            the time as the number of milliseconds since Jan. 1, 1970.
1332      */
setTimeInMillis(long milliseconds)1333     public void setTimeInMillis(long milliseconds) {
1334         if (!isTimeSet || !areFieldsSet || time != milliseconds) {
1335             time = milliseconds;
1336             isTimeSet = true;
1337             areFieldsSet = false;
1338             complete();
1339         }
1340     }
1341 
1342     /**
1343      * Sets the {@code TimeZone} used by this Calendar.
1344      *
1345      * @param timezone
1346      *            a {@code TimeZone}.
1347      */
setTimeZone(TimeZone timezone)1348     public void setTimeZone(TimeZone timezone) {
1349         zone = timezone;
1350         areFieldsSet = false;
1351     }
1352 
1353     /**
1354      * Returns the string representation of this {@code Calendar}.
1355      */
1356     @Override
toString()1357     public String toString() {
1358         StringBuilder result = new StringBuilder(getClass().getName() +
1359                 "[time=" + (isTimeSet ? String.valueOf(time) : "?") +
1360                 ",areFieldsSet=" + areFieldsSet +
1361                 ",lenient=" + lenient +
1362                 ",zone=" + zone.getID() +
1363                 ",firstDayOfWeek=" + firstDayOfWeek +
1364                 ",minimalDaysInFirstWeek=" + minimalDaysInFirstWeek);
1365         for (int i = 0; i < FIELD_COUNT; i++) {
1366             result.append(',');
1367             result.append(FIELD_NAMES[i]);
1368             result.append('=');
1369             if (isSet[i]) {
1370                 result.append(fields[i]);
1371             } else {
1372                 result.append('?');
1373             }
1374         }
1375         result.append(']');
1376         return result.toString();
1377     }
1378 
1379     /**
1380      * Compares the times of the two {@code Calendar}, which represent the milliseconds
1381      * from the January 1, 1970 00:00:00.000 GMT (Gregorian).
1382      *
1383      * @param anotherCalendar
1384      *            another calendar that this one is compared with.
1385      * @return 0 if the times of the two {@code Calendar}s are equal, -1 if the time of
1386      *         this {@code Calendar} is before the other one, 1 if the time of this
1387      *         {@code Calendar} is after the other one.
1388      * @throws NullPointerException
1389      *             if the argument is null.
1390      * @throws IllegalArgumentException
1391      *             if the argument does not include a valid time
1392      *             value.
1393      */
compareTo(Calendar anotherCalendar)1394     public int compareTo(Calendar anotherCalendar) {
1395         if (anotherCalendar == null) {
1396             throw new NullPointerException();
1397         }
1398         long timeInMillis = getTimeInMillis();
1399         long anotherTimeInMillis = anotherCalendar.getTimeInMillis();
1400         if (timeInMillis > anotherTimeInMillis) {
1401             return 1;
1402         }
1403         if (timeInMillis == anotherTimeInMillis) {
1404             return 0;
1405         }
1406         return -1;
1407     }
1408 
1409     /**
1410      * Returns a human-readable string for the value of {@code field}
1411      * using the given style and locale. If no string is available, returns null.
1412      * The value is retrieved by invoking {@code get(field)}.
1413      *
1414      * <p>For example, {@code getDisplayName(MONTH, SHORT, Locale.US)} will return "Jan"
1415      * while {@code getDisplayName(MONTH, LONG, Locale.US)} will return "January".
1416      *
1417      * @param field the field
1418      * @param style {@code SHORT} or {@code LONG}
1419      * @param locale the locale
1420      * @return the display name, or null
1421      * @throws NullPointerException if {@code locale == null}
1422      * @throws IllegalArgumentException if {@code field} or {@code style} is invalid
1423      * @since 1.6
1424      */
getDisplayName(int field, int style, Locale locale)1425     public String getDisplayName(int field, int style, Locale locale) {
1426         // TODO: the RI's documentation says ALL_STYLES is invalid, but actually treats it as SHORT.
1427         if (style == ALL_STYLES) {
1428             style = SHORT;
1429         }
1430         String[] array = getDisplayNameArray(field, style, locale);
1431         int value = get(field);
1432         return (array != null) ? array[value] : null;
1433     }
1434 
getDisplayNameArray(int field, int style, Locale locale)1435     private String[] getDisplayNameArray(int field, int style, Locale locale) {
1436         if (field < 0 || field >= FIELD_COUNT) {
1437             throw new IllegalArgumentException("bad field " + field);
1438         }
1439         checkStyle(style);
1440         DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
1441         switch (field) {
1442         case AM_PM:
1443             return dfs.getAmPmStrings();
1444         case DAY_OF_WEEK:
1445             return (style == LONG) ? dfs.getWeekdays() : dfs.getShortWeekdays();
1446         case ERA:
1447             return dfs.getEras();
1448         case MONTH:
1449             return (style == LONG) ? dfs.getMonths() : dfs.getShortMonths();
1450         }
1451         return null;
1452     }
1453 
checkStyle(int style)1454     private static void checkStyle(int style) {
1455         if (style != ALL_STYLES && style != SHORT && style != LONG) {
1456             throw new IllegalArgumentException("bad style " + style);
1457         }
1458     }
1459 
1460     /**
1461      * Returns a map of human-readable strings to corresponding values,
1462      * for the given field, style, and locale.
1463      * Returns null if no strings are available.
1464      *
1465      * <p>For example, {@code getDisplayNames(MONTH, ALL_STYLES, Locale.US)} would
1466      * contain mappings from "Jan" and "January" to {@link #JANUARY}, and so on.
1467      *
1468      * @param field the field
1469      * @param style {@code SHORT}, {@code LONG}, or {@code ALL_STYLES}
1470      * @param locale the locale
1471      * @return the display name, or null
1472      * @throws NullPointerException if {@code locale == null}
1473      * @throws IllegalArgumentException if {@code field} or {@code style} is invalid
1474      * @since 1.6
1475      */
getDisplayNames(int field, int style, Locale locale)1476     public Map<String, Integer> getDisplayNames(int field, int style, Locale locale) {
1477         checkStyle(style);
1478         complete();
1479         Map<String, Integer> result = new HashMap<String, Integer>();
1480         if (style == SHORT || style == ALL_STYLES) {
1481             insertValuesInMap(result, getDisplayNameArray(field, SHORT, locale));
1482         }
1483         if (style == LONG || style == ALL_STYLES) {
1484             insertValuesInMap(result, getDisplayNameArray(field, LONG, locale));
1485         }
1486         return result.isEmpty() ? null : result;
1487     }
1488 
insertValuesInMap(Map<String, Integer> map, String[] values)1489     private static void insertValuesInMap(Map<String, Integer> map, String[] values) {
1490         if (values == null) {
1491             return;
1492         }
1493         for (int i = 0; i < values.length; ++i) {
1494             if (values[i] != null && !values[i].isEmpty()) {
1495                 map.put(values[i], i);
1496             }
1497         }
1498     }
1499 
1500     private static final ObjectStreamField[] serialPersistentFields = {
1501         new ObjectStreamField("areFieldsSet", boolean.class),
1502         new ObjectStreamField("fields", int[].class),
1503         new ObjectStreamField("firstDayOfWeek", int.class),
1504         new ObjectStreamField("isSet", boolean[].class),
1505         new ObjectStreamField("isTimeSet", boolean.class),
1506         new ObjectStreamField("lenient", boolean.class),
1507         new ObjectStreamField("minimalDaysInFirstWeek", int.class),
1508         new ObjectStreamField("nextStamp", int.class),
1509         new ObjectStreamField("serialVersionOnStream", int.class),
1510         new ObjectStreamField("time", long.class),
1511         new ObjectStreamField("zone", TimeZone.class),
1512     };
1513 
writeObject(ObjectOutputStream stream)1514     private void writeObject(ObjectOutputStream stream) throws IOException {
1515         complete();
1516         ObjectOutputStream.PutField putFields = stream.putFields();
1517         putFields.put("areFieldsSet", areFieldsSet);
1518         putFields.put("fields", this.fields);
1519         putFields.put("firstDayOfWeek", firstDayOfWeek);
1520         putFields.put("isSet", isSet);
1521         putFields.put("isTimeSet", isTimeSet);
1522         putFields.put("lenient", lenient);
1523         putFields.put("minimalDaysInFirstWeek", minimalDaysInFirstWeek);
1524         putFields.put("nextStamp", 2 /* MINIMUM_USER_STAMP */);
1525         putFields.put("serialVersionOnStream", 1);
1526         putFields.put("time", time);
1527         putFields.put("zone", zone);
1528         stream.writeFields();
1529     }
1530 
readObject(ObjectInputStream stream)1531     private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
1532         ObjectInputStream.GetField readFields = stream.readFields();
1533         areFieldsSet = readFields.get("areFieldsSet", false);
1534         this.fields = (int[]) readFields.get("fields", null);
1535         firstDayOfWeek = readFields.get("firstDayOfWeek", Calendar.SUNDAY);
1536         isSet = (boolean[]) readFields.get("isSet", null);
1537         isTimeSet = readFields.get("isTimeSet", false);
1538         lenient = readFields.get("lenient", true);
1539         minimalDaysInFirstWeek = readFields.get("minimalDaysInFirstWeek", 1);
1540         time = readFields.get("time", 0L);
1541         zone = (TimeZone) readFields.get("zone", null);
1542     }
1543 }
1544