• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 /*
28  * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
29  * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
30  *
31  *   The original version of this source code and documentation is copyrighted
32  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
33  * materials are provided under terms of a License Agreement between Taligent
34  * and Sun. This technology is protected by multiple US and International
35  * patents. This notice and attribution to Taligent may not be removed.
36  *   Taligent is a registered trademark of Taligent, Inc.
37  *
38  */
39 
40 package java.util;
41 
42 import java.io.IOException;
43 import java.io.ObjectInputStream;
44 import libcore.util.ZoneInfo;
45 import sun.util.calendar.BaseCalendar;
46 import sun.util.calendar.CalendarDate;
47 import sun.util.calendar.CalendarSystem;
48 import sun.util.calendar.CalendarUtils;
49 import sun.util.calendar.Era;
50 import sun.util.calendar.Gregorian;
51 import sun.util.calendar.JulianCalendar;
52 
53 /**
54  * <code>GregorianCalendar</code> is a concrete subclass of
55  * <code>Calendar</code> and provides the standard calendar system
56  * used by most of the world.
57  *
58  * <p> <code>GregorianCalendar</code> is a hybrid calendar that
59  * supports both the Julian and Gregorian calendar systems with the
60  * support of a single discontinuity, which corresponds by default to
61  * the Gregorian date when the Gregorian calendar was instituted
62  * (October 15, 1582 in some countries, later in others).  The cutover
63  * date may be changed by the caller by calling {@link
64  * #setGregorianChange(Date) setGregorianChange()}.
65  *
66  * <p>
67  * Historically, in those countries which adopted the Gregorian calendar first,
68  * October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models
69  * this correctly.  Before the Gregorian cutover, <code>GregorianCalendar</code>
70  * implements the Julian calendar.  The only difference between the Gregorian
71  * and the Julian calendar is the leap year rule. The Julian calendar specifies
72  * leap years every four years, whereas the Gregorian calendar omits century
73  * years which are not divisible by 400.
74  *
75  * <p>
76  * <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and
77  * Julian calendars. That is, dates are computed by extrapolating the current
78  * rules indefinitely far backward and forward in time. As a result,
79  * <code>GregorianCalendar</code> may be used for all years to generate
80  * meaningful and consistent results. However, dates obtained using
81  * <code>GregorianCalendar</code> are historically accurate only from March 1, 4
82  * AD onward, when modern Julian calendar rules were adopted.  Before this date,
83  * leap year rules were applied irregularly, and before 45 BC the Julian
84  * calendar did not even exist.
85  *
86  * <p>
87  * Prior to the institution of the Gregorian calendar, New Year's Day was
88  * March 25. To avoid confusion, this calendar always uses January 1. A manual
89  * adjustment may be made if desired for dates that are prior to the Gregorian
90  * changeover and which fall between January 1 and March 24.
91  *
92  * <h4><a name="week_and_year">Week Of Year and Week Year</a></h4>
93  *
94  * <p>Values calculated for the {@link Calendar#WEEK_OF_YEAR
95  * WEEK_OF_YEAR} field range from 1 to 53. The first week of a
96  * calendar year is the earliest seven day period starting on {@link
97  * Calendar#getFirstDayOfWeek() getFirstDayOfWeek()} that contains at
98  * least {@link Calendar#getMinimalDaysInFirstWeek()
99  * getMinimalDaysInFirstWeek()} days from that year. It thus depends
100  * on the values of {@code getMinimalDaysInFirstWeek()}, {@code
101  * getFirstDayOfWeek()}, and the day of the week of January 1. Weeks
102  * between week 1 of one year and week 1 of the following year
103  * (exclusive) are numbered sequentially from 2 to 52 or 53 (except
104  * for year(s) involved in the Julian-Gregorian transition).
105  *
106  * <p>The {@code getFirstDayOfWeek()} and {@code
107  * getMinimalDaysInFirstWeek()} values are initialized using
108  * locale-dependent resources when constructing a {@code
109  * GregorianCalendar}. <a name="iso8601_compatible_setting">The week
110  * determination is compatible</a> with the ISO 8601 standard when {@code
111  * getFirstDayOfWeek()} is {@code MONDAY} and {@code
112  * getMinimalDaysInFirstWeek()} is 4, which values are used in locales
113  * where the standard is preferred. These values can explicitly be set by
114  * calling {@link Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()} and
115  * {@link Calendar#setMinimalDaysInFirstWeek(int)
116  * setMinimalDaysInFirstWeek()}.
117  *
118  * <p>A <a name="week_year"><em>week year</em></a> is in sync with a
119  * {@code WEEK_OF_YEAR} cycle. All weeks between the first and last
120  * weeks (inclusive) have the same <em>week year</em> value.
121  * Therefore, the first and last days of a week year may have
122  * different calendar year values.
123  *
124  * <p>For example, January 1, 1998 is a Thursday. If {@code
125  * getFirstDayOfWeek()} is {@code MONDAY} and {@code
126  * getMinimalDaysInFirstWeek()} is 4 (ISO 8601 standard compatible
127  * setting), then week 1 of 1998 starts on December 29, 1997, and ends
128  * on January 4, 1998. The week year is 1998 for the last three days
129  * of calendar year 1997. If, however, {@code getFirstDayOfWeek()} is
130  * {@code SUNDAY}, then week 1 of 1998 starts on January 4, 1998, and
131  * ends on January 10, 1998; the first three days of 1998 then are
132  * part of week 53 of 1997 and their week year is 1997.
133  *
134  * <h4>Week Of Month</h4>
135  *
136  * <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0
137  * to 6.  Week 1 of a month (the days with <code>WEEK_OF_MONTH =
138  * 1</code>) is the earliest set of at least
139  * <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,
140  * ending on the day before <code>getFirstDayOfWeek()</code>.  Unlike
141  * week 1 of a year, week 1 of a month may be shorter than 7 days, need
142  * not start on <code>getFirstDayOfWeek()</code>, and will not include days of
143  * the previous month.  Days of a month before week 1 have a
144  * <code>WEEK_OF_MONTH</code> of 0.
145  *
146  * <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>
147  * and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of
148  * January 1998 is Sunday, January 4 through Saturday, January 10.  These days
149  * have a <code>WEEK_OF_MONTH</code> of 1.  Thursday, January 1 through
150  * Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0.  If
151  * <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1
152  * through January 3 have a <code>WEEK_OF_MONTH</code> of 1.
153  *
154  * <h4>Default Fields Values</h4>
155  *
156  * <p>The <code>clear</code> method sets calendar field(s)
157  * undefined. <code>GregorianCalendar</code> uses the following
158  * default value for each calendar field if its value is undefined.
159  *
160  * <table cellpadding="0" cellspacing="3" border="0"
161  *        summary="GregorianCalendar default field values"
162  *        style="text-align: left; width: 66%;">
163  *   <tbody>
164  *     <tr>
165  *       <th style="vertical-align: top; background-color: rgb(204, 204, 255);
166  *           text-align: center;">Field<br>
167  *       </th>
168  *       <th style="vertical-align: top; background-color: rgb(204, 204, 255);
169  *           text-align: center;">Default Value<br>
170  *       </th>
171  *     </tr>
172  *     <tr>
173  *       <td style="vertical-align: middle;">
174  *              <code>ERA<br></code>
175  *       </td>
176  *       <td style="vertical-align: middle;">
177  *              <code>AD<br></code>
178  *       </td>
179  *     </tr>
180  *     <tr>
181  *       <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
182  *              <code>YEAR<br></code>
183  *       </td>
184  *       <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
185  *              <code>1970<br></code>
186  *       </td>
187  *     </tr>
188  *     <tr>
189  *       <td style="vertical-align: middle;">
190  *              <code>MONTH<br></code>
191  *       </td>
192  *       <td style="vertical-align: middle;">
193  *              <code>JANUARY<br></code>
194  *       </td>
195  *     </tr>
196  *     <tr>
197  *       <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
198  *              <code>DAY_OF_MONTH<br></code>
199  *       </td>
200  *       <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
201  *              <code>1<br></code>
202  *       </td>
203  *     </tr>
204  *     <tr>
205  *       <td style="vertical-align: middle;">
206  *              <code>DAY_OF_WEEK<br></code>
207  *       </td>
208  *       <td style="vertical-align: middle;">
209  *              <code>the first day of week<br></code>
210  *       </td>
211  *     </tr>
212  *     <tr>
213  *       <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
214  *              <code>WEEK_OF_MONTH<br></code>
215  *       </td>
216  *       <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
217  *              <code>0<br></code>
218  *       </td>
219  *     </tr>
220  *     <tr>
221  *       <td style="vertical-align: top;">
222  *              <code>DAY_OF_WEEK_IN_MONTH<br></code>
223  *       </td>
224  *       <td style="vertical-align: top;">
225  *              <code>1<br></code>
226  *       </td>
227  *     </tr>
228  *     <tr>
229  *       <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
230  *              <code>AM_PM<br></code>
231  *       </td>
232  *       <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
233  *              <code>AM<br></code>
234  *       </td>
235  *     </tr>
236  *     <tr>
237  *       <td style="vertical-align: middle;">
238  *              <code>HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND<br></code>
239  *       </td>
240  *       <td style="vertical-align: middle;">
241  *              <code>0<br></code>
242  *       </td>
243  *     </tr>
244  *   </tbody>
245  * </table>
246  * <br>Default values are not applicable for the fields not listed above.
247  *
248  * <p>
249  * <strong>Example:</strong>
250  * <blockquote>
251  * <pre>
252  * // get the supported ids for GMT-08:00 (Pacific Standard Time)
253  * String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
254  * // if no ids were returned, something is wrong. get out.
255  * if (ids.length == 0)
256  *     System.exit(0);
257  *
258  *  // begin output
259  * System.out.println("Current Time");
260  *
261  * // create a Pacific Standard Time time zone
262  * SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
263  *
264  * // set up rules for Daylight Saving Time
265  * pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
266  * pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
267  *
268  * // create a GregorianCalendar with the Pacific Daylight time zone
269  * // and the current date and time
270  * Calendar calendar = new GregorianCalendar(pdt);
271  * Date trialTime = new Date();
272  * calendar.setTime(trialTime);
273  *
274  * // print out a bunch of interesting things
275  * System.out.println("ERA: " + calendar.get(Calendar.ERA));
276  * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
277  * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
278  * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
279  * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
280  * System.out.println("DATE: " + calendar.get(Calendar.DATE));
281  * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
282  * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
283  * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
284  * System.out.println("DAY_OF_WEEK_IN_MONTH: "
285  *                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
286  * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
287  * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
288  * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
289  * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
290  * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
291  * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
292  * System.out.println("ZONE_OFFSET: "
293  *                    + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
294  * System.out.println("DST_OFFSET: "
295  *                    + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
296 
297  * System.out.println("Current Time, with hour reset to 3");
298  * calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
299  * calendar.set(Calendar.HOUR, 3);
300  * System.out.println("ERA: " + calendar.get(Calendar.ERA));
301  * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
302  * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
303  * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
304  * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
305  * System.out.println("DATE: " + calendar.get(Calendar.DATE));
306  * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
307  * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
308  * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
309  * System.out.println("DAY_OF_WEEK_IN_MONTH: "
310  *                    + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
311  * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
312  * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
313  * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
314  * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
315  * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
316  * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
317  * System.out.println("ZONE_OFFSET: "
318  *        + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
319  * System.out.println("DST_OFFSET: "
320  *        + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
321  * </pre>
322  * </blockquote>
323  *
324  * @see          TimeZone
325  * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
326  * @since JDK1.1
327  */
328 public class GregorianCalendar extends Calendar {
329     /*
330      * Implementation Notes
331      *
332      * The epoch is the number of days or milliseconds from some defined
333      * starting point. The epoch for java.util.Date is used here; that is,
334      * milliseconds from January 1, 1970 (Gregorian), midnight UTC.  Other
335      * epochs which are used are January 1, year 1 (Gregorian), which is day 1
336      * of the Gregorian calendar, and December 30, year 0 (Gregorian), which is
337      * day 1 of the Julian calendar.
338      *
339      * We implement the proleptic Julian and Gregorian calendars.  This means we
340      * implement the modern definition of the calendar even though the
341      * historical usage differs.  For example, if the Gregorian change is set
342      * to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which
343      * labels dates preceding the invention of the Gregorian calendar in 1582 as
344      * if the calendar existed then.
345      *
346      * Likewise, with the Julian calendar, we assume a consistent
347      * 4-year leap year rule, even though the historical pattern of
348      * leap years is irregular, being every 3 years from 45 BCE
349      * through 9 BCE, then every 4 years from 8 CE onwards, with no
350      * leap years in-between.  Thus date computations and functions
351      * such as isLeapYear() are not intended to be historically
352      * accurate.
353      */
354 
355 //////////////////
356 // Class Variables
357 //////////////////
358 
359     /**
360      * Value of the <code>ERA</code> field indicating
361      * the period before the common era (before Christ), also known as BCE.
362      * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
363      * ..., 2 BC, 1 BC, 1 AD, 2 AD,...
364      *
365      * @see #ERA
366      */
367     public static final int BC = 0;
368 
369     /**
370      * Value of the {@link #ERA} field indicating
371      * the period before the common era, the same value as {@link #BC}.
372      *
373      * @see #CE
374      */
375     static final int BCE = 0;
376 
377     /**
378      * Value of the <code>ERA</code> field indicating
379      * the common era (Anno Domini), also known as CE.
380      * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
381      * ..., 2 BC, 1 BC, 1 AD, 2 AD,...
382      *
383      * @see #ERA
384      */
385     public static final int AD = 1;
386 
387     /**
388      * Value of the {@link #ERA} field indicating
389      * the common era, the same value as {@link #AD}.
390      *
391      * @see #BCE
392      */
393     static final int CE = 1;
394 
395     private static final int EPOCH_OFFSET   = 719163; // Fixed date of January 1, 1970 (Gregorian)
396     private static final int EPOCH_YEAR     = 1970;
397 
398     static final int MONTH_LENGTH[]
399         = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
400     static final int LEAP_MONTH_LENGTH[]
401         = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
402 
403     // Useful millisecond constants.  Although ONE_DAY and ONE_WEEK can fit
404     // into ints, they must be longs in order to prevent arithmetic overflow
405     // when performing (bug 4173516).
406     private static final int  ONE_SECOND = 1000;
407     private static final int  ONE_MINUTE = 60*ONE_SECOND;
408     private static final int  ONE_HOUR   = 60*ONE_MINUTE;
409     private static final long ONE_DAY    = 24*ONE_HOUR;
410     private static final long ONE_WEEK   = 7*ONE_DAY;
411 
412     /*
413      * <pre>
414      *                            Greatest       Least
415      * Field name        Minimum   Minimum     Maximum     Maximum
416      * ----------        -------   -------     -------     -------
417      * ERA                     0         0           1           1
418      * YEAR                    1         1   292269054   292278994
419      * MONTH                   0         0          11          11
420      * WEEK_OF_YEAR            1         1          52*         53
421      * WEEK_OF_MONTH           0         0           4*          6
422      * DAY_OF_MONTH            1         1          28*         31
423      * DAY_OF_YEAR             1         1         365*        366
424      * DAY_OF_WEEK             1         1           7           7
425      * DAY_OF_WEEK_IN_MONTH   -1        -1           4*          6
426      * AM_PM                   0         0           1           1
427      * HOUR                    0         0          11          11
428      * HOUR_OF_DAY             0         0          23          23
429      * MINUTE                  0         0          59          59
430      * SECOND                  0         0          59          59
431      * MILLISECOND             0         0         999         999
432      * ZONE_OFFSET        -13:00    -13:00       14:00       14:00
433      * DST_OFFSET           0:00      0:00        0:20        2:00
434      * </pre>
435      * *: depends on the Gregorian change date
436      */
437     static final int MIN_VALUES[] = {
438         BCE,            // ERA
439         1,              // YEAR
440         JANUARY,        // MONTH
441         1,              // WEEK_OF_YEAR
442         0,              // WEEK_OF_MONTH
443         1,              // DAY_OF_MONTH
444         1,              // DAY_OF_YEAR
445         SUNDAY,         // DAY_OF_WEEK
446         1,              // DAY_OF_WEEK_IN_MONTH
447         AM,             // AM_PM
448         0,              // HOUR
449         0,              // HOUR_OF_DAY
450         0,              // MINUTE
451         0,              // SECOND
452         0,              // MILLISECOND
453         -13*ONE_HOUR,   // ZONE_OFFSET (UNIX compatibility)
454         0               // DST_OFFSET
455     };
456     static final int LEAST_MAX_VALUES[] = {
457         CE,             // ERA
458         292269054,      // YEAR
459         DECEMBER,       // MONTH
460         52,             // WEEK_OF_YEAR
461         4,              // WEEK_OF_MONTH
462         28,             // DAY_OF_MONTH
463         365,            // DAY_OF_YEAR
464         SATURDAY,       // DAY_OF_WEEK
465         4,              // DAY_OF_WEEK_IN
466         PM,             // AM_PM
467         11,             // HOUR
468         23,             // HOUR_OF_DAY
469         59,             // MINUTE
470         59,             // SECOND
471         999,            // MILLISECOND
472         14*ONE_HOUR,    // ZONE_OFFSET
473         20*ONE_MINUTE   // DST_OFFSET (historical least maximum)
474     };
475     static final int MAX_VALUES[] = {
476         CE,             // ERA
477         292278994,      // YEAR
478         DECEMBER,       // MONTH
479         53,             // WEEK_OF_YEAR
480         6,              // WEEK_OF_MONTH
481         31,             // DAY_OF_MONTH
482         366,            // DAY_OF_YEAR
483         SATURDAY,       // DAY_OF_WEEK
484         6,              // DAY_OF_WEEK_IN
485         PM,             // AM_PM
486         11,             // HOUR
487         23,             // HOUR_OF_DAY
488         59,             // MINUTE
489         59,             // SECOND
490         999,            // MILLISECOND
491         14*ONE_HOUR,    // ZONE_OFFSET
492         2*ONE_HOUR      // DST_OFFSET (double summer time)
493     };
494 
495     // Proclaim serialization compatibility with JDK 1.1
496     static final long serialVersionUID = -8125100834729963327L;
497 
498     // Reference to the sun.util.calendar.Gregorian instance (singleton).
499     private static final Gregorian gcal =
500                                 CalendarSystem.getGregorianCalendar();
501 
502     // Reference to the JulianCalendar instance (singleton), set as needed. See
503     // getJulianCalendarSystem().
504     private static JulianCalendar jcal;
505 
506     // JulianCalendar eras. See getJulianCalendarSystem().
507     private static Era[] jeras;
508 
509     // The default value of gregorianCutover.
510     static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;
511 
512 /////////////////////
513 // Instance Variables
514 /////////////////////
515 
516     /**
517      * The point at which the Gregorian calendar rules are used, measured in
518      * milliseconds from the standard epoch.  Default is October 15, 1582
519      * (Gregorian) 00:00:00 UTC or -12219292800000L.  For this value, October 4,
520      * 1582 (Julian) is followed by October 15, 1582 (Gregorian).  This
521      * corresponds to Julian day number 2299161.
522      * @serial
523      */
524     private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;
525 
526     /**
527      * The fixed date of the gregorianCutover.
528      */
529     private transient long gregorianCutoverDate =
530         (((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736
531 
532     /**
533      * The normalized year of the gregorianCutover in Gregorian, with
534      * 0 representing 1 BCE, -1 representing 2 BCE, etc.
535      */
536     private transient int gregorianCutoverYear = 1582;
537 
538     /**
539      * The normalized year of the gregorianCutover in Julian, with 0
540      * representing 1 BCE, -1 representing 2 BCE, etc.
541      */
542     private transient int gregorianCutoverYearJulian = 1582;
543 
544     /**
545      * gdate always has a sun.util.calendar.Gregorian.Date instance to
546      * avoid overhead of creating it. The assumption is that most
547      * applications will need only Gregorian calendar calculations.
548      */
549     private transient BaseCalendar.Date gdate;
550 
551     /**
552      * Reference to either gdate or a JulianCalendar.Date
553      * instance. After calling complete(), this value is guaranteed to
554      * be set.
555      */
556     private transient BaseCalendar.Date cdate;
557 
558     /**
559      * The CalendarSystem used to calculate the date in cdate. After
560      * calling complete(), this value is guaranteed to be set and
561      * consistent with the cdate value.
562      */
563     private transient BaseCalendar calsys;
564 
565     /**
566      * Temporary int[2] to get time zone offsets. zoneOffsets[0] gets
567      * the GMT offset value and zoneOffsets[1] gets the DST saving
568      * value.
569      */
570     private transient int[] zoneOffsets;
571 
572     /**
573      * Temporary storage for saving original fields[] values in
574      * non-lenient mode.
575      */
576     private transient int[] originalFields;
577 
578 ///////////////
579 // Constructors
580 ///////////////
581 
582     /**
583      * Constructs a default <code>GregorianCalendar</code> using the current time
584      * in the default time zone with the default locale.
585      */
GregorianCalendar()586     public GregorianCalendar() {
587         this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
588         setZoneShared(true);
589     }
590 
591     /**
592      * Constructs a <code>GregorianCalendar</code> based on the current time
593      * in the given time zone with the default locale.
594      *
595      * @param zone the given time zone.
596      */
GregorianCalendar(TimeZone zone)597     public GregorianCalendar(TimeZone zone) {
598         this(zone, Locale.getDefault(Locale.Category.FORMAT));
599     }
600 
601     /**
602      * Constructs a <code>GregorianCalendar</code> based on the current time
603      * in the default time zone with the given locale.
604      *
605      * @param aLocale the given locale.
606      */
GregorianCalendar(Locale aLocale)607     public GregorianCalendar(Locale aLocale) {
608         this(TimeZone.getDefaultRef(), aLocale);
609         setZoneShared(true);
610     }
611 
612     /**
613      * Constructs a <code>GregorianCalendar</code> based on the current time
614      * in the given time zone with the given locale.
615      *
616      * @param zone the given time zone.
617      * @param aLocale the given locale.
618      */
GregorianCalendar(TimeZone zone, Locale aLocale)619     public GregorianCalendar(TimeZone zone, Locale aLocale) {
620         super(zone, aLocale);
621         gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);
622         setTimeInMillis(System.currentTimeMillis());
623     }
624 
625     /**
626      * Constructs a <code>GregorianCalendar</code> with the given date set
627      * in the default time zone with the default locale.
628      *
629      * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
630      * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
631      * Month value is 0-based. e.g., 0 for January.
632      * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
633      */
GregorianCalendar(int year, int month, int dayOfMonth)634     public GregorianCalendar(int year, int month, int dayOfMonth) {
635         this(year, month, dayOfMonth, 0, 0, 0, 0);
636     }
637 
638     /**
639      * Constructs a <code>GregorianCalendar</code> with the given date
640      * and time set for the default time zone with the default locale.
641      *
642      * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
643      * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
644      * Month value is 0-based. e.g., 0 for January.
645      * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
646      * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
647      * in the calendar.
648      * @param minute the value used to set the <code>MINUTE</code> calendar field
649      * in the calendar.
650      */
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)651     public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
652                              int minute) {
653         this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);
654     }
655 
656     /**
657      * Constructs a GregorianCalendar with the given date
658      * and time set for the default time zone with the default locale.
659      *
660      * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
661      * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
662      * Month value is 0-based. e.g., 0 for January.
663      * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
664      * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
665      * in the calendar.
666      * @param minute the value used to set the <code>MINUTE</code> calendar field
667      * in the calendar.
668      * @param second the value used to set the <code>SECOND</code> calendar field
669      * in the calendar.
670      */
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second)671     public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
672                              int minute, int second) {
673         this(year, month, dayOfMonth, hourOfDay, minute, second, 0);
674     }
675 
676     /**
677      * Constructs a <code>GregorianCalendar</code> with the given date
678      * and time set for the default time zone with the default locale.
679      *
680      * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
681      * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
682      * Month value is 0-based. e.g., 0 for January.
683      * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
684      * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
685      * in the calendar.
686      * @param minute the value used to set the <code>MINUTE</code> calendar field
687      * in the calendar.
688      * @param second the value used to set the <code>SECOND</code> calendar field
689      * in the calendar.
690      * @param millis the value used to set the <code>MILLISECOND</code> calendar field
691      */
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second, int millis)692     GregorianCalendar(int year, int month, int dayOfMonth,
693                       int hourOfDay, int minute, int second, int millis) {
694         super();
695         gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
696         this.set(YEAR, year);
697         this.set(MONTH, month);
698         this.set(DAY_OF_MONTH, dayOfMonth);
699 
700         // Set AM_PM and HOUR here to set their stamp values before
701         // setting HOUR_OF_DAY (6178071).
702         if (hourOfDay >= 12 && hourOfDay <= 23) {
703             // If hourOfDay is a valid PM hour, set the correct PM values
704             // so that it won't throw an exception in case it's set to
705             // non-lenient later.
706             this.internalSet(AM_PM, PM);
707             this.internalSet(HOUR, hourOfDay - 12);
708         } else {
709             // The default value for AM_PM is AM.
710             // We don't care any out of range value here for leniency.
711             this.internalSet(HOUR, hourOfDay);
712         }
713         // The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)
714         setFieldsComputed(HOUR_MASK|AM_PM_MASK);
715 
716         this.set(HOUR_OF_DAY, hourOfDay);
717         this.set(MINUTE, minute);
718         this.set(SECOND, second);
719         // should be changed to set() when this constructor is made
720         // public.
721         this.internalSet(MILLISECOND, millis);
722     }
723 
GregorianCalendar(long milliseconds)724     GregorianCalendar(long milliseconds) {
725         this();
726         setTimeInMillis(milliseconds);
727     }
728 
729 /////////////////
730 // Public methods
731 /////////////////
732 
733     /**
734      * Sets the <code>GregorianCalendar</code> change date. This is the point when the switch
735      * from Julian dates to Gregorian dates occurred. Default is October 15,
736      * 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.
737      * <p>
738      * To obtain a pure Julian calendar, set the change date to
739      * <code>Date(Long.MAX_VALUE)</code>.  To obtain a pure Gregorian calendar,
740      * set the change date to <code>Date(Long.MIN_VALUE)</code>.
741      *
742      * @param date the given Gregorian cutover date.
743      */
setGregorianChange(Date date)744     public void setGregorianChange(Date date) {
745         long cutoverTime = date.getTime();
746         if (cutoverTime == gregorianCutover) {
747             return;
748         }
749         // Before changing the cutover date, make sure to have the
750         // time of this calendar.
751         complete();
752         setGregorianChange(cutoverTime);
753     }
754 
setGregorianChange(long cutoverTime)755     private void setGregorianChange(long cutoverTime) {
756         gregorianCutover = cutoverTime;
757         gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)
758                                 + EPOCH_OFFSET;
759 
760         // To provide the "pure" Julian calendar as advertised.
761         // Strictly speaking, the last millisecond should be a
762         // Gregorian date. However, the API doc specifies that setting
763         // the cutover date to Long.MAX_VALUE will make this calendar
764         // a pure Julian calendar. (See 4167995)
765         if (cutoverTime == Long.MAX_VALUE) {
766             gregorianCutoverDate++;
767         }
768 
769         BaseCalendar.Date d = getGregorianCutoverDate();
770 
771         // Set the cutover year (in the Gregorian year numbering)
772         gregorianCutoverYear = d.getYear();
773 
774         BaseCalendar jcal = getJulianCalendarSystem();
775         d = (BaseCalendar.Date) jcal.newCalendarDate(TimeZone.NO_TIMEZONE);
776         jcal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);
777         gregorianCutoverYearJulian = d.getNormalizedYear();
778 
779         if (time < gregorianCutover) {
780             // The field values are no longer valid under the new
781             // cutover date.
782             setUnnormalized();
783         }
784     }
785 
786     /**
787      * Gets the Gregorian Calendar change date.  This is the point when the
788      * switch from Julian dates to Gregorian dates occurred. Default is
789      * October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian
790      * calendar.
791      *
792      * @return the Gregorian cutover date for this <code>GregorianCalendar</code> object.
793      */
getGregorianChange()794     public final Date getGregorianChange() {
795         return new Date(gregorianCutover);
796     }
797 
798     /**
799      * Determines if the given year is a leap year. Returns <code>true</code> if
800      * the given year is a leap year. To specify BC year numbers,
801      * <code>1 - year number</code> must be given. For example, year BC 4 is
802      * specified as -3.
803      *
804      * @param year the given year.
805      * @return <code>true</code> if the given year is a leap year; <code>false</code> otherwise.
806      */
isLeapYear(int year)807     public boolean isLeapYear(int year) {
808         if ((year & 3) != 0) {
809             return false;
810         }
811 
812         if (year > gregorianCutoverYear) {
813             return (year%100 != 0) || (year%400 == 0); // Gregorian
814         }
815         if (year < gregorianCutoverYearJulian) {
816             return true; // Julian
817         }
818         boolean gregorian;
819         // If the given year is the Gregorian cutover year, we need to
820         // determine which calendar system to be applied to February in the year.
821         if (gregorianCutoverYear == gregorianCutoverYearJulian) {
822             BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian
823             gregorian = d.getMonth() < BaseCalendar.MARCH;
824         } else {
825             gregorian = year == gregorianCutoverYear;
826         }
827         return gregorian ? (year%100 != 0) || (year%400 == 0) : true;
828     }
829 
830     /**
831      * Compares this <code>GregorianCalendar</code> to the specified
832      * <code>Object</code>. The result is <code>true</code> if and
833      * only if the argument is a <code>GregorianCalendar</code> object
834      * that represents the same time value (millisecond offset from
835      * the <a href="Calendar.html#Epoch">Epoch</a>) under the same
836      * <code>Calendar</code> parameters and Gregorian change date as
837      * this object.
838      *
839      * @param obj the object to compare with.
840      * @return <code>true</code> if this object is equal to <code>obj</code>;
841      * <code>false</code> otherwise.
842      * @see Calendar#compareTo(Calendar)
843      */
844     public boolean equals(Object obj) {
845         return obj instanceof GregorianCalendar &&
846             super.equals(obj) &&
847             gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;
848     }
849 
850     /**
851      * Generates the hash code for this <code>GregorianCalendar</code> object.
852      */
853     public int hashCode() {
854         return super.hashCode() ^ (int)gregorianCutoverDate;
855     }
856 
857     /**
858      * Adds the specified (signed) amount of time to the given calendar field,
859      * based on the calendar's rules.
860      *
861      * <p><em>Add rule 1</em>. The value of <code>field</code>
862      * after the call minus the value of <code>field</code> before the
863      * call is <code>amount</code>, modulo any overflow that has occurred in
864      * <code>field</code>. Overflow occurs when a field value exceeds its
865      * range and, as a result, the next larger field is incremented or
866      * decremented and the field value is adjusted back into its range.</p>
867      *
868      * <p><em>Add rule 2</em>. If a smaller field is expected to be
869      * invariant, but it is impossible for it to be equal to its
870      * prior value because of changes in its minimum or maximum after
871      * <code>field</code> is changed, then its value is adjusted to be as close
872      * as possible to its expected value. A smaller field represents a
873      * smaller unit of time. <code>HOUR</code> is a smaller field than
874      * <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields
875      * that are not expected to be invariant. The calendar system
876      * determines what fields are expected to be invariant.</p>
877      *
878      * @param field the calendar field.
879      * @param amount the amount of date or time to be added to the field.
880      * @exception IllegalArgumentException if <code>field</code> is
881      * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
882      * or if any calendar fields have out-of-range values in
883      * non-lenient mode.
884      */
885     public void add(int field, int amount) {
886         // If amount == 0, do nothing even the given field is out of
887         // range. This is tested by JCK.
888         if (amount == 0) {
889             return;   // Do nothing!
890         }
891 
892         if (field < 0 || field >= ZONE_OFFSET) {
893             throw new IllegalArgumentException();
894         }
895 
896         // Sync the time and calendar fields.
897         complete();
898 
899         if (field == YEAR) {
900             int year = internalGet(YEAR);
901             if (internalGetEra() == CE) {
902                 year += amount;
903                 if (year > 0) {
904                     set(YEAR, year);
905                 } else { // year <= 0
906                     set(YEAR, 1 - year);
907                     // if year == 0, you get 1 BCE.
908                     set(ERA, BCE);
909                 }
910             }
911             else { // era == BCE
912                 year -= amount;
913                 if (year > 0) {
914                     set(YEAR, year);
915                 } else { // year <= 0
916                     set(YEAR, 1 - year);
917                     // if year == 0, you get 1 CE
918                     set(ERA, CE);
919                 }
920             }
921             pinDayOfMonth();
922         } else if (field == MONTH) {
923             int month = internalGet(MONTH) + amount;
924             int year = internalGet(YEAR);
925             int y_amount;
926 
927             if (month >= 0) {
928                 y_amount = month/12;
929             } else {
930                 y_amount = (month+1)/12 - 1;
931             }
932             if (y_amount != 0) {
933                 if (internalGetEra() == CE) {
934                     year += y_amount;
935                     if (year > 0) {
936                         set(YEAR, year);
937                     } else { // year <= 0
938                         set(YEAR, 1 - year);
939                         // if year == 0, you get 1 BCE
940                         set(ERA, BCE);
941                     }
942                 }
943                 else { // era == BCE
944                     year -= y_amount;
945                     if (year > 0) {
946                         set(YEAR, year);
947                     } else { // year <= 0
948                         set(YEAR, 1 - year);
949                         // if year == 0, you get 1 CE
950                         set(ERA, CE);
951                     }
952                 }
953             }
954 
955             if (month >= 0) {
956                 set(MONTH, (int) (month % 12));
957             } else {
958                 // month < 0
959                 month %= 12;
960                 if (month < 0) {
961                     month += 12;
962                 }
963                 set(MONTH, JANUARY + month);
964             }
965             pinDayOfMonth();
966         } else if (field == ERA) {
967             int era = internalGet(ERA) + amount;
968             if (era < 0) {
969                 era = 0;
970             }
971             if (era > 1) {
972                 era = 1;
973             }
974             set(ERA, era);
975         } else {
976             long delta = amount;
977             long timeOfDay = 0;
978             switch (field) {
979             // Handle the time fields here. Convert the given
980             // amount to milliseconds and call setTimeInMillis.
981             case HOUR:
982             case HOUR_OF_DAY:
983                 delta *= 60 * 60 * 1000;        // hours to minutes
984                 break;
985 
986             case MINUTE:
987                 delta *= 60 * 1000;             // minutes to seconds
988                 break;
989 
990             case SECOND:
991                 delta *= 1000;                  // seconds to milliseconds
992                 break;
993 
994             case MILLISECOND:
995                 break;
996 
997             // Handle week, day and AM_PM fields which involves
998             // time zone offset change adjustment. Convert the
999             // given amount to the number of days.
1000             case WEEK_OF_YEAR:
1001             case WEEK_OF_MONTH:
1002             case DAY_OF_WEEK_IN_MONTH:
1003                 delta *= 7;
1004                 break;
1005 
1006             case DAY_OF_MONTH: // synonym of DATE
1007             case DAY_OF_YEAR:
1008             case DAY_OF_WEEK:
1009                 break;
1010 
1011             case AM_PM:
1012                 // Convert the amount to the number of days (delta)
1013                 // and +12 or -12 hours (timeOfDay).
1014                 delta = amount / 2;
1015                 timeOfDay = 12 * (amount % 2);
1016                 break;
1017             }
1018 
1019             // The time fields don't require time zone offset change
1020             // adjustment.
1021             if (field >= HOUR) {
1022                 setTimeInMillis(time + delta);
1023                 return;
1024             }
1025 
1026             // The rest of the fields (week, day or AM_PM fields)
1027             // require time zone offset (both GMT and DST) change
1028             // adjustment.
1029 
1030             // Translate the current time to the fixed date and time
1031             // of the day.
1032             long fd = getCurrentFixedDate();
1033             timeOfDay += internalGet(HOUR_OF_DAY);
1034             timeOfDay *= 60;
1035             timeOfDay += internalGet(MINUTE);
1036             timeOfDay *= 60;
1037             timeOfDay += internalGet(SECOND);
1038             timeOfDay *= 1000;
1039             timeOfDay += internalGet(MILLISECOND);
1040             if (timeOfDay >= ONE_DAY) {
1041                 fd++;
1042                 timeOfDay -= ONE_DAY;
1043             } else if (timeOfDay < 0) {
1044                 fd--;
1045                 timeOfDay += ONE_DAY;
1046             }
1047 
1048             fd += delta; // fd is the expected fixed date after the calculation
1049 
1050             // Calculate the time in the UTC time zone.
1051             long utcTime = (fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay;
1052 
1053             // Neither of the time zone related fields are relevant because they have not been
1054             // set since the call to complete() above.
1055             int tzMask = 0;
1056 
1057             // Adjust the time to account for zone and daylight savings time offset.
1058             long millis = adjustForZoneAndDaylightSavingsTime(tzMask, utcTime, getZone());
1059 
1060             // Update the time and recompute the fields.
1061             setTimeInMillis(millis);
1062         }
1063     }
1064 
1065     /**
1066      * Adds or subtracts (up/down) a single unit of time on the given time
1067      * field without changing larger fields.
1068      * <p>
1069      * <em>Example</em>: Consider a <code>GregorianCalendar</code>
1070      * originally set to December 31, 1999. Calling {@link #roll(int,boolean) roll(Calendar.MONTH, true)}
1071      * sets the calendar to January 31, 1999.  The <code>YEAR</code> field is unchanged
1072      * because it is a larger field than <code>MONTH</code>.</p>
1073      *
1074      * @param up indicates if the value of the specified calendar field is to be
1075      * rolled up or rolled down. Use <code>true</code> if rolling up, <code>false</code> otherwise.
1076      * @exception IllegalArgumentException if <code>field</code> is
1077      * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
1078      * or if any calendar fields have out-of-range values in
1079      * non-lenient mode.
1080      * @see #add(int,int)
1081      * @see #set(int,int)
1082      */
roll(int field, boolean up)1083     public void roll(int field, boolean up) {
1084         roll(field, up ? +1 : -1);
1085     }
1086 
1087     /**
1088      * Adds a signed amount to the specified calendar field without changing larger fields.
1089      * A negative roll amount means to subtract from field without changing
1090      * larger fields. If the specified amount is 0, this method performs nothing.
1091      *
1092      * <p>This method calls {@link #complete()} before adding the
1093      * amount so that all the calendar fields are normalized. If there
1094      * is any calendar field having an out-of-range value in non-lenient mode, then an
1095      * <code>IllegalArgumentException</code> is thrown.
1096      *
1097      * <p>
1098      * <em>Example</em>: Consider a <code>GregorianCalendar</code>
1099      * originally set to August 31, 1999. Calling <code>roll(Calendar.MONTH,
1100      * 8)</code> sets the calendar to April 30, <strong>1999</strong>. Using a
1101      * <code>GregorianCalendar</code>, the <code>DAY_OF_MONTH</code> field cannot
1102      * be 31 in the month April. <code>DAY_OF_MONTH</code> is set to the closest possible
1103      * value, 30. The <code>YEAR</code> field maintains the value of 1999 because it
1104      * is a larger field than <code>MONTH</code>.
1105      * <p>
1106      * <em>Example</em>: Consider a <code>GregorianCalendar</code>
1107      * originally set to Sunday June 6, 1999. Calling
1108      * <code>roll(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
1109      * Tuesday June 1, 1999, whereas calling
1110      * <code>add(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
1111      * Sunday May 30, 1999. This is because the roll rule imposes an
1112      * additional constraint: The <code>MONTH</code> must not change when the
1113      * <code>WEEK_OF_MONTH</code> is rolled. Taken together with add rule 1,
1114      * the resultant date must be between Tuesday June 1 and Saturday June
1115      * 5. According to add rule 2, the <code>DAY_OF_WEEK</code>, an invariant
1116      * when changing the <code>WEEK_OF_MONTH</code>, is set to Tuesday, the
1117      * closest possible value to Sunday (where Sunday is the first day of the
1118      * week).</p>
1119      *
1120      * @param field the calendar field.
1121      * @param amount the signed amount to add to <code>field</code>.
1122      * @exception IllegalArgumentException if <code>field</code> is
1123      * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
1124      * or if any calendar fields have out-of-range values in
1125      * non-lenient mode.
1126      * @see #roll(int,boolean)
1127      * @see #add(int,int)
1128      * @see #set(int,int)
1129      * @since 1.2
1130      */
roll(int field, int amount)1131     public void roll(int field, int amount) {
1132         // If amount == 0, do nothing even the given field is out of
1133         // range. This is tested by JCK.
1134         if (amount == 0) {
1135             return;
1136         }
1137 
1138         if (field < 0 || field >= ZONE_OFFSET) {
1139             throw new IllegalArgumentException();
1140         }
1141 
1142         // Sync the time and calendar fields.
1143         complete();
1144 
1145         int min = getMinimum(field);
1146         int max = getMaximum(field);
1147 
1148         switch (field) {
1149         case AM_PM:
1150         case ERA:
1151         case YEAR:
1152         case MINUTE:
1153         case SECOND:
1154         case MILLISECOND:
1155             // These fields are handled simply, since they have fixed minima
1156             // and maxima.  The field DAY_OF_MONTH is almost as simple.  Other
1157             // fields are complicated, since the range within they must roll
1158             // varies depending on the date.
1159             break;
1160 
1161         case HOUR:
1162         case HOUR_OF_DAY:
1163             {
1164                 int unit = max + 1; // 12 or 24 hours
1165                 int h = internalGet(field);
1166                 int nh = (h + amount) % unit;
1167                 if (nh < 0) {
1168                     nh += unit;
1169                 }
1170                 time += ONE_HOUR * (nh - h);
1171 
1172                 // The day might have changed, which could happen if
1173                 // the daylight saving time transition brings it to
1174                 // the next day, although it's very unlikely. But we
1175                 // have to make sure not to change the larger fields.
1176                 CalendarDate d = calsys.getCalendarDate(time, getZone());
1177                 if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) {
1178                     d.setDate(internalGet(YEAR),
1179                               internalGet(MONTH) + 1,
1180                               internalGet(DAY_OF_MONTH));
1181                     if (field == HOUR) {
1182                         assert (internalGet(AM_PM) == PM);
1183                         d.addHours(+12); // restore PM
1184                     }
1185                     time = calsys.getTime(d);
1186                 }
1187                 int hourOfDay = d.getHours();
1188                 internalSet(field, hourOfDay % unit);
1189                 if (field == HOUR) {
1190                     internalSet(HOUR_OF_DAY, hourOfDay);
1191                 } else {
1192                     internalSet(AM_PM, hourOfDay / 12);
1193                     internalSet(HOUR, hourOfDay % 12);
1194                 }
1195 
1196                 // Time zone offset and/or daylight saving might have changed.
1197                 int zoneOffset = d.getZoneOffset();
1198                 int saving = d.getDaylightSaving();
1199                 internalSet(ZONE_OFFSET, zoneOffset - saving);
1200                 internalSet(DST_OFFSET, saving);
1201                 return;
1202             }
1203 
1204         case MONTH:
1205             // Rolling the month involves both pinning the final value to [0, 11]
1206             // and adjusting the DAY_OF_MONTH if necessary.  We only adjust the
1207             // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
1208             // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
1209             {
1210                 if (!isCutoverYear(cdate.getNormalizedYear())) {
1211                     int mon = (internalGet(MONTH) + amount) % 12;
1212                     if (mon < 0) {
1213                         mon += 12;
1214                     }
1215                     set(MONTH, mon);
1216 
1217                     // Keep the day of month in the range.  We don't want to spill over
1218                     // into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->
1219                     // mar3.
1220                     int monthLen = monthLength(mon);
1221                     if (internalGet(DAY_OF_MONTH) > monthLen) {
1222                         set(DAY_OF_MONTH, monthLen);
1223                     }
1224                 } else {
1225                     // We need to take care of different lengths in
1226                     // year and month due to the cutover.
1227                     int yearLength = getActualMaximum(MONTH) + 1;
1228                     int mon = (internalGet(MONTH) + amount) % yearLength;
1229                     if (mon < 0) {
1230                         mon += yearLength;
1231                     }
1232                     set(MONTH, mon);
1233                     int monthLen = getActualMaximum(DAY_OF_MONTH);
1234                     if (internalGet(DAY_OF_MONTH) > monthLen) {
1235                         set(DAY_OF_MONTH, monthLen);
1236                     }
1237                 }
1238                 return;
1239             }
1240 
1241         case WEEK_OF_YEAR:
1242             {
1243                 int y = cdate.getNormalizedYear();
1244                 max = getActualMaximum(WEEK_OF_YEAR);
1245                 set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
1246                 int woy = internalGet(WEEK_OF_YEAR);
1247                 int value = woy + amount;
1248                 if (!isCutoverYear(y)) {
1249                     // If the new value is in between min and max
1250                     // (exclusive), then we can use the value.
1251                     if (value > min && value < max) {
1252                         set(WEEK_OF_YEAR, value);
1253                         return;
1254                     }
1255                     long fd = getCurrentFixedDate();
1256                     // Make sure that the min week has the current DAY_OF_WEEK
1257                     long day1 = fd - (7 * (woy - min));
1258                     if (calsys.getYearFromFixedDate(day1) != y) {
1259                         min++;
1260                     }
1261 
1262                     // Make sure the same thing for the max week
1263                     fd += 7 * (max - internalGet(WEEK_OF_YEAR));
1264                     if (calsys.getYearFromFixedDate(fd) != y) {
1265                         max--;
1266                     }
1267                     break;
1268                 }
1269 
1270                 // Handle cutover here.
1271                 long fd = getCurrentFixedDate();
1272                 BaseCalendar cal;
1273                 if (gregorianCutoverYear == gregorianCutoverYearJulian) {
1274                     cal = getCutoverCalendarSystem();
1275                 } else if (y == gregorianCutoverYear) {
1276                     cal = gcal;
1277                 } else {
1278                     cal = getJulianCalendarSystem();
1279                 }
1280                 long day1 = fd - (7 * (woy - min));
1281                 // Make sure that the min week has the current DAY_OF_WEEK
1282                 if (cal.getYearFromFixedDate(day1) != y) {
1283                     min++;
1284                 }
1285 
1286                 // Make sure the same thing for the max week
1287                 fd += 7 * (max - woy);
1288                 cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
1289                 if (cal.getYearFromFixedDate(fd) != y) {
1290                     max--;
1291                 }
1292                 // value: the new WEEK_OF_YEAR which must be converted
1293                 // to month and day of month.
1294                 value = getRolledValue(woy, amount, min, max) - 1;
1295                 BaseCalendar.Date d = getCalendarDate(day1 + value * 7);
1296                 set(MONTH, d.getMonth() - 1);
1297                 set(DAY_OF_MONTH, d.getDayOfMonth());
1298                 return;
1299             }
1300 
1301         case WEEK_OF_MONTH:
1302             {
1303                 boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());
1304                 // dow: relative day of week from first day of week
1305                 int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
1306                 if (dow < 0) {
1307                     dow += 7;
1308                 }
1309 
1310                 long fd = getCurrentFixedDate();
1311                 long month1;     // fixed date of the first day (usually 1) of the month
1312                 int monthLength; // actual month length
1313                 if (isCutoverYear) {
1314                     month1 = getFixedDateMonth1(cdate, fd);
1315                     monthLength = actualMonthLength();
1316                 } else {
1317                     month1 = fd - internalGet(DAY_OF_MONTH) + 1;
1318                     monthLength = calsys.getMonthLength(cdate);
1319                 }
1320 
1321                 // the first day of week of the month.
1322                 long monthDay1st = calsys.getDayOfWeekDateOnOrBefore(month1 + 6,
1323                                                                      getFirstDayOfWeek());
1324                 // if the week has enough days to form a week, the
1325                 // week starts from the previous month.
1326                 if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
1327                     monthDay1st -= 7;
1328                 }
1329                 max = getActualMaximum(field);
1330 
1331                 // value: the new WEEK_OF_MONTH value
1332                 int value = getRolledValue(internalGet(field), amount, 1, max) - 1;
1333 
1334                 // nfd: fixed date of the rolled date
1335                 long nfd = monthDay1st + value * 7 + dow;
1336 
1337                 // Unlike WEEK_OF_YEAR, we need to change day of week if the
1338                 // nfd is out of the month.
1339                 if (nfd < month1) {
1340                     nfd = month1;
1341                 } else if (nfd >= (month1 + monthLength)) {
1342                     nfd = month1 + monthLength - 1;
1343                 }
1344                 int dayOfMonth;
1345                 if (isCutoverYear) {
1346                     // If we are in the cutover year, convert nfd to
1347                     // its calendar date and use dayOfMonth.
1348                     BaseCalendar.Date d = getCalendarDate(nfd);
1349                     dayOfMonth = d.getDayOfMonth();
1350                 } else {
1351                     dayOfMonth = (int)(nfd - month1) + 1;
1352                 }
1353                 set(DAY_OF_MONTH, dayOfMonth);
1354                 return;
1355             }
1356 
1357         case DAY_OF_MONTH:
1358             {
1359                 if (!isCutoverYear(cdate.getNormalizedYear())) {
1360                     max = calsys.getMonthLength(cdate);
1361                     break;
1362                 }
1363 
1364                 // Cutover year handling
1365                 long fd = getCurrentFixedDate();
1366                 long month1 = getFixedDateMonth1(cdate, fd);
1367                 // It may not be a regular month. Convert the date and range to
1368                 // the relative values, perform the roll, and
1369                 // convert the result back to the rolled date.
1370                 int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);
1371                 BaseCalendar.Date d = getCalendarDate(month1 + value);
1372                 assert d.getMonth()-1 == internalGet(MONTH);
1373                 set(DAY_OF_MONTH, d.getDayOfMonth());
1374                 return;
1375             }
1376 
1377         case DAY_OF_YEAR:
1378             {
1379                 max = getActualMaximum(field);
1380                 if (!isCutoverYear(cdate.getNormalizedYear())) {
1381                     break;
1382                 }
1383 
1384                 // Handle cutover here.
1385                 long fd = getCurrentFixedDate();
1386                 long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;
1387                 int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);
1388                 BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);
1389                 set(MONTH, d.getMonth() - 1);
1390                 set(DAY_OF_MONTH, d.getDayOfMonth());
1391                 return;
1392             }
1393 
1394         case DAY_OF_WEEK:
1395             {
1396                 if (!isCutoverYear(cdate.getNormalizedYear())) {
1397                     // If the week of year is in the same year, we can
1398                     // just change DAY_OF_WEEK.
1399                     int weekOfYear = internalGet(WEEK_OF_YEAR);
1400                     if (weekOfYear > 1 && weekOfYear < 52) {
1401                         set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]
1402                         max = SATURDAY;
1403                         break;
1404                     }
1405                 }
1406 
1407                 // We need to handle it in a different way around year
1408                 // boundaries and in the cutover year. Note that
1409                 // changing era and year values violates the roll
1410                 // rule: not changing larger calendar fields...
1411                 amount %= 7;
1412                 if (amount == 0) {
1413                     return;
1414                 }
1415                 long fd = getCurrentFixedDate();
1416                 long dowFirst = calsys.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
1417                 fd += amount;
1418                 if (fd < dowFirst) {
1419                     fd += 7;
1420                 } else if (fd >= dowFirst + 7) {
1421                     fd -= 7;
1422                 }
1423                 BaseCalendar.Date d = getCalendarDate(fd);
1424                 set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));
1425                 set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
1426                 return;
1427             }
1428 
1429         case DAY_OF_WEEK_IN_MONTH:
1430             {
1431                 min = 1; // after normalized, min should be 1.
1432                 if (!isCutoverYear(cdate.getNormalizedYear())) {
1433                     int dom = internalGet(DAY_OF_MONTH);
1434                     int monthLength = calsys.getMonthLength(cdate);
1435                     int lastDays = monthLength % 7;
1436                     max = monthLength / 7;
1437                     int x = (dom - 1) % 7;
1438                     if (x < lastDays) {
1439                         max++;
1440                     }
1441                     set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
1442                     break;
1443                 }
1444 
1445                 // Cutover year handling
1446                 long fd = getCurrentFixedDate();
1447                 long month1 = getFixedDateMonth1(cdate, fd);
1448                 int monthLength = actualMonthLength();
1449                 int lastDays = monthLength % 7;
1450                 max = monthLength / 7;
1451                 int x = (int)(fd - month1) % 7;
1452                 if (x < lastDays) {
1453                     max++;
1454                 }
1455                 int value = getRolledValue(internalGet(field), amount, min, max) - 1;
1456                 fd = month1 + value * 7 + x;
1457                 BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
1458                 BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
1459                 cal.getCalendarDateFromFixedDate(d, fd);
1460                 set(DAY_OF_MONTH, d.getDayOfMonth());
1461                 return;
1462             }
1463         }
1464 
1465         set(field, getRolledValue(internalGet(field), amount, min, max));
1466     }
1467 
1468     /**
1469      * Returns the minimum value for the given calendar field of this
1470      * <code>GregorianCalendar</code> instance. The minimum value is
1471      * defined as the smallest value returned by the {@link
1472      * Calendar#get(int) get} method for any possible time value,
1473      * taking into consideration the current values of the
1474      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1475      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1476      * {@link #getGregorianChange() getGregorianChange} and
1477      * {@link Calendar#getTimeZone() getTimeZone} methods.
1478      *
1479      * @param field the calendar field.
1480      * @return the minimum value for the given calendar field.
1481      * @see #getMaximum(int)
1482      * @see #getGreatestMinimum(int)
1483      * @see #getLeastMaximum(int)
1484      * @see #getActualMinimum(int)
1485      * @see #getActualMaximum(int)
1486      */
getMinimum(int field)1487     public int getMinimum(int field) {
1488         return MIN_VALUES[field];
1489     }
1490 
1491     /**
1492      * Returns the maximum value for the given calendar field of this
1493      * <code>GregorianCalendar</code> instance. The maximum value is
1494      * defined as the largest value returned by the {@link
1495      * Calendar#get(int) get} method for any possible time value,
1496      * taking into consideration the current values of the
1497      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1498      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1499      * {@link #getGregorianChange() getGregorianChange} and
1500      * {@link Calendar#getTimeZone() getTimeZone} methods.
1501      *
1502      * @param field the calendar field.
1503      * @return the maximum value for the given calendar field.
1504      * @see #getMinimum(int)
1505      * @see #getGreatestMinimum(int)
1506      * @see #getLeastMaximum(int)
1507      * @see #getActualMinimum(int)
1508      * @see #getActualMaximum(int)
1509      */
getMaximum(int field)1510     public int getMaximum(int field) {
1511         switch (field) {
1512         case MONTH:
1513         case DAY_OF_MONTH:
1514         case DAY_OF_YEAR:
1515         case WEEK_OF_YEAR:
1516         case WEEK_OF_MONTH:
1517         case DAY_OF_WEEK_IN_MONTH:
1518         case YEAR:
1519             {
1520                 // On or after Gregorian 200-3-1, Julian and Gregorian
1521                 // calendar dates are the same or Gregorian dates are
1522                 // larger (i.e., there is a "gap") after 300-3-1.
1523                 if (gregorianCutoverYear > 200) {
1524                     break;
1525                 }
1526                 // There might be "overlapping" dates.
1527                 GregorianCalendar gc = (GregorianCalendar) clone();
1528                 gc.setLenient(true);
1529                 gc.setTimeInMillis(gregorianCutover);
1530                 int v1 = gc.getActualMaximum(field);
1531                 gc.setTimeInMillis(gregorianCutover-1);
1532                 int v2 = gc.getActualMaximum(field);
1533                 return Math.max(MAX_VALUES[field], Math.max(v1, v2));
1534             }
1535         }
1536         return MAX_VALUES[field];
1537     }
1538 
1539     /**
1540      * Returns the highest minimum value for the given calendar field
1541      * of this <code>GregorianCalendar</code> instance. The highest
1542      * minimum value is defined as the largest value returned by
1543      * {@link #getActualMinimum(int)} for any possible time value,
1544      * taking into consideration the current values of the
1545      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1546      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1547      * {@link #getGregorianChange() getGregorianChange} and
1548      * {@link Calendar#getTimeZone() getTimeZone} methods.
1549      *
1550      * @param field the calendar field.
1551      * @return the highest minimum value for the given calendar field.
1552      * @see #getMinimum(int)
1553      * @see #getMaximum(int)
1554      * @see #getLeastMaximum(int)
1555      * @see #getActualMinimum(int)
1556      * @see #getActualMaximum(int)
1557      */
getGreatestMinimum(int field)1558     public int getGreatestMinimum(int field) {
1559         if (field == DAY_OF_MONTH) {
1560             BaseCalendar.Date d = getGregorianCutoverDate();
1561             long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);
1562             d = getCalendarDate(mon1);
1563             return Math.max(MIN_VALUES[field], d.getDayOfMonth());
1564         }
1565         return MIN_VALUES[field];
1566     }
1567 
1568     /**
1569      * Returns the lowest maximum value for the given calendar field
1570      * of this <code>GregorianCalendar</code> instance. The lowest
1571      * maximum value is defined as the smallest value returned by
1572      * {@link #getActualMaximum(int)} for any possible time value,
1573      * taking into consideration the current values of the
1574      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1575      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1576      * {@link #getGregorianChange() getGregorianChange} and
1577      * {@link Calendar#getTimeZone() getTimeZone} methods.
1578      *
1579      * @param field the calendar field
1580      * @return the lowest maximum value for the given calendar field.
1581      * @see #getMinimum(int)
1582      * @see #getMaximum(int)
1583      * @see #getGreatestMinimum(int)
1584      * @see #getActualMinimum(int)
1585      * @see #getActualMaximum(int)
1586      */
getLeastMaximum(int field)1587     public int getLeastMaximum(int field) {
1588         switch (field) {
1589         case MONTH:
1590         case DAY_OF_MONTH:
1591         case DAY_OF_YEAR:
1592         case WEEK_OF_YEAR:
1593         case WEEK_OF_MONTH:
1594         case DAY_OF_WEEK_IN_MONTH:
1595         case YEAR:
1596             {
1597                 GregorianCalendar gc = (GregorianCalendar) clone();
1598                 gc.setLenient(true);
1599                 gc.setTimeInMillis(gregorianCutover);
1600                 int v1 = gc.getActualMaximum(field);
1601                 gc.setTimeInMillis(gregorianCutover-1);
1602                 int v2 = gc.getActualMaximum(field);
1603                 return Math.min(LEAST_MAX_VALUES[field], Math.min(v1, v2));
1604             }
1605         }
1606         return LEAST_MAX_VALUES[field];
1607     }
1608 
1609     /**
1610      * Returns the minimum value that this calendar field could have,
1611      * taking into consideration the given time value and the current
1612      * values of the
1613      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1614      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1615      * {@link #getGregorianChange() getGregorianChange} and
1616      * {@link Calendar#getTimeZone() getTimeZone} methods.
1617      *
1618      * <p>For example, if the Gregorian change date is January 10,
1619      * 1970 and the date of this <code>GregorianCalendar</code> is
1620      * January 20, 1970, the actual minimum value of the
1621      * <code>DAY_OF_MONTH</code> field is 10 because the previous date
1622      * of January 10, 1970 is December 27, 1996 (in the Julian
1623      * calendar). Therefore, December 28, 1969 to January 9, 1970
1624      * don't exist.
1625      *
1626      * @param field the calendar field
1627      * @return the minimum of the given field for the time value of
1628      * this <code>GregorianCalendar</code>
1629      * @see #getMinimum(int)
1630      * @see #getMaximum(int)
1631      * @see #getGreatestMinimum(int)
1632      * @see #getLeastMaximum(int)
1633      * @see #getActualMaximum(int)
1634      * @since 1.2
1635      */
getActualMinimum(int field)1636     public int getActualMinimum(int field) {
1637         if (field == DAY_OF_MONTH) {
1638             GregorianCalendar gc = getNormalizedCalendar();
1639             int year = gc.cdate.getNormalizedYear();
1640             if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {
1641                 long month1 = getFixedDateMonth1(gc.cdate, gc.calsys.getFixedDate(gc.cdate));
1642                 BaseCalendar.Date d = getCalendarDate(month1);
1643                 return d.getDayOfMonth();
1644             }
1645         }
1646         return getMinimum(field);
1647     }
1648 
1649     /**
1650      * Returns the maximum value that this calendar field could have,
1651      * taking into consideration the given time value and the current
1652      * values of the
1653      * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
1654      * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
1655      * {@link #getGregorianChange() getGregorianChange} and
1656      * {@link Calendar#getTimeZone() getTimeZone} methods.
1657      * For example, if the date of this instance is February 1, 2004,
1658      * the actual maximum value of the <code>DAY_OF_MONTH</code> field
1659      * is 29 because 2004 is a leap year, and if the date of this
1660      * instance is February 1, 2005, it's 28.
1661      *
1662      * <p>This method calculates the maximum value of {@link
1663      * Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link
1664      * Calendar#YEAR YEAR} (calendar year) value, not the <a
1665      * href="#week_year">week year</a>. Call {@link
1666      * #getWeeksInWeekYear()} to get the maximum value of {@code
1667      * WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.
1668      *
1669      * @param field the calendar field
1670      * @return the maximum of the given field for the time value of
1671      * this <code>GregorianCalendar</code>
1672      * @see #getMinimum(int)
1673      * @see #getMaximum(int)
1674      * @see #getGreatestMinimum(int)
1675      * @see #getLeastMaximum(int)
1676      * @see #getActualMinimum(int)
1677      * @since 1.2
1678      */
getActualMaximum(int field)1679     public int getActualMaximum(int field) {
1680         final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
1681             HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
1682             ZONE_OFFSET_MASK|DST_OFFSET_MASK;
1683         if ((fieldsForFixedMax & (1<<field)) != 0) {
1684             return getMaximum(field);
1685         }
1686 
1687         GregorianCalendar gc = getNormalizedCalendar();
1688         BaseCalendar.Date date = gc.cdate;
1689         BaseCalendar cal = gc.calsys;
1690         int normalizedYear = date.getNormalizedYear();
1691 
1692         int value = -1;
1693         switch (field) {
1694         case MONTH:
1695             {
1696                 if (!gc.isCutoverYear(normalizedYear)) {
1697                     value = DECEMBER;
1698                     break;
1699                 }
1700 
1701                 // January 1 of the next year may or may not exist.
1702                 long nextJan1;
1703                 do {
1704                     nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);
1705                 } while (nextJan1 < gregorianCutoverDate);
1706                 BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
1707                 cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);
1708                 value = d.getMonth() - 1;
1709             }
1710             break;
1711 
1712         case DAY_OF_MONTH:
1713             {
1714                 value = cal.getMonthLength(date);
1715                 if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {
1716                     break;
1717                 }
1718 
1719                 // Handle cutover year.
1720                 long fd = gc.getCurrentFixedDate();
1721                 if (fd >= gregorianCutoverDate) {
1722                     break;
1723                 }
1724                 int monthLength = gc.actualMonthLength();
1725                 long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;
1726                 // Convert the fixed date to its calendar date.
1727                 BaseCalendar.Date d = gc.getCalendarDate(monthEnd);
1728                 value = d.getDayOfMonth();
1729             }
1730             break;
1731 
1732         case DAY_OF_YEAR:
1733             {
1734                 if (!gc.isCutoverYear(normalizedYear)) {
1735                     value = cal.getYearLength(date);
1736                     break;
1737                 }
1738 
1739                 // Handle cutover year.
1740                 long jan1;
1741                 if (gregorianCutoverYear == gregorianCutoverYearJulian) {
1742                     BaseCalendar cocal = gc.getCutoverCalendarSystem();
1743                     jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);
1744                 } else if (normalizedYear == gregorianCutoverYearJulian) {
1745                     jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);
1746                 } else {
1747                     jan1 = gregorianCutoverDate;
1748                 }
1749                 // January 1 of the next year may or may not exist.
1750                 long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);
1751                 if (nextJan1 < gregorianCutoverDate) {
1752                     nextJan1 = gregorianCutoverDate;
1753                 }
1754                 assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
1755                                                 date.getDayOfMonth(), date);
1756                 assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
1757                                                 date.getDayOfMonth(), date);
1758                 value = (int)(nextJan1 - jan1);
1759             }
1760             break;
1761 
1762         case WEEK_OF_YEAR:
1763             {
1764                 if (!gc.isCutoverYear(normalizedYear)) {
1765                     // Get the day of week of January 1 of the year
1766                     CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
1767                     d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
1768                     int dayOfWeek = cal.getDayOfWeek(d);
1769                     // Normalize the day of week with the firstDayOfWeek value
1770                     dayOfWeek -= getFirstDayOfWeek();
1771                     if (dayOfWeek < 0) {
1772                         dayOfWeek += 7;
1773                     }
1774                     value = 52;
1775                     int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
1776                     if ((magic == 6) ||
1777                         (date.isLeapYear() && (magic == 5 || magic == 12))) {
1778                         value++;
1779                     }
1780                     break;
1781                 }
1782 
1783                 if (gc == this) {
1784                     gc = (GregorianCalendar) gc.clone();
1785                 }
1786                 int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
1787                 gc.set(DAY_OF_YEAR, maxDayOfYear);
1788                 value = gc.get(WEEK_OF_YEAR);
1789                 if (internalGet(YEAR) != gc.getWeekYear()) {
1790                     gc.set(DAY_OF_YEAR, maxDayOfYear - 7);
1791                     value = gc.get(WEEK_OF_YEAR);
1792                 }
1793             }
1794             break;
1795 
1796         case WEEK_OF_MONTH:
1797             {
1798                 if (!gc.isCutoverYear(normalizedYear)) {
1799                     CalendarDate d = cal.newCalendarDate(null);
1800                     d.setDate(date.getYear(), date.getMonth(), 1);
1801                     int dayOfWeek = cal.getDayOfWeek(d);
1802                     int monthLength = cal.getMonthLength(d);
1803                     dayOfWeek -= getFirstDayOfWeek();
1804                     if (dayOfWeek < 0) {
1805                         dayOfWeek += 7;
1806                     }
1807                     int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
1808                     value = 3;
1809                     if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
1810                         value++;
1811                     }
1812                     monthLength -= nDaysFirstWeek + 7 * 3;
1813                     if (monthLength > 0) {
1814                         value++;
1815                         if (monthLength > 7) {
1816                             value++;
1817                         }
1818                     }
1819                     break;
1820                 }
1821 
1822                 // Cutover year handling
1823                 if (gc == this) {
1824                     gc = (GregorianCalendar) gc.clone();
1825                 }
1826                 int y = gc.internalGet(YEAR);
1827                 int m = gc.internalGet(MONTH);
1828                 do {
1829                     value = gc.get(WEEK_OF_MONTH);
1830                     gc.add(WEEK_OF_MONTH, +1);
1831                 } while (gc.get(YEAR) == y && gc.get(MONTH) == m);
1832             }
1833             break;
1834 
1835         case DAY_OF_WEEK_IN_MONTH:
1836             {
1837                 // may be in the Gregorian cutover month
1838                 int ndays, dow1;
1839                 int dow = date.getDayOfWeek();
1840                 if (!gc.isCutoverYear(normalizedYear)) {
1841                     BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
1842                     ndays = cal.getMonthLength(d);
1843                     d.setDayOfMonth(1);
1844                     cal.normalize(d);
1845                     dow1 = d.getDayOfWeek();
1846                 } else {
1847                     // Let a cloned GregorianCalendar take care of the cutover cases.
1848                     if (gc == this) {
1849                         gc = (GregorianCalendar) clone();
1850                     }
1851                     ndays = gc.actualMonthLength();
1852                     gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
1853                     dow1 = gc.get(DAY_OF_WEEK);
1854                 }
1855                 int x = dow - dow1;
1856                 if (x < 0) {
1857                     x += 7;
1858                 }
1859                 ndays -= x;
1860                 value = (ndays + 6) / 7;
1861             }
1862             break;
1863 
1864         case YEAR:
1865             /* The year computation is no different, in principle, from the
1866              * others, however, the range of possible maxima is large.  In
1867              * addition, the way we know we've exceeded the range is different.
1868              * For these reasons, we use the special case code below to handle
1869              * this field.
1870              *
1871              * The actual maxima for YEAR depend on the type of calendar:
1872              *
1873              *     Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
1874              *     Julian    = Dec  2, 292269055 BCE - Jan  3, 292272993 CE
1875              *     Hybrid    = Dec  2, 292269055 BCE - Aug 17, 292278994 CE
1876              *
1877              * We know we've exceeded the maximum when either the month, date,
1878              * time, or era changes in response to setting the year.  We don't
1879              * check for month, date, and time here because the year and era are
1880              * sufficient to detect an invalid year setting.  NOTE: If code is
1881              * added to check the month and date in the future for some reason,
1882              * Feb 29 must be allowed to shift to Mar 1 when setting the year.
1883              */
1884             {
1885                 if (gc == this) {
1886                     gc = (GregorianCalendar) clone();
1887                 }
1888 
1889                 // Calculate the millisecond offset from the beginning
1890                 // of the year of this calendar and adjust the max
1891                 // year value if we are beyond the limit in the max
1892                 // year.
1893                 long current = gc.getYearOffsetInMillis();
1894 
1895                 if (gc.internalGetEra() == CE) {
1896                     gc.setTimeInMillis(Long.MAX_VALUE);
1897                     value = gc.get(YEAR);
1898                     long maxEnd = gc.getYearOffsetInMillis();
1899                     if (current > maxEnd) {
1900                         value--;
1901                     }
1902                 } else {
1903                     CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?
1904                         gcal : getJulianCalendarSystem();
1905                     CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
1906                     long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
1907                     maxEnd *= 60;
1908                     maxEnd += d.getMinutes();
1909                     maxEnd *= 60;
1910                     maxEnd += d.getSeconds();
1911                     maxEnd *= 1000;
1912                     maxEnd += d.getMillis();
1913                     value = d.getYear();
1914                     if (value <= 0) {
1915                         assert mincal == gcal;
1916                         value = 1 - value;
1917                     }
1918                     if (current < maxEnd) {
1919                         value--;
1920                     }
1921                 }
1922             }
1923             break;
1924 
1925         default:
1926             throw new ArrayIndexOutOfBoundsException(field);
1927         }
1928         return value;
1929     }
1930 
1931     /**
1932      * Returns the millisecond offset from the beginning of this
1933      * year. This Calendar object must have been normalized.
1934      */
getYearOffsetInMillis()1935     private final long getYearOffsetInMillis() {
1936         long t = (internalGet(DAY_OF_YEAR) - 1) * 24;
1937         t += internalGet(HOUR_OF_DAY);
1938         t *= 60;
1939         t += internalGet(MINUTE);
1940         t *= 60;
1941         t += internalGet(SECOND);
1942         t *= 1000;
1943         return t + internalGet(MILLISECOND) -
1944             (internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));
1945     }
1946 
clone()1947     public Object clone()
1948     {
1949         GregorianCalendar other = (GregorianCalendar) super.clone();
1950 
1951         other.gdate = (BaseCalendar.Date) gdate.clone();
1952         if (cdate != null) {
1953             if (cdate != gdate) {
1954                 other.cdate = (BaseCalendar.Date) cdate.clone();
1955             } else {
1956                 other.cdate = other.gdate;
1957             }
1958         }
1959         other.originalFields = null;
1960         other.zoneOffsets = null;
1961         return other;
1962     }
1963 
getTimeZone()1964     public TimeZone getTimeZone() {
1965         TimeZone zone = super.getTimeZone();
1966         // To share the zone by CalendarDates
1967         gdate.setZone(zone);
1968         if (cdate != null && cdate != gdate) {
1969             cdate.setZone(zone);
1970         }
1971         return zone;
1972     }
1973 
setTimeZone(TimeZone zone)1974     public void setTimeZone(TimeZone zone) {
1975         super.setTimeZone(zone);
1976         // To share the zone by CalendarDates
1977         gdate.setZone(zone);
1978         if (cdate != null && cdate != gdate) {
1979             cdate.setZone(zone);
1980         }
1981     }
1982 
1983     /**
1984      * Returns {@code true} indicating this {@code GregorianCalendar}
1985      * supports week dates.
1986      *
1987      * @return {@code true} (always)
1988      * @see #getWeekYear()
1989      * @see #setWeekDate(int,int,int)
1990      * @see #getWeeksInWeekYear()
1991      * @since 1.7
1992      */
1993     @Override
isWeekDateSupported()1994     public final boolean isWeekDateSupported() {
1995         return true;
1996     }
1997 
1998     /**
1999      * Returns the <a href="#week_year">week year</a> represented by this
2000      * {@code GregorianCalendar}. The dates in the weeks between 1 and the
2001      * maximum week number of the week year have the same week year value
2002      * that may be one year before or after the {@link Calendar#YEAR YEAR}
2003      * (calendar year) value.
2004      *
2005      * <p>This method calls {@link Calendar#complete()} before
2006      * calculating the week year.
2007      *
2008      * @return the week year represented by this {@code GregorianCalendar}.
2009      *         If the {@link Calendar#ERA ERA} value is {@link #BC}, the year is
2010      *         represented by 0 or a negative number: BC 1 is 0, BC 2
2011      *         is -1, BC 3 is -2, and so on.
2012      * @throws IllegalArgumentException
2013      *         if any of the calendar fields is invalid in non-lenient mode.
2014      * @see #isWeekDateSupported()
2015      * @see #getWeeksInWeekYear()
2016      * @see Calendar#getFirstDayOfWeek()
2017      * @see Calendar#getMinimalDaysInFirstWeek()
2018      * @since 1.7
2019      */
2020     @Override
getWeekYear()2021     public int getWeekYear() {
2022         int year = get(YEAR); // implicitly calls complete()
2023         if (internalGetEra() == BCE) {
2024             year = 1 - year;
2025         }
2026 
2027         // Fast path for the Gregorian calendar years that are never
2028         // affected by the Julian-Gregorian transition
2029         if (year > gregorianCutoverYear + 1) {
2030             int weekOfYear = internalGet(WEEK_OF_YEAR);
2031             if (internalGet(MONTH) == JANUARY) {
2032                 if (weekOfYear >= 52) {
2033                     --year;
2034                 }
2035             } else {
2036                 if (weekOfYear == 1) {
2037                     ++year;
2038                 }
2039             }
2040             return year;
2041         }
2042 
2043         // General (slow) path
2044         int dayOfYear = internalGet(DAY_OF_YEAR);
2045         int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
2046         int minimalDays = getMinimalDaysInFirstWeek();
2047 
2048         // Quickly check the possibility of year adjustments before
2049         // cloning this GregorianCalendar.
2050         if (dayOfYear > minimalDays && dayOfYear < (maxDayOfYear - 6)) {
2051             return year;
2052         }
2053 
2054         // Create a clone to work on the calculation
2055         GregorianCalendar cal = (GregorianCalendar) clone();
2056         cal.setLenient(true);
2057         // Use GMT so that intermediate date calculations won't
2058         // affect the time of day fields.
2059         cal.setTimeZone(TimeZone.getTimeZone("GMT"));
2060         // Go to the first day of the year, which is usually January 1.
2061         cal.set(DAY_OF_YEAR, 1);
2062         cal.complete();
2063 
2064         // Get the first day of the first day-of-week in the year.
2065         int delta = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2066         if (delta != 0) {
2067             if (delta < 0) {
2068                 delta += 7;
2069             }
2070             cal.add(DAY_OF_YEAR, delta);
2071         }
2072         int minDayOfYear = cal.get(DAY_OF_YEAR);
2073         if (dayOfYear < minDayOfYear) {
2074             if (minDayOfYear <= minimalDays) {
2075                 --year;
2076             }
2077         } else {
2078             cal.set(YEAR, year + 1);
2079             cal.set(DAY_OF_YEAR, 1);
2080             cal.complete();
2081             int del = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2082             if (del != 0) {
2083                 if (del < 0) {
2084                     del += 7;
2085                 }
2086                 cal.add(DAY_OF_YEAR, del);
2087             }
2088             minDayOfYear = cal.get(DAY_OF_YEAR) - 1;
2089             if (minDayOfYear == 0) {
2090                 minDayOfYear = 7;
2091             }
2092             if (minDayOfYear >= minimalDays) {
2093                 int days = maxDayOfYear - dayOfYear + 1;
2094                 if (days <= (7 - minDayOfYear)) {
2095                     ++year;
2096                 }
2097             }
2098         }
2099         return year;
2100     }
2101 
2102     /**
2103      * Sets this {@code GregorianCalendar} to the date given by the
2104      * date specifiers - <a href="#week_year">{@code weekYear}</a>,
2105      * {@code weekOfYear}, and {@code dayOfWeek}. {@code weekOfYear}
2106      * follows the <a href="#week_and_year">{@code WEEK_OF_YEAR}
2107      * numbering</a>.  The {@code dayOfWeek} value must be one of the
2108      * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} values: {@link
2109      * Calendar#SUNDAY SUNDAY} to {@link Calendar#SATURDAY SATURDAY}.
2110      *
2111      * <p>Note that the numeric day-of-week representation differs from
2112      * the ISO 8601 standard, and that the {@code weekOfYear}
2113      * numbering is compatible with the standard when {@code
2114      * getFirstDayOfWeek()} is {@code MONDAY} and {@code
2115      * getMinimalDaysInFirstWeek()} is 4.
2116      *
2117      * <p>Unlike the {@code set} method, all of the calendar fields
2118      * and the instant of time value are calculated upon return.
2119      *
2120      * <p>If {@code weekOfYear} is out of the valid week-of-year
2121      * range in {@code weekYear}, the {@code weekYear}
2122      * and {@code weekOfYear} values are adjusted in lenient
2123      * mode, or an {@code IllegalArgumentException} is thrown in
2124      * non-lenient mode.
2125      *
2126      * @param weekYear    the week year
2127      * @param weekOfYear  the week number based on {@code weekYear}
2128      * @param dayOfWeek   the day of week value: one of the constants
2129      *                    for the {@link #DAY_OF_WEEK DAY_OF_WEEK} field:
2130      *                    {@link Calendar#SUNDAY SUNDAY}, ...,
2131      *                    {@link Calendar#SATURDAY SATURDAY}.
2132      * @exception IllegalArgumentException
2133      *            if any of the given date specifiers is invalid,
2134      *            or if any of the calendar fields are inconsistent
2135      *            with the given date specifiers in non-lenient mode
2136      * @see GregorianCalendar#isWeekDateSupported()
2137      * @see Calendar#getFirstDayOfWeek()
2138      * @see Calendar#getMinimalDaysInFirstWeek()
2139      * @since 1.7
2140      */
2141     @Override
setWeekDate(int weekYear, int weekOfYear, int dayOfWeek)2142     public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {
2143         if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
2144             throw new IllegalArgumentException("invalid dayOfWeek: " + dayOfWeek);
2145         }
2146 
2147         // To avoid changing the time of day fields by date
2148         // calculations, use a clone with the GMT time zone.
2149         GregorianCalendar gc = (GregorianCalendar) clone();
2150         gc.setLenient(true);
2151         int era = gc.get(ERA);
2152         gc.clear();
2153         gc.setTimeZone(TimeZone.getTimeZone("GMT"));
2154         gc.set(ERA, era);
2155         gc.set(YEAR, weekYear);
2156         gc.set(WEEK_OF_YEAR, 1);
2157         gc.set(DAY_OF_WEEK, getFirstDayOfWeek());
2158         int days = dayOfWeek - getFirstDayOfWeek();
2159         if (days < 0) {
2160             days += 7;
2161         }
2162         days += 7 * (weekOfYear - 1);
2163         if (days != 0) {
2164             gc.add(DAY_OF_YEAR, days);
2165         } else {
2166             gc.complete();
2167         }
2168 
2169         if (!isLenient() &&
2170             (gc.getWeekYear() != weekYear
2171              || gc.internalGet(WEEK_OF_YEAR) != weekOfYear
2172              || gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {
2173             throw new IllegalArgumentException();
2174         }
2175 
2176         set(ERA, gc.internalGet(ERA));
2177         set(YEAR, gc.internalGet(YEAR));
2178         set(MONTH, gc.internalGet(MONTH));
2179         set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));
2180 
2181         // to avoid throwing an IllegalArgumentException in
2182         // non-lenient, set WEEK_OF_YEAR internally
2183         internalSet(WEEK_OF_YEAR, weekOfYear);
2184         complete();
2185     }
2186 
2187     /**
2188      * Returns the number of weeks in the <a href="#week_year">week year</a>
2189      * represented by this {@code GregorianCalendar}.
2190      *
2191      * <p>For example, if this {@code GregorianCalendar}'s date is
2192      * December 31, 2008 with <a href="#iso8601_compatible_setting">the ISO
2193      * 8601 compatible setting</a>, this method will return 53 for the
2194      * period: December 29, 2008 to January 3, 2010 while {@link
2195      * #getActualMaximum(int) getActualMaximum(WEEK_OF_YEAR)} will return
2196      * 52 for the period: December 31, 2007 to December 28, 2008.
2197      *
2198      * @return the number of weeks in the week year.
2199      * @see Calendar#WEEK_OF_YEAR
2200      * @see #getWeekYear()
2201      * @see #getActualMaximum(int)
2202      * @since 1.7
2203      */
getWeeksInWeekYear()2204     public int getWeeksInWeekYear() {
2205         GregorianCalendar gc = getNormalizedCalendar();
2206         int weekYear = gc.getWeekYear();
2207         if (weekYear == gc.internalGet(YEAR)) {
2208             return gc.getActualMaximum(WEEK_OF_YEAR);
2209         }
2210 
2211         // Use the 2nd week for calculating the max of WEEK_OF_YEAR
2212         if (gc == this) {
2213             gc = (GregorianCalendar) gc.clone();
2214         }
2215         gc.setWeekDate(weekYear, 2, internalGet(DAY_OF_WEEK));
2216         return gc.getActualMaximum(WEEK_OF_YEAR);
2217     }
2218 
2219 /////////////////////////////
2220 // Time => Fields computation
2221 /////////////////////////////
2222 
2223     /**
2224      * The fixed date corresponding to gdate. If the value is
2225      * Long.MIN_VALUE, the fixed date value is unknown. Currently,
2226      * Julian calendar dates are not cached.
2227      */
2228     transient private long cachedFixedDate = Long.MIN_VALUE;
2229 
2230     /**
2231      * Converts the time value (millisecond offset from the <a
2232      * href="Calendar.html#Epoch">Epoch</a>) to calendar field values.
2233      * The time is <em>not</em>
2234      * recomputed first; to recompute the time, then the fields, call the
2235      * <code>complete</code> method.
2236      *
2237      * @see Calendar#complete
2238      */
computeFields()2239     protected void computeFields() {
2240         int mask = 0;
2241         if (isPartiallyNormalized()) {
2242             // Determine which calendar fields need to be computed.
2243             mask = getSetStateFields();
2244             int fieldMask = ~mask & ALL_FIELDS;
2245             // We have to call computTime in case calsys == null in
2246             // order to set calsys and cdate. (6263644)
2247             if (fieldMask != 0 || calsys == null) {
2248                 mask |= computeFields(fieldMask,
2249                                       mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));
2250                 assert mask == ALL_FIELDS;
2251             }
2252         } else {
2253             mask = ALL_FIELDS;
2254             computeFields(mask, 0);
2255         }
2256         // After computing all the fields, set the field state to `COMPUTED'.
2257         setFieldsComputed(mask);
2258     }
2259 
2260     /**
2261      * This computeFields implements the conversion from UTC
2262      * (millisecond offset from the Epoch) to calendar
2263      * field values. fieldMask specifies which fields to change the
2264      * setting state to COMPUTED, although all fields are set to
2265      * the correct values. This is required to fix 4685354.
2266      *
2267      * @param fieldMask a bit mask to specify which fields to change
2268      * the setting state.
2269      * @param tzMask a bit mask to specify which time zone offset
2270      * fields to be used for time calculations
2271      * @return a new field mask that indicates what field values have
2272      * actually been set.
2273      */
computeFields(int fieldMask, int tzMask)2274     private int computeFields(int fieldMask, int tzMask) {
2275         int zoneOffset = 0;
2276         TimeZone tz = getZone();
2277         if (zoneOffsets == null) {
2278             zoneOffsets = new int[2];
2279         }
2280         if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
2281             if (tz instanceof ZoneInfo) {
2282                 ZoneInfo zoneInfo = (ZoneInfo) tz;
2283                 zoneOffset = zoneInfo.getOffsetsByUtcTime(time, zoneOffsets);
2284             } else {
2285                 zoneOffset = tz.getOffset(time);
2286                 zoneOffsets[0] = tz.getRawOffset();
2287                 zoneOffsets[1] = zoneOffset - zoneOffsets[0];
2288             }
2289         }
2290         if (tzMask != 0) {
2291             if (isFieldSet(tzMask, ZONE_OFFSET)) {
2292                 zoneOffsets[0] = internalGet(ZONE_OFFSET);
2293             }
2294             if (isFieldSet(tzMask, DST_OFFSET)) {
2295                 zoneOffsets[1] = internalGet(DST_OFFSET);
2296             }
2297             zoneOffset = zoneOffsets[0] + zoneOffsets[1];
2298         }
2299 
2300         // By computing time and zoneOffset separately, we can take
2301         // the wider range of time+zoneOffset than the previous
2302         // implementation.
2303         long fixedDate = zoneOffset / ONE_DAY;
2304         int timeOfDay = zoneOffset % (int)ONE_DAY;
2305         fixedDate += time / ONE_DAY;
2306         timeOfDay += (int) (time % ONE_DAY);
2307         if (timeOfDay >= ONE_DAY) {
2308             timeOfDay -= ONE_DAY;
2309             ++fixedDate;
2310         } else {
2311             while (timeOfDay < 0) {
2312                 timeOfDay += ONE_DAY;
2313                 --fixedDate;
2314             }
2315         }
2316         fixedDate += EPOCH_OFFSET;
2317 
2318         int era = CE;
2319         int year;
2320         if (fixedDate >= gregorianCutoverDate) {
2321             // Handle Gregorian dates.
2322             assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized()
2323                         : "cache control: not normalized";
2324             assert cachedFixedDate == Long.MIN_VALUE ||
2325                    gcal.getFixedDate(gdate.getNormalizedYear(),
2326                                           gdate.getMonth(),
2327                                           gdate.getDayOfMonth(), gdate)
2328                                 == cachedFixedDate
2329                         : "cache control: inconsictency" +
2330                           ", cachedFixedDate=" + cachedFixedDate +
2331                           ", computed=" +
2332                           gcal.getFixedDate(gdate.getNormalizedYear(),
2333                                                  gdate.getMonth(),
2334                                                  gdate.getDayOfMonth(),
2335                                                  gdate) +
2336                           ", date=" + gdate;
2337 
2338             // See if we can use gdate to avoid date calculation.
2339             if (fixedDate != cachedFixedDate) {
2340                 gcal.getCalendarDateFromFixedDate(gdate, fixedDate);
2341                 cachedFixedDate = fixedDate;
2342             }
2343 
2344             year = gdate.getYear();
2345             if (year <= 0) {
2346                 year = 1 - year;
2347                 era = BCE;
2348             }
2349             calsys = gcal;
2350             cdate = gdate;
2351             assert cdate.getDayOfWeek() > 0 : "dow="+cdate.getDayOfWeek()+", date="+cdate;
2352         } else {
2353             // Handle Julian calendar dates.
2354             calsys = getJulianCalendarSystem();
2355             cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());
2356             jcal.getCalendarDateFromFixedDate(cdate, fixedDate);
2357             Era e = cdate.getEra();
2358             if (e == jeras[0]) {
2359                 era = BCE;
2360             }
2361             year = cdate.getYear();
2362         }
2363 
2364         // Always set the ERA and YEAR values.
2365         internalSet(ERA, era);
2366         internalSet(YEAR, year);
2367         int mask = fieldMask | (ERA_MASK|YEAR_MASK);
2368 
2369         int month =  cdate.getMonth() - 1; // 0-based
2370         int dayOfMonth = cdate.getDayOfMonth();
2371 
2372         // Set the basic date fields.
2373         if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))
2374             != 0) {
2375             internalSet(MONTH, month);
2376             internalSet(DAY_OF_MONTH, dayOfMonth);
2377             internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());
2378             mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;
2379         }
2380 
2381         if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
2382                           |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {
2383             if (timeOfDay != 0) {
2384                 int hours = timeOfDay / ONE_HOUR;
2385                 internalSet(HOUR_OF_DAY, hours);
2386                 internalSet(AM_PM, hours / 12); // Assume AM == 0
2387                 internalSet(HOUR, hours % 12);
2388                 int r = timeOfDay % ONE_HOUR;
2389                 internalSet(MINUTE, r / ONE_MINUTE);
2390                 r %= ONE_MINUTE;
2391                 internalSet(SECOND, r / ONE_SECOND);
2392                 internalSet(MILLISECOND, r % ONE_SECOND);
2393             } else {
2394                 internalSet(HOUR_OF_DAY, 0);
2395                 internalSet(AM_PM, AM);
2396                 internalSet(HOUR, 0);
2397                 internalSet(MINUTE, 0);
2398                 internalSet(SECOND, 0);
2399                 internalSet(MILLISECOND, 0);
2400             }
2401             mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
2402                      |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);
2403         }
2404 
2405         if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {
2406             internalSet(ZONE_OFFSET, zoneOffsets[0]);
2407             internalSet(DST_OFFSET, zoneOffsets[1]);
2408             mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
2409         }
2410 
2411         if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
2412             int normalizedYear = cdate.getNormalizedYear();
2413             long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);
2414             int dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
2415             long fixedDateMonth1 = fixedDate - dayOfMonth + 1;
2416             int cutoverGap = 0;
2417             int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
2418             int relativeDayOfMonth = dayOfMonth - 1;
2419 
2420             // If we are in the cutover year, we need some special handling.
2421             if (normalizedYear == cutoverYear) {
2422                 // Need to take care of the "missing" days.
2423                 if (gregorianCutoverYearJulian <= gregorianCutoverYear) {
2424                     // We need to find out where we are. The cutover
2425                     // gap could even be more than one year.  (One
2426                     // year difference in ~48667 years.)
2427                     fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);
2428                     if (fixedDate >= gregorianCutoverDate) {
2429                         fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);
2430                     }
2431                 }
2432                 int realDayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
2433                 cutoverGap = dayOfYear - realDayOfYear;
2434                 dayOfYear = realDayOfYear;
2435                 relativeDayOfMonth = (int)(fixedDate - fixedDateMonth1);
2436             }
2437             internalSet(DAY_OF_YEAR, dayOfYear);
2438             internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);
2439 
2440             int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
2441 
2442             // The spec is to calculate WEEK_OF_YEAR in the
2443             // ISO8601-style. This creates problems, though.
2444             if (weekOfYear == 0) {
2445                 // If the date belongs to the last week of the
2446                 // previous year, use the week number of "12/31" of
2447                 // the "previous" year. Again, if the previous year is
2448                 // the Gregorian cutover year, we need to take care of
2449                 // it.  Usually the previous day of January 1 is
2450                 // December 31, which is not always true in
2451                 // GregorianCalendar.
2452                 long fixedDec31 = fixedDateJan1 - 1;
2453                 long prevJan1  = fixedDateJan1 - 365;
2454                 if (normalizedYear > (cutoverYear + 1)) {
2455                     if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {
2456                         --prevJan1;
2457                     }
2458                 } else if (normalizedYear <= gregorianCutoverYearJulian) {
2459                     if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {
2460                         --prevJan1;
2461                     }
2462                 } else {
2463                     BaseCalendar calForJan1 = calsys;
2464                     //int prevYear = normalizedYear - 1;
2465                     int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();
2466                     if (prevYear == gregorianCutoverYear) {
2467                         calForJan1 = getCutoverCalendarSystem();
2468                         if (calForJan1 == jcal) {
2469                             prevJan1 = calForJan1.getFixedDate(prevYear,
2470                                                                BaseCalendar.JANUARY,
2471                                                                1,
2472                                                                null);
2473                         } else {
2474                             prevJan1 = gregorianCutoverDate;
2475                             calForJan1 = gcal;
2476                         }
2477                     } else if (prevYear <= gregorianCutoverYearJulian) {
2478                         calForJan1 = getJulianCalendarSystem();
2479                         prevJan1 = calForJan1.getFixedDate(prevYear,
2480                                                            BaseCalendar.JANUARY,
2481                                                            1,
2482                                                            null);
2483                     }
2484                 }
2485                 weekOfYear = getWeekNumber(prevJan1, fixedDec31);
2486             } else {
2487                 if (normalizedYear > gregorianCutoverYear ||
2488                     normalizedYear < (gregorianCutoverYearJulian - 1)) {
2489                     // Regular years
2490                     if (weekOfYear >= 52) {
2491                         long nextJan1 = fixedDateJan1 + 365;
2492                         if (cdate.isLeapYear()) {
2493                             nextJan1++;
2494                         }
2495                         long nextJan1st = calsys.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
2496                                                                             getFirstDayOfWeek());
2497                         int ndays = (int)(nextJan1st - nextJan1);
2498                         if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
2499                             // The first days forms a week in which the date is included.
2500                             weekOfYear = 1;
2501                         }
2502                     }
2503                 } else {
2504                     BaseCalendar calForJan1 = calsys;
2505                     int nextYear = normalizedYear + 1;
2506                     if (nextYear == (gregorianCutoverYearJulian + 1) &&
2507                         nextYear < gregorianCutoverYear) {
2508                         // In case the gap is more than one year.
2509                         nextYear = gregorianCutoverYear;
2510                     }
2511                     if (nextYear == gregorianCutoverYear) {
2512                         calForJan1 = getCutoverCalendarSystem();
2513                     }
2514 
2515                     long nextJan1;
2516                     if (nextYear > gregorianCutoverYear
2517                         || gregorianCutoverYearJulian == gregorianCutoverYear
2518                         || nextYear == gregorianCutoverYearJulian) {
2519                         nextJan1 = calForJan1.getFixedDate(nextYear,
2520                                                            BaseCalendar.JANUARY,
2521                                                            1,
2522                                                            null);
2523                     } else {
2524                         nextJan1 = gregorianCutoverDate;
2525                         calForJan1 = gcal;
2526                     }
2527 
2528                     long nextJan1st = calForJan1.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
2529                                                                             getFirstDayOfWeek());
2530                     int ndays = (int)(nextJan1st - nextJan1);
2531                     if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
2532                         // The first days forms a week in which the date is included.
2533                         weekOfYear = 1;
2534                     }
2535                 }
2536             }
2537             internalSet(WEEK_OF_YEAR, weekOfYear);
2538             internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
2539             mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK);
2540         }
2541         return mask;
2542     }
2543 
2544     /**
2545      * Returns the number of weeks in a period between fixedDay1 and
2546      * fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule
2547      * is applied to calculate the number of weeks.
2548      *
2549      * @param fixedDay1 the fixed date of the first day of the period
2550      * @param fixedDate the fixed date of the last day of the period
2551      * @return the number of weeks of the given period
2552      */
getWeekNumber(long fixedDay1, long fixedDate)2553     private final int getWeekNumber(long fixedDay1, long fixedDate) {
2554         // We can always use `gcal' since Julian and Gregorian are the
2555         // same thing for this calculation.
2556         long fixedDay1st = gcal.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,
2557                                                            getFirstDayOfWeek());
2558         int ndays = (int)(fixedDay1st - fixedDay1);
2559         assert ndays <= 7;
2560         if (ndays >= getMinimalDaysInFirstWeek()) {
2561             fixedDay1st -= 7;
2562         }
2563         int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);
2564         if (normalizedDayOfPeriod >= 0) {
2565             return normalizedDayOfPeriod / 7 + 1;
2566         }
2567         return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;
2568     }
2569 
2570     /**
2571      * Converts calendar field values to the time value (millisecond
2572      * offset from the <a href="Calendar.html#Epoch">Epoch</a>).
2573      *
2574      * @exception IllegalArgumentException if any calendar fields are invalid.
2575      */
computeTime()2576     protected void computeTime() {
2577         // In non-lenient mode, perform brief checking of calendar
2578         // fields which have been set externally. Through this
2579         // checking, the field values are stored in originalFields[]
2580         // to see if any of them are normalized later.
2581         if (!isLenient()) {
2582             if (originalFields == null) {
2583                 originalFields = new int[FIELD_COUNT];
2584             }
2585             for (int field = 0; field < FIELD_COUNT; field++) {
2586                 int value = internalGet(field);
2587                 if (isExternallySet(field)) {
2588                     // Quick validation for any out of range values
2589                     if (value < getMinimum(field) || value > getMaximum(field)) {
2590                         throw new IllegalArgumentException(getFieldName(field));
2591                     }
2592                 }
2593                 originalFields[field] = value;
2594             }
2595         }
2596 
2597         // Let the super class determine which calendar fields to be
2598         // used to calculate the time.
2599         int fieldMask = selectFields();
2600 
2601         // The year defaults to the epoch start. We don't check
2602         // fieldMask for YEAR because YEAR is a mandatory field to
2603         // determine the date.
2604         int year = isSet(YEAR) ? internalGet(YEAR) : EPOCH_YEAR;
2605 
2606         int era = internalGetEra();
2607         if (era == BCE) {
2608             year = 1 - year;
2609         } else if (era != CE) {
2610             // Even in lenient mode we disallow ERA values other than CE & BCE.
2611             // (The same normalization rule as add()/roll() could be
2612             // applied here in lenient mode. But this checking is kept
2613             // unchanged for compatibility as of 1.5.)
2614             throw new IllegalArgumentException("Invalid era");
2615         }
2616 
2617         // If year is 0 or negative, we need to set the ERA value later.
2618         if (year <= 0 && !isSet(ERA)) {
2619             fieldMask |= ERA_MASK;
2620             setFieldsComputed(ERA_MASK);
2621         }
2622 
2623         // Calculate the time of day. We rely on the convention that
2624         // an UNSET field has 0.
2625         long timeOfDay = 0;
2626         if (isFieldSet(fieldMask, HOUR_OF_DAY)) {
2627             timeOfDay += (long) internalGet(HOUR_OF_DAY);
2628         } else {
2629             timeOfDay += internalGet(HOUR);
2630             // The default value of AM_PM is 0 which designates AM.
2631             if (isFieldSet(fieldMask, AM_PM)) {
2632                 timeOfDay += 12 * internalGet(AM_PM);
2633             }
2634         }
2635         timeOfDay *= 60;
2636         timeOfDay += internalGet(MINUTE);
2637         timeOfDay *= 60;
2638         timeOfDay += internalGet(SECOND);
2639         timeOfDay *= 1000;
2640         timeOfDay += internalGet(MILLISECOND);
2641 
2642         // Convert the time of day to the number of days and the
2643         // millisecond offset from midnight.
2644         long fixedDate = timeOfDay / ONE_DAY;
2645         timeOfDay %= ONE_DAY;
2646         while (timeOfDay < 0) {
2647             timeOfDay += ONE_DAY;
2648             --fixedDate;
2649         }
2650 
2651         // Calculate the fixed date since January 1, 1 (Gregorian).
2652         calculateFixedDate: {
2653             long gfd, jfd;
2654             if (year > gregorianCutoverYear && year > gregorianCutoverYearJulian) {
2655                 gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
2656                 if (gfd >= gregorianCutoverDate) {
2657                     fixedDate = gfd;
2658                     break calculateFixedDate;
2659                 }
2660                 jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2661             } else if (year < gregorianCutoverYear && year < gregorianCutoverYearJulian) {
2662                 jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2663                 if (jfd < gregorianCutoverDate) {
2664                     fixedDate = jfd;
2665                     break calculateFixedDate;
2666                 }
2667                 gfd = jfd;
2668             } else {
2669                 jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
2670                 gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
2671             }
2672 
2673             // Now we have to determine which calendar date it is.
2674 
2675             // If the date is relative from the beginning of the year
2676             // in the Julian calendar, then use jfd;
2677             if (isFieldSet(fieldMask, DAY_OF_YEAR) || isFieldSet(fieldMask, WEEK_OF_YEAR)) {
2678                 if (gregorianCutoverYear == gregorianCutoverYearJulian) {
2679                     fixedDate = jfd;
2680                     break calculateFixedDate;
2681                 } else if (year == gregorianCutoverYear) {
2682                     fixedDate = gfd;
2683                     break calculateFixedDate;
2684                 }
2685             }
2686 
2687             if (gfd >= gregorianCutoverDate) {
2688                 if (jfd >= gregorianCutoverDate) {
2689                     fixedDate = gfd;
2690                 } else {
2691                     // The date is in an "overlapping" period. No way
2692                     // to disambiguate it. Determine it using the
2693                     // previous date calculation.
2694                     if (calsys == gcal || calsys == null) {
2695                         fixedDate = gfd;
2696                     } else {
2697                         fixedDate = jfd;
2698                     }
2699                 }
2700             } else {
2701                 if (jfd < gregorianCutoverDate) {
2702                     fixedDate = jfd;
2703                 } else {
2704                     // The date is in a "missing" period.
2705                     if (!isLenient()) {
2706                         throw new IllegalArgumentException("the specified date doesn't exist");
2707                     }
2708                     // Take the Julian date for compatibility, which
2709                     // will produce a Gregorian date.
2710                     fixedDate = jfd;
2711                 }
2712             }
2713         }
2714 
2715         // millis represents local wall-clock time in milliseconds.
2716         long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;
2717 
2718         // Compute the time zone offset and DST offset.  There are two potential
2719         // ambiguities here.  We'll assume a 2:00 am (wall time) switchover time
2720         // for discussion purposes here.
2721         // 1. The transition into DST.  Here, a designated time of 2:00 am - 2:59 am
2722         //    can be in standard or in DST depending.  However, 2:00 am is an invalid
2723         //    representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).
2724         //    We assume standard time.
2725         // 2. The transition out of DST.  Here, a designated time of 1:00 am - 1:59 am
2726         //    can be in standard or DST.  Both are valid representations (the rep
2727         //    jumps from 1:59:59 DST to 1:00:00 Std).
2728         //    Again, we assume standard time.
2729         // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET
2730         // or DST_OFFSET fields; then we use those fields.
2731         TimeZone zone = getZone();
2732 
2733         int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
2734 
2735         millis = adjustForZoneAndDaylightSavingsTime(tzMask, millis, zone);
2736 
2737         // Set this calendar's time in milliseconds
2738         time = millis;
2739 
2740         int mask = computeFields(fieldMask | getSetStateFields(), tzMask);
2741 
2742         if (!isLenient()) {
2743             for (int field = 0; field < FIELD_COUNT; field++) {
2744                 if (!isExternallySet(field)) {
2745                     continue;
2746                 }
2747                 if (originalFields[field] != internalGet(field)) {
2748                     String s = originalFields[field] + " -> " + internalGet(field);
2749                     // Restore the original field values
2750                     System.arraycopy(originalFields, 0, fields, 0, fields.length);
2751                     throw new IllegalArgumentException(getFieldName(field) + ": " + s);
2752                 }
2753             }
2754         }
2755         setFieldsNormalized(mask);
2756     }
2757 
2758     /**
2759      * Calculates the time in milliseconds that this calendar represents using the UTC time,
2760      * timezone information (specifically Daylight Savings Time (DST) rules, if any) and knowledge
2761      * of what fields were explicitly set on the calendar.
2762      *
2763      * <p>A time is represented as the number of milliseconds since
2764      * <i>1st January 1970 00:00:00.000 UTC</i>.
2765      *
2766      * <p>This uses the terms {@link SimpleTimeZone#STANDARD_TIME standard time},
2767      * {@link SimpleTimeZone#WALL_TIME} wall time} and {@link SimpleTimeZone#UTC_TIME UTC time} as
2768      * used in {@link SimpleTimeZone}. Specifically:
2769      *
2770      * <dl>
2771      * <dt><b>UTC time</b></dt>
2772      * <dd>This is the time within the UTC time zone. UTC does not support DST so the UTC time,
2773      * standard time and wall time are all identical within the UTC time zone.</dd>
2774      * <dt><b>standard time</b></dt>
2775      * <dd>This is the local time within the time zone and is not affected by DST.</dd>
2776      * <dt><b>wall time</b></dt>
2777      * <dd>This is the local time within the time zone as shown on a wall clock. If the time zone
2778      * supports DST then it will be the same as <b>standard time</b> when outside DST and it will
2779      * differ (usually be an hour later) when inside DST. This is what the fields on the Calendar
2780      * represent.</dd>
2781      * </dl>
2782      *
2783      * <p>The {@code utcTimeInMillis} value supplied was calculated as if the fields represented
2784      * a standard time in the {@code UTC} time zone. It is the value that would be returned by
2785      * {@link #getTimeInMillis()} when called on this calendar if it was in UTC time zone. e.g. If
2786      * the calendar was set to say <i>2014 March 19th 13:27.53 -08:00</i> then the value of
2787      * {@code utcTimeInMillis} would be the value of {@link #getTimeInMillis()} when called on a
2788      * calendar set to <i>2014 March 19th 13:27.53 -00:00</i>, note the time zone offset is set to
2789      * 0.
2790      *
2791      * <p>To adjust from a UTC time in millis to the standard time in millis we must
2792      * <em>subtract</em> the offset from UTC. e.g. given an offset of UTC-08:00, to convert
2793      * "14:00 UTC" to "14:00 UTC-08:00" we must subtract -08:00 (i.e. add 8 hours). Another way to
2794      * think about it is that 8 hours has to elapse after 14:00 UTC before it is 14:00 UTC-08:00.
2795      *
2796      * <p>As the zone offset can depend on the time and we cannot calculate the time properly until
2797      * we know the time there is a bit of a catch-22. So, what this does is use the
2798      * {@link TimeZone#getRawOffset() raw offset} to calculate a ballpark standard time and then
2799      * uses that value to retrieve the appropriate zone and DST offsets from the time zone. They
2800      * are then used to make the final wall time calculation.
2801      *
2802      * <p>The DST offset will need clearing if the standard time is not a valid wall clock. See
2803      * {@link #adjustDstOffsetForInvalidWallClock(long, TimeZone, int)} for more information.
2804      *
2805      * @param tzMask the set of time zone related fields, i.e. {@link #ZONE_OFFSET_MASK} and
2806      * {@link #DST_OFFSET_MASK}
2807      * @param utcTimeInMillis the time in millis, calculated assuming the time zone was GMT.
2808      * @param zone the actual time zone.
2809      * @return the UTC time in millis after adjusting for zone and DST offset.
2810      */
adjustForZoneAndDaylightSavingsTime( int tzMask, long utcTimeInMillis, TimeZone zone)2811     private long adjustForZoneAndDaylightSavingsTime(
2812             int tzMask, long utcTimeInMillis, TimeZone zone) {
2813 
2814         // The following don't actually need to be initialized because they are always set before
2815         // they are used but the compiler cannot detect that.
2816         int zoneOffset = 0;
2817         int dstOffset = 0;
2818 
2819         // If either of the ZONE_OFFSET or DST_OFFSET fields are not set then get the information
2820         // from the TimeZone.
2821         if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
2822             if (zoneOffsets == null) {
2823                 zoneOffsets = new int[2];
2824             }
2825             int gmtOffset = isFieldSet(tzMask, ZONE_OFFSET) ?
2826                                 internalGet(ZONE_OFFSET) : zone.getRawOffset();
2827 
2828             // Calculate the standard time (no DST) in the supplied zone. This is a ballpark figure
2829             // and not used in the final calculation as the offset used here may not be the same as
2830             // the actual offset the time zone requires be used for this time. This is to handle
2831             // situations like Honolulu, where its raw offset changed from GMT-10:30 to GMT-10:00
2832             // in 1947. The TimeZone always uses a raw offset of -10:00 but will return -10:30
2833             // for dates before the change over.
2834             long standardTimeInZone = utcTimeInMillis - gmtOffset;
2835 
2836             // Retrieve the correct zone and DST offsets from the time zone.
2837             if (zone instanceof ZoneInfo) {
2838                 ZoneInfo zoneInfo = (ZoneInfo) zone;
2839                 zoneInfo.getOffsetsByUtcTime(standardTimeInZone, zoneOffsets);
2840             } else {
2841                 zone.getOffsets(standardTimeInZone, zoneOffsets);
2842             }
2843             zoneOffset = zoneOffsets[0];
2844             dstOffset = zoneOffsets[1];
2845 
2846             // If necessary adjust the DST offset to handle an invalid wall clock sensibly.
2847             dstOffset = adjustDstOffsetForInvalidWallClock(standardTimeInZone, zone, dstOffset);
2848         }
2849 
2850         // If either ZONE_OFFSET of DST_OFFSET fields are set then get the information from the
2851         // fields, potentially overriding information from the TimeZone.
2852         if (tzMask != 0) {
2853             if (isFieldSet(tzMask, ZONE_OFFSET)) {
2854                 zoneOffset = internalGet(ZONE_OFFSET);
2855             }
2856             if (isFieldSet(tzMask, DST_OFFSET)) {
2857                 dstOffset = internalGet(DST_OFFSET);
2858             }
2859         }
2860 
2861         // Adjust the time zone offset values to get the UTC time.
2862         long standardTimeInZone = utcTimeInMillis - zoneOffset;
2863         return standardTimeInZone - dstOffset;
2864     }
2865 
2866     /**
2867      * If the supplied millis is in daylight savings time (DST) and is the result of an invalid
2868      * wall clock then adjust the DST offset to ensure sensible behavior.
2869      *
2870      * <p>When transitioning into DST, i.e. when the clocks spring forward (usually by one hour)
2871      * there is a wall clock period that is invalid, it literally doesn't exist. e.g. If clocks
2872      * go forward one hour at 02:00 on 9th March 2014 (standard time) then the wall time of
2873      * 02:00-02:59:59.999 is not a valid. The wall clock jumps straight from 01:59:59.999 to
2874      * 03:00. The following table shows the relationship between the time in millis, the standard
2875      * time and the wall time at the point of transitioning into DST. As can be seen there is no
2876      * 02:00 in the wall time.
2877      *
2878      * <pre>
2879      * Time In Millis - ......  x+1h .....  x+2h .....  x+3h
2880      * Standard Time  - ...... 01:00 ..... 02:00 ..... 03:00 .....
2881      * Wall Time      - ...... 01:00 ..... 03:00 ..... 04:00 .....
2882      *                                       ^
2883      *                                 02:00 missing
2884      * </pre>
2885      *
2886      * <p>The calendar fields represent wall time. If the user sets the fields on the calendar so
2887      * that it is in that invalid period then this code attempts to do something sensible. It
2888      * treats 02:MM:SS.SSS as if it is {@code 01:MM:SS.SSS + 1 hour}. That makes sense from both
2889      * the input calendar fields perspective and from the time in millis perspective. Of course the
2890      * result of that is that when the time is formatted in that time zone that the time is
2891      * actually 03:MM:SS.SSS.
2892      *
2893      * <pre>
2894      * Wall Time      - ...... 01:00 ..... <b>02:00 .....</b> 03:00 ..... 04:00 .....
2895      * Time In Millis - ......  x+1h ..... <b> x+2h .....</b>  x+2h .....  x+3h .....
2896      * </pre>
2897      *
2898      * <p>The way that works is as follows. First the standard time is calculated and the DST
2899      * offset is determined. Then if the time is in DST (the DST offset is not 0) but it was not in
2900      * DST an hour earlier (or however long the DST offset is) then it must be in that invalid
2901      * period, in which case set the DST offset to 0. That is then subtracted from the time in
2902      * millis to produce the correct result. The following diagram illustrates the process.
2903      *
2904      * <pre>
2905      * Standard Time  - ...... 01:00 ..... 02:00 ..... 03:00 ..... 04:00 .....
2906      * Time In Millis - ......  x+1h .....  x+2h .....  x+3h .....  x+4h .....
2907      * DST Offset     - ......    0h .....    1h .....    1h .....    1h .....
2908      * Adjusted DST   - ......    0h .....    <b>0h</b> .....    1h .....    1h .....
2909      * Adjusted Time  - ......  x+1h .....  x+2h .....  <b>x+2h</b> .....  <b>x+3h</b> .....
2910      * </pre>
2911      *
2912      * @return the adjusted DST offset.
2913      */
adjustDstOffsetForInvalidWallClock( long standardTimeInZone, TimeZone zone, int dstOffset)2914     private int adjustDstOffsetForInvalidWallClock(
2915             long standardTimeInZone, TimeZone zone, int dstOffset) {
2916 
2917         if (dstOffset != 0) {
2918             // If applying the DST offset produces a time that is outside DST then it must be
2919             // an invalid wall clock so clear the DST offset to avoid that happening.
2920             if (!zone.inDaylightTime(new Date(standardTimeInZone - dstOffset))) {
2921                 dstOffset = 0;
2922             }
2923         }
2924         return dstOffset;
2925     }
2926 
2927     /**
2928      * Computes the fixed date under either the Gregorian or the
2929      * Julian calendar, using the given year and the specified calendar fields.
2930      *
2931      * @param cal the CalendarSystem to be used for the date calculation
2932      * @param year the normalized year number, with 0 indicating the
2933      * year 1 BCE, -1 indicating 2 BCE, etc.
2934      * @param fieldMask the calendar fields to be used for the date calculation
2935      * @return the fixed date
2936      * @see Calendar#selectFields
2937      */
getFixedDate(BaseCalendar cal, int year, int fieldMask)2938     private long getFixedDate(BaseCalendar cal, int year, int fieldMask) {
2939         int month = JANUARY;
2940         if (isFieldSet(fieldMask, MONTH)) {
2941             // No need to check if MONTH has been set (no isSet(MONTH)
2942             // call) since its unset value happens to be JANUARY (0).
2943             month = internalGet(MONTH);
2944 
2945             // If the month is out of range, adjust it into range
2946             if (month > DECEMBER) {
2947                 year += month / 12;
2948                 month %= 12;
2949             } else if (month < JANUARY) {
2950                 int[] rem = new int[1];
2951                 year += CalendarUtils.floorDivide(month, 12, rem);
2952                 month = rem[0];
2953             }
2954         }
2955 
2956         // Get the fixed date since Jan 1, 1 (Gregorian). We are on
2957         // the first day of either `month' or January in 'year'.
2958         long fixedDate = cal.getFixedDate(year, month + 1, 1,
2959                                           cal == gcal ? gdate : null);
2960         if (isFieldSet(fieldMask, MONTH)) {
2961             // Month-based calculations
2962             if (isFieldSet(fieldMask, DAY_OF_MONTH)) {
2963                 // We are on the first day of the month. Just add the
2964                 // offset if DAY_OF_MONTH is set. If the isSet call
2965                 // returns false, that means DAY_OF_MONTH has been
2966                 // selected just because of the selected
2967                 // combination. We don't need to add any since the
2968                 // default value is the 1st.
2969                 if (isSet(DAY_OF_MONTH)) {
2970                     // To avoid underflow with DAY_OF_MONTH-1, add
2971                     // DAY_OF_MONTH, then subtract 1.
2972                     fixedDate += internalGet(DAY_OF_MONTH);
2973                     fixedDate--;
2974                 }
2975             } else {
2976                 if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {
2977                     long firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(fixedDate + 6,
2978                                                                          getFirstDayOfWeek());
2979                     // If we have enough days in the first week, then
2980                     // move to the previous week.
2981                     if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
2982                         firstDayOfWeek -= 7;
2983                     }
2984                     if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
2985                         firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
2986                                                                         internalGet(DAY_OF_WEEK));
2987                     }
2988                     // In lenient mode, we treat days of the previous
2989                     // months as a part of the specified
2990                     // WEEK_OF_MONTH. See 4633646.
2991                     fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);
2992                 } else {
2993                     int dayOfWeek;
2994                     if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
2995                         dayOfWeek = internalGet(DAY_OF_WEEK);
2996                     } else {
2997                         dayOfWeek = getFirstDayOfWeek();
2998                     }
2999                     // We are basing this on the day-of-week-in-month.  The only
3000                     // trickiness occurs if the day-of-week-in-month is
3001                     // negative.
3002                     int dowim;
3003                     if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {
3004                         dowim = internalGet(DAY_OF_WEEK_IN_MONTH);
3005                     } else {
3006                         dowim = 1;
3007                     }
3008                     if (dowim >= 0) {
3009                         fixedDate = cal.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,
3010                                                                    dayOfWeek);
3011                     } else {
3012                         // Go to the first day of the next week of
3013                         // the specified week boundary.
3014                         int lastDate = monthLength(month, year) + (7 * (dowim + 1));
3015                         // Then, get the day of week date on or before the last date.
3016                         fixedDate = cal.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,
3017                                                                    dayOfWeek);
3018                     }
3019                 }
3020             }
3021         } else {
3022             if (year == gregorianCutoverYear && cal == gcal
3023                 && fixedDate < gregorianCutoverDate
3024                 && gregorianCutoverYear != gregorianCutoverYearJulian) {
3025                 // January 1 of the year doesn't exist.  Use
3026                 // gregorianCutoverDate as the first day of the
3027                 // year.
3028                 fixedDate = gregorianCutoverDate;
3029             }
3030             // We are on the first day of the year.
3031             if (isFieldSet(fieldMask, DAY_OF_YEAR)) {
3032                 // Add the offset, then subtract 1. (Make sure to avoid underflow.)
3033                 fixedDate += internalGet(DAY_OF_YEAR);
3034                 fixedDate--;
3035             } else {
3036                 long firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(fixedDate + 6,
3037                                                                      getFirstDayOfWeek());
3038                 // If we have enough days in the first week, then move
3039                 // to the previous week.
3040                 if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
3041                     firstDayOfWeek -= 7;
3042                 }
3043                 if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
3044                     int dayOfWeek = internalGet(DAY_OF_WEEK);
3045                     if (dayOfWeek != getFirstDayOfWeek()) {
3046                         firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
3047                                                                         dayOfWeek);
3048                     }
3049                 }
3050                 fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);
3051             }
3052         }
3053 
3054         return fixedDate;
3055     }
3056 
3057     /**
3058      * Returns this object if it's normalized (all fields and time are
3059      * in sync). Otherwise, a cloned object is returned after calling
3060      * complete() in lenient mode.
3061      */
getNormalizedCalendar()3062     private final GregorianCalendar getNormalizedCalendar() {
3063         GregorianCalendar gc;
3064         if (isFullyNormalized()) {
3065             gc = this;
3066         } else {
3067             // Create a clone and normalize the calendar fields
3068             gc = (GregorianCalendar) this.clone();
3069             gc.setLenient(true);
3070             gc.complete();
3071         }
3072         return gc;
3073     }
3074 
3075     /**
3076      * Returns the Julian calendar system instance (singleton). 'jcal'
3077      * and 'jeras' are set upon the return.
3078      */
getJulianCalendarSystem()3079     synchronized private static final BaseCalendar getJulianCalendarSystem() {
3080         if (jcal == null) {
3081             jcal = (JulianCalendar) CalendarSystem.forName("julian");
3082             jeras = jcal.getEras();
3083         }
3084         return jcal;
3085     }
3086 
3087     /**
3088      * Returns the calendar system for dates before the cutover date
3089      * in the cutover year. If the cutover date is January 1, the
3090      * method returns Gregorian. Otherwise, Julian.
3091      */
getCutoverCalendarSystem()3092     private BaseCalendar getCutoverCalendarSystem() {
3093         if (gregorianCutoverYearJulian < gregorianCutoverYear) {
3094             return gcal;
3095         }
3096         return getJulianCalendarSystem();
3097     }
3098 
3099     /**
3100      * Determines if the specified year (normalized) is the Gregorian
3101      * cutover year. This object must have been normalized.
3102      */
isCutoverYear(int normalizedYear)3103     private final boolean isCutoverYear(int normalizedYear) {
3104         int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
3105         return normalizedYear == cutoverYear;
3106     }
3107 
3108     /**
3109      * Returns the fixed date of the first day of the year (usually
3110      * January 1) before the specified date.
3111      *
3112      * @param date the date for which the first day of the year is
3113      * calculated. The date has to be in the cut-over year (Gregorian
3114      * or Julian).
3115      * @param fixedDate the fixed date representation of the date
3116      */
getFixedDateJan1(BaseCalendar.Date date, long fixedDate)3117     private final long getFixedDateJan1(BaseCalendar.Date date, long fixedDate) {
3118         assert date.getNormalizedYear() == gregorianCutoverYear ||
3119             date.getNormalizedYear() == gregorianCutoverYearJulian;
3120         if (gregorianCutoverYear != gregorianCutoverYearJulian) {
3121             if (fixedDate >= gregorianCutoverDate) {
3122                 // Dates before the cutover date don't exist
3123                 // in the same (Gregorian) year. So, no
3124                 // January 1 exists in the year. Use the
3125                 // cutover date as the first day of the year.
3126                 return gregorianCutoverDate;
3127             }
3128         }
3129         // January 1 of the normalized year should exist.
3130         BaseCalendar jcal = getJulianCalendarSystem();
3131         return jcal.getFixedDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1, null);
3132     }
3133 
3134     /**
3135      * Returns the fixed date of the first date of the month (usually
3136      * the 1st of the month) before the specified date.
3137      *
3138      * @param date the date for which the first day of the month is
3139      * calculated. The date has to be in the cut-over year (Gregorian
3140      * or Julian).
3141      * @param fixedDate the fixed date representation of the date
3142      */
getFixedDateMonth1(BaseCalendar.Date date, long fixedDate)3143     private final long getFixedDateMonth1(BaseCalendar.Date date, long fixedDate) {
3144         assert date.getNormalizedYear() == gregorianCutoverYear ||
3145             date.getNormalizedYear() == gregorianCutoverYearJulian;
3146         BaseCalendar.Date gCutover = getGregorianCutoverDate();
3147         if (gCutover.getMonth() == BaseCalendar.JANUARY
3148             && gCutover.getDayOfMonth() == 1) {
3149             // The cutover happened on January 1.
3150             return fixedDate - date.getDayOfMonth() + 1;
3151         }
3152 
3153         long fixedDateMonth1;
3154         // The cutover happened sometime during the year.
3155         if (date.getMonth() == gCutover.getMonth()) {
3156             // The cutover happened in the month.
3157             BaseCalendar.Date jLastDate = getLastJulianDate();
3158             if (gregorianCutoverYear == gregorianCutoverYearJulian
3159                 && gCutover.getMonth() == jLastDate.getMonth()) {
3160                 // The "gap" fits in the same month.
3161                 fixedDateMonth1 = jcal.getFixedDate(date.getNormalizedYear(),
3162                                                     date.getMonth(),
3163                                                     1,
3164                                                     null);
3165             } else {
3166                 // Use the cutover date as the first day of the month.
3167                 fixedDateMonth1 = gregorianCutoverDate;
3168             }
3169         } else {
3170             // The cutover happened before the month.
3171             fixedDateMonth1 = fixedDate - date.getDayOfMonth() + 1;
3172         }
3173 
3174         return fixedDateMonth1;
3175     }
3176 
3177     /**
3178      * Returns a CalendarDate produced from the specified fixed date.
3179      *
3180      * @param fd the fixed date
3181      */
getCalendarDate(long fd)3182     private final BaseCalendar.Date getCalendarDate(long fd) {
3183         BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
3184         BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
3185         cal.getCalendarDateFromFixedDate(d, fd);
3186         return d;
3187     }
3188 
3189     /**
3190      * Returns the Gregorian cutover date as a BaseCalendar.Date. The
3191      * date is a Gregorian date.
3192      */
getGregorianCutoverDate()3193     private final BaseCalendar.Date getGregorianCutoverDate() {
3194         return getCalendarDate(gregorianCutoverDate);
3195     }
3196 
3197     /**
3198      * Returns the day before the Gregorian cutover date as a
3199      * BaseCalendar.Date. The date is a Julian date.
3200      */
getLastJulianDate()3201     private final BaseCalendar.Date getLastJulianDate() {
3202         return getCalendarDate(gregorianCutoverDate - 1);
3203     }
3204 
3205     /**
3206      * Returns the length of the specified month in the specified
3207      * year. The year number must be normalized.
3208      *
3209      * @see #isLeapYear(int)
3210      */
monthLength(int month, int year)3211     private final int monthLength(int month, int year) {
3212         return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];
3213     }
3214 
3215     /**
3216      * Returns the length of the specified month in the year provided
3217      * by internalGet(YEAR).
3218      *
3219      * @see #isLeapYear(int)
3220      */
monthLength(int month)3221     private final int monthLength(int month) {
3222         int year = internalGet(YEAR);
3223         if (internalGetEra() == BCE) {
3224             year = 1 - year;
3225         }
3226         return monthLength(month, year);
3227     }
3228 
actualMonthLength()3229     private final int actualMonthLength() {
3230         int year = cdate.getNormalizedYear();
3231         if (year != gregorianCutoverYear && year != gregorianCutoverYearJulian) {
3232             return calsys.getMonthLength(cdate);
3233         }
3234         BaseCalendar.Date date = (BaseCalendar.Date) cdate.clone();
3235         long fd = calsys.getFixedDate(date);
3236         long month1 = getFixedDateMonth1(date, fd);
3237         long next1 = month1 + calsys.getMonthLength(date);
3238         if (next1 < gregorianCutoverDate) {
3239             return (int)(next1 - month1);
3240         }
3241         if (cdate != gdate) {
3242             date = (BaseCalendar.Date) gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
3243         }
3244         gcal.getCalendarDateFromFixedDate(date, next1);
3245         next1 = getFixedDateMonth1(date, next1);
3246         return (int)(next1 - month1);
3247     }
3248 
3249     /**
3250      * Returns the length (in days) of the specified year. The year
3251      * must be normalized.
3252      */
yearLength(int year)3253     private final int yearLength(int year) {
3254         return isLeapYear(year) ? 366 : 365;
3255     }
3256 
3257     /**
3258      * Returns the length (in days) of the year provided by
3259      * internalGet(YEAR).
3260      */
yearLength()3261     private final int yearLength() {
3262         int year = internalGet(YEAR);
3263         if (internalGetEra() == BCE) {
3264             year = 1 - year;
3265         }
3266         return yearLength(year);
3267     }
3268 
3269     /**
3270      * After adjustments such as add(MONTH), add(YEAR), we don't want the
3271      * month to jump around.  E.g., we don't want Jan 31 + 1 month to go to Mar
3272      * 3, we want it to go to Feb 28.  Adjustments which might run into this
3273      * problem call this method to retain the proper month.
3274      */
pinDayOfMonth()3275     private final void pinDayOfMonth() {
3276         int year = internalGet(YEAR);
3277         int monthLen;
3278         if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {
3279             monthLen = monthLength(internalGet(MONTH));
3280         } else {
3281             GregorianCalendar gc = getNormalizedCalendar();
3282             monthLen = gc.getActualMaximum(DAY_OF_MONTH);
3283         }
3284         int dom = internalGet(DAY_OF_MONTH);
3285         if (dom > monthLen) {
3286             set(DAY_OF_MONTH, monthLen);
3287         }
3288     }
3289 
3290     /**
3291      * Returns the fixed date value of this object. The time value and
3292      * calendar fields must be in synch.
3293      */
getCurrentFixedDate()3294     private final long getCurrentFixedDate() {
3295         return (calsys == gcal) ? cachedFixedDate : calsys.getFixedDate(cdate);
3296     }
3297 
3298     /**
3299      * Returns the new value after 'roll'ing the specified value and amount.
3300      */
getRolledValue(int value, int amount, int min, int max)3301     private static final int getRolledValue(int value, int amount, int min, int max) {
3302         assert value >= min && value <= max;
3303         int range = max - min + 1;
3304         amount %= range;
3305         int n = value + amount;
3306         if (n > max) {
3307             n -= range;
3308         } else if (n < min) {
3309             n += range;
3310         }
3311         assert n >= min && n <= max;
3312         return n;
3313     }
3314 
3315     /**
3316      * Returns the ERA.  We need a special method for this because the
3317      * default ERA is CE, but a zero (unset) ERA is BCE.
3318      */
internalGetEra()3319     private final int internalGetEra() {
3320         return isSet(ERA) ? internalGet(ERA) : CE;
3321     }
3322 
3323     /**
3324      * Updates internal state.
3325      */
readObject(ObjectInputStream stream)3326     private void readObject(ObjectInputStream stream)
3327             throws IOException, ClassNotFoundException {
3328         stream.defaultReadObject();
3329         if (gdate == null) {
3330             gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
3331             cachedFixedDate = Long.MIN_VALUE;
3332         }
3333         setGregorianChange(gregorianCutover);
3334     }
3335 }
3336