Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/GregorianCalendar.java
38829 views
/*1* Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved27* (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved28*29* The original version of this source code and documentation is copyrighted30* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These31* materials are provided under terms of a License Agreement between Taligent32* and Sun. This technology is protected by multiple US and International33* patents. This notice and attribution to Taligent may not be removed.34* Taligent is a registered trademark of Taligent, Inc.35*36*/3738package java.util;3940import java.io.IOException;41import java.io.ObjectInputStream;42import java.time.Instant;43import java.time.ZonedDateTime;44import java.time.temporal.ChronoField;45import sun.util.calendar.BaseCalendar;46import sun.util.calendar.CalendarDate;47import sun.util.calendar.CalendarSystem;48import sun.util.calendar.CalendarUtils;49import sun.util.calendar.Era;50import sun.util.calendar.Gregorian;51import sun.util.calendar.JulianCalendar;52import sun.util.calendar.ZoneInfo;5354/**55* <code>GregorianCalendar</code> is a concrete subclass of56* <code>Calendar</code> and provides the standard calendar system57* used by most of the world.58*59* <p> <code>GregorianCalendar</code> is a hybrid calendar that60* supports both the Julian and Gregorian calendar systems with the61* support of a single discontinuity, which corresponds by default to62* the Gregorian date when the Gregorian calendar was instituted63* (October 15, 1582 in some countries, later in others). The cutover64* date may be changed by the caller by calling {@link65* #setGregorianChange(Date) setGregorianChange()}.66*67* <p>68* Historically, in those countries which adopted the Gregorian calendar first,69* October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models70* this correctly. Before the Gregorian cutover, <code>GregorianCalendar</code>71* implements the Julian calendar. The only difference between the Gregorian72* and the Julian calendar is the leap year rule. The Julian calendar specifies73* leap years every four years, whereas the Gregorian calendar omits century74* years which are not divisible by 400.75*76* <p>77* <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and78* Julian calendars. That is, dates are computed by extrapolating the current79* rules indefinitely far backward and forward in time. As a result,80* <code>GregorianCalendar</code> may be used for all years to generate81* meaningful and consistent results. However, dates obtained using82* <code>GregorianCalendar</code> are historically accurate only from March 1, 483* AD onward, when modern Julian calendar rules were adopted. Before this date,84* leap year rules were applied irregularly, and before 45 BC the Julian85* calendar did not even exist.86*87* <p>88* Prior to the institution of the Gregorian calendar, New Year's Day was89* March 25. To avoid confusion, this calendar always uses January 1. A manual90* adjustment may be made if desired for dates that are prior to the Gregorian91* changeover and which fall between January 1 and March 24.92*93* <h3><a name="week_and_year">Week Of Year and Week Year</a></h3>94*95* <p>Values calculated for the {@link Calendar#WEEK_OF_YEAR96* WEEK_OF_YEAR} field range from 1 to 53. The first week of a97* calendar year is the earliest seven day period starting on {@link98* Calendar#getFirstDayOfWeek() getFirstDayOfWeek()} that contains at99* least {@link Calendar#getMinimalDaysInFirstWeek()100* getMinimalDaysInFirstWeek()} days from that year. It thus depends101* on the values of {@code getMinimalDaysInFirstWeek()}, {@code102* getFirstDayOfWeek()}, and the day of the week of January 1. Weeks103* between week 1 of one year and week 1 of the following year104* (exclusive) are numbered sequentially from 2 to 52 or 53 (except105* for year(s) involved in the Julian-Gregorian transition).106*107* <p>The {@code getFirstDayOfWeek()} and {@code108* getMinimalDaysInFirstWeek()} values are initialized using109* locale-dependent resources when constructing a {@code110* GregorianCalendar}. <a name="iso8601_compatible_setting">The week111* determination is compatible</a> with the ISO 8601 standard when {@code112* getFirstDayOfWeek()} is {@code MONDAY} and {@code113* getMinimalDaysInFirstWeek()} is 4, which values are used in locales114* where the standard is preferred. These values can explicitly be set by115* calling {@link Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()} and116* {@link Calendar#setMinimalDaysInFirstWeek(int)117* setMinimalDaysInFirstWeek()}.118*119* <p>A <a name="week_year"><em>week year</em></a> is in sync with a120* {@code WEEK_OF_YEAR} cycle. All weeks between the first and last121* weeks (inclusive) have the same <em>week year</em> value.122* Therefore, the first and last days of a week year may have123* different calendar year values.124*125* <p>For example, January 1, 1998 is a Thursday. If {@code126* getFirstDayOfWeek()} is {@code MONDAY} and {@code127* getMinimalDaysInFirstWeek()} is 4 (ISO 8601 standard compatible128* setting), then week 1 of 1998 starts on December 29, 1997, and ends129* on January 4, 1998. The week year is 1998 for the last three days130* of calendar year 1997. If, however, {@code getFirstDayOfWeek()} is131* {@code SUNDAY}, then week 1 of 1998 starts on January 4, 1998, and132* ends on January 10, 1998; the first three days of 1998 then are133* part of week 53 of 1997 and their week year is 1997.134*135* <h4>Week Of Month</h4>136*137* <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0138* to 6. Week 1 of a month (the days with <code>WEEK_OF_MONTH =139* 1</code>) is the earliest set of at least140* <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,141* ending on the day before <code>getFirstDayOfWeek()</code>. Unlike142* week 1 of a year, week 1 of a month may be shorter than 7 days, need143* not start on <code>getFirstDayOfWeek()</code>, and will not include days of144* the previous month. Days of a month before week 1 have a145* <code>WEEK_OF_MONTH</code> of 0.146*147* <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>148* and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of149* January 1998 is Sunday, January 4 through Saturday, January 10. These days150* have a <code>WEEK_OF_MONTH</code> of 1. Thursday, January 1 through151* Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0. If152* <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1153* through January 3 have a <code>WEEK_OF_MONTH</code> of 1.154*155* <h4>Default Fields Values</h4>156*157* <p>The <code>clear</code> method sets calendar field(s)158* undefined. <code>GregorianCalendar</code> uses the following159* default value for each calendar field if its value is undefined.160*161* <table cellpadding="0" cellspacing="3" border="0"162* summary="GregorianCalendar default field values"163* style="text-align: left; width: 66%;">164* <tbody>165* <tr>166* <th style="vertical-align: top; background-color: rgb(204, 204, 255);167* text-align: center;">Field<br>168* </th>169* <th style="vertical-align: top; background-color: rgb(204, 204, 255);170* text-align: center;">Default Value<br>171* </th>172* </tr>173* <tr>174* <td style="vertical-align: middle;">175* <code>ERA<br></code>176* </td>177* <td style="vertical-align: middle;">178* <code>AD<br></code>179* </td>180* </tr>181* <tr>182* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">183* <code>YEAR<br></code>184* </td>185* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">186* <code>1970<br></code>187* </td>188* </tr>189* <tr>190* <td style="vertical-align: middle;">191* <code>MONTH<br></code>192* </td>193* <td style="vertical-align: middle;">194* <code>JANUARY<br></code>195* </td>196* </tr>197* <tr>198* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">199* <code>DAY_OF_MONTH<br></code>200* </td>201* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">202* <code>1<br></code>203* </td>204* </tr>205* <tr>206* <td style="vertical-align: middle;">207* <code>DAY_OF_WEEK<br></code>208* </td>209* <td style="vertical-align: middle;">210* <code>the first day of week<br></code>211* </td>212* </tr>213* <tr>214* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">215* <code>WEEK_OF_MONTH<br></code>216* </td>217* <td style="vertical-align: top; background-color: rgb(238, 238, 255);">218* <code>0<br></code>219* </td>220* </tr>221* <tr>222* <td style="vertical-align: top;">223* <code>DAY_OF_WEEK_IN_MONTH<br></code>224* </td>225* <td style="vertical-align: top;">226* <code>1<br></code>227* </td>228* </tr>229* <tr>230* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">231* <code>AM_PM<br></code>232* </td>233* <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">234* <code>AM<br></code>235* </td>236* </tr>237* <tr>238* <td style="vertical-align: middle;">239* <code>HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND<br></code>240* </td>241* <td style="vertical-align: middle;">242* <code>0<br></code>243* </td>244* </tr>245* </tbody>246* </table>247* <br>Default values are not applicable for the fields not listed above.248*249* <p>250* <strong>Example:</strong>251* <blockquote>252* <pre>253* // get the supported ids for GMT-08:00 (Pacific Standard Time)254* String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);255* // if no ids were returned, something is wrong. get out.256* if (ids.length == 0)257* System.exit(0);258*259* // begin output260* System.out.println("Current Time");261*262* // create a Pacific Standard Time time zone263* SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);264*265* // set up rules for Daylight Saving Time266* pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);267* pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);268*269* // create a GregorianCalendar with the Pacific Daylight time zone270* // and the current date and time271* Calendar calendar = new GregorianCalendar(pdt);272* Date trialTime = new Date();273* calendar.setTime(trialTime);274*275* // print out a bunch of interesting things276* System.out.println("ERA: " + calendar.get(Calendar.ERA));277* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));278* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));279* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));280* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));281* System.out.println("DATE: " + calendar.get(Calendar.DATE));282* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));283* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));284* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));285* System.out.println("DAY_OF_WEEK_IN_MONTH: "286* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));287* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));288* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));289* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));290* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));291* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));292* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));293* System.out.println("ZONE_OFFSET: "294* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));295* System.out.println("DST_OFFSET: "296* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));297298* System.out.println("Current Time, with hour reset to 3");299* calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override300* calendar.set(Calendar.HOUR, 3);301* System.out.println("ERA: " + calendar.get(Calendar.ERA));302* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));303* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));304* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));305* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));306* System.out.println("DATE: " + calendar.get(Calendar.DATE));307* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));308* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));309* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));310* System.out.println("DAY_OF_WEEK_IN_MONTH: "311* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));312* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));313* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));314* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));315* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));316* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));317* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));318* System.out.println("ZONE_OFFSET: "319* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours320* System.out.println("DST_OFFSET: "321* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours322* </pre>323* </blockquote>324*325* @see TimeZone326* @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu327* @since JDK1.1328*/329public class GregorianCalendar extends Calendar {330/*331* Implementation Notes332*333* The epoch is the number of days or milliseconds from some defined334* starting point. The epoch for java.util.Date is used here; that is,335* milliseconds from January 1, 1970 (Gregorian), midnight UTC. Other336* epochs which are used are January 1, year 1 (Gregorian), which is day 1337* of the Gregorian calendar, and December 30, year 0 (Gregorian), which is338* day 1 of the Julian calendar.339*340* We implement the proleptic Julian and Gregorian calendars. This means we341* implement the modern definition of the calendar even though the342* historical usage differs. For example, if the Gregorian change is set343* to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which344* labels dates preceding the invention of the Gregorian calendar in 1582 as345* if the calendar existed then.346*347* Likewise, with the Julian calendar, we assume a consistent348* 4-year leap year rule, even though the historical pattern of349* leap years is irregular, being every 3 years from 45 BCE350* through 9 BCE, then every 4 years from 8 CE onwards, with no351* leap years in-between. Thus date computations and functions352* such as isLeapYear() are not intended to be historically353* accurate.354*/355356//////////////////357// Class Variables358//////////////////359360/**361* Value of the <code>ERA</code> field indicating362* the period before the common era (before Christ), also known as BCE.363* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is364* ..., 2 BC, 1 BC, 1 AD, 2 AD,...365*366* @see #ERA367*/368public static final int BC = 0;369370/**371* Value of the {@link #ERA} field indicating372* the period before the common era, the same value as {@link #BC}.373*374* @see #CE375*/376static final int BCE = 0;377378/**379* Value of the <code>ERA</code> field indicating380* the common era (Anno Domini), also known as CE.381* The sequence of years at the transition from <code>BC</code> to <code>AD</code> is382* ..., 2 BC, 1 BC, 1 AD, 2 AD,...383*384* @see #ERA385*/386public static final int AD = 1;387388/**389* Value of the {@link #ERA} field indicating390* the common era, the same value as {@link #AD}.391*392* @see #BCE393*/394static final int CE = 1;395396private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)397private static final int EPOCH_YEAR = 1970;398399static final int MONTH_LENGTH[]400= {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based401static final int LEAP_MONTH_LENGTH[]402= {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based403404// Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit405// into ints, they must be longs in order to prevent arithmetic overflow406// when performing (bug 4173516).407private static final int ONE_SECOND = 1000;408private static final int ONE_MINUTE = 60*ONE_SECOND;409private static final int ONE_HOUR = 60*ONE_MINUTE;410private static final long ONE_DAY = 24*ONE_HOUR;411private static final long ONE_WEEK = 7*ONE_DAY;412413/*414* <pre>415* Greatest Least416* Field name Minimum Minimum Maximum Maximum417* ---------- ------- ------- ------- -------418* ERA 0 0 1 1419* YEAR 1 1 292269054 292278994420* MONTH 0 0 11 11421* WEEK_OF_YEAR 1 1 52* 53422* WEEK_OF_MONTH 0 0 4* 6423* DAY_OF_MONTH 1 1 28* 31424* DAY_OF_YEAR 1 1 365* 366425* DAY_OF_WEEK 1 1 7 7426* DAY_OF_WEEK_IN_MONTH -1 -1 4* 6427* AM_PM 0 0 1 1428* HOUR 0 0 11 11429* HOUR_OF_DAY 0 0 23 23430* MINUTE 0 0 59 59431* SECOND 0 0 59 59432* MILLISECOND 0 0 999 999433* ZONE_OFFSET -13:00 -13:00 14:00 14:00434* DST_OFFSET 0:00 0:00 0:20 2:00435* </pre>436* *: depends on the Gregorian change date437*/438static final int MIN_VALUES[] = {439BCE, // ERA4401, // YEAR441JANUARY, // MONTH4421, // WEEK_OF_YEAR4430, // WEEK_OF_MONTH4441, // DAY_OF_MONTH4451, // DAY_OF_YEAR446SUNDAY, // DAY_OF_WEEK4471, // DAY_OF_WEEK_IN_MONTH448AM, // AM_PM4490, // HOUR4500, // HOUR_OF_DAY4510, // MINUTE4520, // SECOND4530, // MILLISECOND454-13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility)4550 // DST_OFFSET456};457static final int LEAST_MAX_VALUES[] = {458CE, // ERA459292269054, // YEAR460DECEMBER, // MONTH46152, // WEEK_OF_YEAR4624, // WEEK_OF_MONTH46328, // DAY_OF_MONTH464365, // DAY_OF_YEAR465SATURDAY, // DAY_OF_WEEK4664, // DAY_OF_WEEK_IN467PM, // AM_PM46811, // HOUR46923, // HOUR_OF_DAY47059, // MINUTE47159, // SECOND472999, // MILLISECOND47314*ONE_HOUR, // ZONE_OFFSET47420*ONE_MINUTE // DST_OFFSET (historical least maximum)475};476static final int MAX_VALUES[] = {477CE, // ERA478292278994, // YEAR479DECEMBER, // MONTH48053, // WEEK_OF_YEAR4816, // WEEK_OF_MONTH48231, // DAY_OF_MONTH483366, // DAY_OF_YEAR484SATURDAY, // DAY_OF_WEEK4856, // DAY_OF_WEEK_IN486PM, // AM_PM48711, // HOUR48823, // HOUR_OF_DAY48959, // MINUTE49059, // SECOND491999, // MILLISECOND49214*ONE_HOUR, // ZONE_OFFSET4932*ONE_HOUR // DST_OFFSET (double summer time)494};495496// Proclaim serialization compatibility with JDK 1.1497@SuppressWarnings("FieldNameHidesFieldInSuperclass")498static final long serialVersionUID = -8125100834729963327L;499500// Reference to the sun.util.calendar.Gregorian instance (singleton).501private static final Gregorian gcal =502CalendarSystem.getGregorianCalendar();503504// Reference to the JulianCalendar instance (singleton), set as needed. See505// getJulianCalendarSystem().506private static JulianCalendar jcal;507508// JulianCalendar eras. See getJulianCalendarSystem().509private static Era[] jeras;510511// The default value of gregorianCutover.512static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;513514/////////////////////515// Instance Variables516/////////////////////517518/**519* The point at which the Gregorian calendar rules are used, measured in520* milliseconds from the standard epoch. Default is October 15, 1582521* (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,522* 1582 (Julian) is followed by October 15, 1582 (Gregorian). This523* corresponds to Julian day number 2299161.524* @serial525*/526private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;527528/**529* The fixed date of the gregorianCutover.530*/531private transient long gregorianCutoverDate =532(((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736533534/**535* The normalized year of the gregorianCutover in Gregorian, with536* 0 representing 1 BCE, -1 representing 2 BCE, etc.537*/538private transient int gregorianCutoverYear = 1582;539540/**541* The normalized year of the gregorianCutover in Julian, with 0542* representing 1 BCE, -1 representing 2 BCE, etc.543*/544private transient int gregorianCutoverYearJulian = 1582;545546/**547* gdate always has a sun.util.calendar.Gregorian.Date instance to548* avoid overhead of creating it. The assumption is that most549* applications will need only Gregorian calendar calculations.550*/551private transient BaseCalendar.Date gdate;552553/**554* Reference to either gdate or a JulianCalendar.Date555* instance. After calling complete(), this value is guaranteed to556* be set.557*/558private transient BaseCalendar.Date cdate;559560/**561* The CalendarSystem used to calculate the date in cdate. After562* calling complete(), this value is guaranteed to be set and563* consistent with the cdate value.564*/565private transient BaseCalendar calsys;566567/**568* Temporary int[2] to get time zone offsets. zoneOffsets[0] gets569* the GMT offset value and zoneOffsets[1] gets the DST saving570* value.571*/572private transient int[] zoneOffsets;573574/**575* Temporary storage for saving original fields[] values in576* non-lenient mode.577*/578private transient int[] originalFields;579580///////////////581// Constructors582///////////////583584/**585* Constructs a default <code>GregorianCalendar</code> using the current time586* in the default time zone with the default587* {@link Locale.Category#FORMAT FORMAT} locale.588*/589public GregorianCalendar() {590this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));591setZoneShared(true);592}593594/**595* Constructs a <code>GregorianCalendar</code> based on the current time596* in the given time zone with the default597* {@link Locale.Category#FORMAT FORMAT} locale.598*599* @param zone the given time zone.600*/601public GregorianCalendar(TimeZone zone) {602this(zone, Locale.getDefault(Locale.Category.FORMAT));603}604605/**606* Constructs a <code>GregorianCalendar</code> based on the current time607* in the default time zone with the given locale.608*609* @param aLocale the given locale.610*/611public GregorianCalendar(Locale aLocale) {612this(TimeZone.getDefaultRef(), aLocale);613setZoneShared(true);614}615616/**617* Constructs a <code>GregorianCalendar</code> based on the current time618* in the given time zone with the given locale.619*620* @param zone the given time zone.621* @param aLocale the given locale.622*/623public GregorianCalendar(TimeZone zone, Locale aLocale) {624super(zone, aLocale);625gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);626setTimeInMillis(System.currentTimeMillis());627}628629/**630* Constructs a <code>GregorianCalendar</code> with the given date set631* in the default time zone with the default locale.632*633* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.634* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.635* Month value is 0-based. e.g., 0 for January.636* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.637*/638public GregorianCalendar(int year, int month, int dayOfMonth) {639this(year, month, dayOfMonth, 0, 0, 0, 0);640}641642/**643* Constructs a <code>GregorianCalendar</code> with the given date644* and time set for the default time zone with the default locale.645*646* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.647* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.648* Month value is 0-based. e.g., 0 for January.649* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.650* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field651* in the calendar.652* @param minute the value used to set the <code>MINUTE</code> calendar field653* in the calendar.654*/655public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,656int minute) {657this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);658}659660/**661* Constructs a GregorianCalendar with the given date662* and time set for the default time zone with the default locale.663*664* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.665* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.666* Month value is 0-based. e.g., 0 for January.667* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.668* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field669* in the calendar.670* @param minute the value used to set the <code>MINUTE</code> calendar field671* in the calendar.672* @param second the value used to set the <code>SECOND</code> calendar field673* in the calendar.674*/675public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,676int minute, int second) {677this(year, month, dayOfMonth, hourOfDay, minute, second, 0);678}679680/**681* Constructs a <code>GregorianCalendar</code> with the given date682* and time set for the default time zone with the default locale.683*684* @param year the value used to set the <code>YEAR</code> calendar field in the calendar.685* @param month the value used to set the <code>MONTH</code> calendar field in the calendar.686* Month value is 0-based. e.g., 0 for January.687* @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.688* @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field689* in the calendar.690* @param minute the value used to set the <code>MINUTE</code> calendar field691* in the calendar.692* @param second the value used to set the <code>SECOND</code> calendar field693* in the calendar.694* @param millis the value used to set the <code>MILLISECOND</code> calendar field695*/696GregorianCalendar(int year, int month, int dayOfMonth,697int hourOfDay, int minute, int second, int millis) {698super();699gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());700this.set(YEAR, year);701this.set(MONTH, month);702this.set(DAY_OF_MONTH, dayOfMonth);703704// Set AM_PM and HOUR here to set their stamp values before705// setting HOUR_OF_DAY (6178071).706if (hourOfDay >= 12 && hourOfDay <= 23) {707// If hourOfDay is a valid PM hour, set the correct PM values708// so that it won't throw an exception in case it's set to709// non-lenient later.710this.internalSet(AM_PM, PM);711this.internalSet(HOUR, hourOfDay - 12);712} else {713// The default value for AM_PM is AM.714// We don't care any out of range value here for leniency.715this.internalSet(HOUR, hourOfDay);716}717// The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)718setFieldsComputed(HOUR_MASK|AM_PM_MASK);719720this.set(HOUR_OF_DAY, hourOfDay);721this.set(MINUTE, minute);722this.set(SECOND, second);723// should be changed to set() when this constructor is made724// public.725this.internalSet(MILLISECOND, millis);726}727728/**729* Constructs an empty GregorianCalendar.730*731* @param zone the given time zone732* @param aLocale the given locale733* @param flag the flag requesting an empty instance734*/735GregorianCalendar(TimeZone zone, Locale locale, boolean flag) {736super(zone, locale);737gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());738}739740/////////////////741// Public methods742/////////////////743744/**745* Sets the <code>GregorianCalendar</code> change date. This is the point when the switch746* from Julian dates to Gregorian dates occurred. Default is October 15,747* 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.748* <p>749* To obtain a pure Julian calendar, set the change date to750* <code>Date(Long.MAX_VALUE)</code>. To obtain a pure Gregorian calendar,751* set the change date to <code>Date(Long.MIN_VALUE)</code>.752*753* @param date the given Gregorian cutover date.754*/755public void setGregorianChange(Date date) {756long cutoverTime = date.getTime();757if (cutoverTime == gregorianCutover) {758return;759}760// Before changing the cutover date, make sure to have the761// time of this calendar.762complete();763setGregorianChange(cutoverTime);764}765766private void setGregorianChange(long cutoverTime) {767gregorianCutover = cutoverTime;768gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)769+ EPOCH_OFFSET;770771// To provide the "pure" Julian calendar as advertised.772// Strictly speaking, the last millisecond should be a773// Gregorian date. However, the API doc specifies that setting774// the cutover date to Long.MAX_VALUE will make this calendar775// a pure Julian calendar. (See 4167995)776if (cutoverTime == Long.MAX_VALUE) {777gregorianCutoverDate++;778}779780BaseCalendar.Date d = getGregorianCutoverDate();781782// Set the cutover year (in the Gregorian year numbering)783gregorianCutoverYear = d.getYear();784785BaseCalendar julianCal = getJulianCalendarSystem();786d = (BaseCalendar.Date) julianCal.newCalendarDate(TimeZone.NO_TIMEZONE);787julianCal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);788gregorianCutoverYearJulian = d.getNormalizedYear();789790if (time < gregorianCutover) {791// The field values are no longer valid under the new792// cutover date.793setUnnormalized();794}795}796797/**798* Gets the Gregorian Calendar change date. This is the point when the799* switch from Julian dates to Gregorian dates occurred. Default is800* October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian801* calendar.802*803* @return the Gregorian cutover date for this <code>GregorianCalendar</code> object.804*/805public final Date getGregorianChange() {806return new Date(gregorianCutover);807}808809/**810* Determines if the given year is a leap year. Returns <code>true</code> if811* the given year is a leap year. To specify BC year numbers,812* <code>1 - year number</code> must be given. For example, year BC 4 is813* specified as -3.814*815* @param year the given year.816* @return <code>true</code> if the given year is a leap year; <code>false</code> otherwise.817*/818public boolean isLeapYear(int year) {819if ((year & 3) != 0) {820return false;821}822823if (year > gregorianCutoverYear) {824return (year%100 != 0) || (year%400 == 0); // Gregorian825}826if (year < gregorianCutoverYearJulian) {827return true; // Julian828}829boolean gregorian;830// If the given year is the Gregorian cutover year, we need to831// determine which calendar system to be applied to February in the year.832if (gregorianCutoverYear == gregorianCutoverYearJulian) {833BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian834gregorian = d.getMonth() < BaseCalendar.MARCH;835} else {836gregorian = year == gregorianCutoverYear;837}838return gregorian ? (year%100 != 0) || (year%400 == 0) : true;839}840841/**842* Returns {@code "gregory"} as the calendar type.843*844* @return {@code "gregory"}845* @since 1.8846*/847@Override848public String getCalendarType() {849return "gregory";850}851852/**853* Compares this <code>GregorianCalendar</code> to the specified854* <code>Object</code>. The result is <code>true</code> if and855* only if the argument is a <code>GregorianCalendar</code> object856* that represents the same time value (millisecond offset from857* the <a href="Calendar.html#Epoch">Epoch</a>) under the same858* <code>Calendar</code> parameters and Gregorian change date as859* this object.860*861* @param obj the object to compare with.862* @return <code>true</code> if this object is equal to <code>obj</code>;863* <code>false</code> otherwise.864* @see Calendar#compareTo(Calendar)865*/866@Override867public boolean equals(Object obj) {868return obj instanceof GregorianCalendar &&869super.equals(obj) &&870gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;871}872873/**874* Generates the hash code for this <code>GregorianCalendar</code> object.875*/876@Override877public int hashCode() {878return super.hashCode() ^ (int)gregorianCutoverDate;879}880881/**882* Adds the specified (signed) amount of time to the given calendar field,883* based on the calendar's rules.884*885* <p><em>Add rule 1</em>. The value of <code>field</code>886* after the call minus the value of <code>field</code> before the887* call is <code>amount</code>, modulo any overflow that has occurred in888* <code>field</code>. Overflow occurs when a field value exceeds its889* range and, as a result, the next larger field is incremented or890* decremented and the field value is adjusted back into its range.</p>891*892* <p><em>Add rule 2</em>. If a smaller field is expected to be893* invariant, but it is impossible for it to be equal to its894* prior value because of changes in its minimum or maximum after895* <code>field</code> is changed, then its value is adjusted to be as close896* as possible to its expected value. A smaller field represents a897* smaller unit of time. <code>HOUR</code> is a smaller field than898* <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields899* that are not expected to be invariant. The calendar system900* determines what fields are expected to be invariant.</p>901*902* @param field the calendar field.903* @param amount the amount of date or time to be added to the field.904* @exception IllegalArgumentException if <code>field</code> is905* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,906* or if any calendar fields have out-of-range values in907* non-lenient mode.908*/909@Override910public void add(int field, int amount) {911// If amount == 0, do nothing even the given field is out of912// range. This is tested by JCK.913if (amount == 0) {914return; // Do nothing!915}916917if (field < 0 || field >= ZONE_OFFSET) {918throw new IllegalArgumentException();919}920921// Sync the time and calendar fields.922complete();923924if (field == YEAR) {925int year = internalGet(YEAR);926if (internalGetEra() == CE) {927year += amount;928if (year > 0) {929set(YEAR, year);930} else { // year <= 0931set(YEAR, 1 - year);932// if year == 0, you get 1 BCE.933set(ERA, BCE);934}935}936else { // era == BCE937year -= amount;938if (year > 0) {939set(YEAR, year);940} else { // year <= 0941set(YEAR, 1 - year);942// if year == 0, you get 1 CE943set(ERA, CE);944}945}946pinDayOfMonth();947} else if (field == MONTH) {948int month = internalGet(MONTH) + amount;949int year = internalGet(YEAR);950int y_amount;951952if (month >= 0) {953y_amount = month/12;954} else {955y_amount = (month+1)/12 - 1;956}957if (y_amount != 0) {958if (internalGetEra() == CE) {959year += y_amount;960if (year > 0) {961set(YEAR, year);962} else { // year <= 0963set(YEAR, 1 - year);964// if year == 0, you get 1 BCE965set(ERA, BCE);966}967}968else { // era == BCE969year -= y_amount;970if (year > 0) {971set(YEAR, year);972} else { // year <= 0973set(YEAR, 1 - year);974// if year == 0, you get 1 CE975set(ERA, CE);976}977}978}979980if (month >= 0) {981set(MONTH, month % 12);982} else {983// month < 0984month %= 12;985if (month < 0) {986month += 12;987}988set(MONTH, JANUARY + month);989}990pinDayOfMonth();991} else if (field == ERA) {992int era = internalGet(ERA) + amount;993if (era < 0) {994era = 0;995}996if (era > 1) {997era = 1;998}999set(ERA, era);1000} else {1001long delta = amount;1002long timeOfDay = 0;1003switch (field) {1004// Handle the time fields here. Convert the given1005// amount to milliseconds and call setTimeInMillis.1006case HOUR:1007case HOUR_OF_DAY:1008delta *= 60 * 60 * 1000; // hours to minutes1009break;10101011case MINUTE:1012delta *= 60 * 1000; // minutes to seconds1013break;10141015case SECOND:1016delta *= 1000; // seconds to milliseconds1017break;10181019case MILLISECOND:1020break;10211022// Handle week, day and AM_PM fields which involves1023// time zone offset change adjustment. Convert the1024// given amount to the number of days.1025case WEEK_OF_YEAR:1026case WEEK_OF_MONTH:1027case DAY_OF_WEEK_IN_MONTH:1028delta *= 7;1029break;10301031case DAY_OF_MONTH: // synonym of DATE1032case DAY_OF_YEAR:1033case DAY_OF_WEEK:1034break;10351036case AM_PM:1037// Convert the amount to the number of days (delta)1038// and +12 or -12 hours (timeOfDay).1039delta = amount / 2;1040timeOfDay = 12 * (amount % 2);1041break;1042}10431044// The time fields don't require time zone offset change1045// adjustment.1046if (field >= HOUR) {1047setTimeInMillis(time + delta);1048return;1049}10501051// The rest of the fields (week, day or AM_PM fields)1052// require time zone offset (both GMT and DST) change1053// adjustment.10541055// Translate the current time to the fixed date and time1056// of the day.1057long fd = getCurrentFixedDate();1058timeOfDay += internalGet(HOUR_OF_DAY);1059timeOfDay *= 60;1060timeOfDay += internalGet(MINUTE);1061timeOfDay *= 60;1062timeOfDay += internalGet(SECOND);1063timeOfDay *= 1000;1064timeOfDay += internalGet(MILLISECOND);1065if (timeOfDay >= ONE_DAY) {1066fd++;1067timeOfDay -= ONE_DAY;1068} else if (timeOfDay < 0) {1069fd--;1070timeOfDay += ONE_DAY;1071}10721073fd += delta; // fd is the expected fixed date after the calculation1074int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);1075setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);1076zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);1077// If the time zone offset has changed, then adjust the difference.1078if (zoneOffset != 0) {1079setTimeInMillis(time + zoneOffset);1080long fd2 = getCurrentFixedDate();1081// If the adjustment has changed the date, then take1082// the previous one.1083if (fd2 != fd) {1084setTimeInMillis(time - zoneOffset);1085}1086}1087}1088}10891090/**1091* Adds or subtracts (up/down) a single unit of time on the given time1092* field without changing larger fields.1093* <p>1094* <em>Example</em>: Consider a <code>GregorianCalendar</code>1095* originally set to December 31, 1999. Calling {@link #roll(int,boolean) roll(Calendar.MONTH, true)}1096* sets the calendar to January 31, 1999. The <code>YEAR</code> field is unchanged1097* because it is a larger field than <code>MONTH</code>.</p>1098*1099* @param up indicates if the value of the specified calendar field is to be1100* rolled up or rolled down. Use <code>true</code> if rolling up, <code>false</code> otherwise.1101* @exception IllegalArgumentException if <code>field</code> is1102* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,1103* or if any calendar fields have out-of-range values in1104* non-lenient mode.1105* @see #add(int,int)1106* @see #set(int,int)1107*/1108@Override1109public void roll(int field, boolean up) {1110roll(field, up ? +1 : -1);1111}11121113/**1114* Adds a signed amount to the specified calendar field without changing larger fields.1115* A negative roll amount means to subtract from field without changing1116* larger fields. If the specified amount is 0, this method performs nothing.1117*1118* <p>This method calls {@link #complete()} before adding the1119* amount so that all the calendar fields are normalized. If there1120* is any calendar field having an out-of-range value in non-lenient mode, then an1121* <code>IllegalArgumentException</code> is thrown.1122*1123* <p>1124* <em>Example</em>: Consider a <code>GregorianCalendar</code>1125* originally set to August 31, 1999. Calling <code>roll(Calendar.MONTH,1126* 8)</code> sets the calendar to April 30, <strong>1999</strong>. Using a1127* <code>GregorianCalendar</code>, the <code>DAY_OF_MONTH</code> field cannot1128* be 31 in the month April. <code>DAY_OF_MONTH</code> is set to the closest possible1129* value, 30. The <code>YEAR</code> field maintains the value of 1999 because it1130* is a larger field than <code>MONTH</code>.1131* <p>1132* <em>Example</em>: Consider a <code>GregorianCalendar</code>1133* originally set to Sunday June 6, 1999. Calling1134* <code>roll(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to1135* Tuesday June 1, 1999, whereas calling1136* <code>add(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to1137* Sunday May 30, 1999. This is because the roll rule imposes an1138* additional constraint: The <code>MONTH</code> must not change when the1139* <code>WEEK_OF_MONTH</code> is rolled. Taken together with add rule 1,1140* the resultant date must be between Tuesday June 1 and Saturday June1141* 5. According to add rule 2, the <code>DAY_OF_WEEK</code>, an invariant1142* when changing the <code>WEEK_OF_MONTH</code>, is set to Tuesday, the1143* closest possible value to Sunday (where Sunday is the first day of the1144* week).</p>1145*1146* @param field the calendar field.1147* @param amount the signed amount to add to <code>field</code>.1148* @exception IllegalArgumentException if <code>field</code> is1149* <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,1150* or if any calendar fields have out-of-range values in1151* non-lenient mode.1152* @see #roll(int,boolean)1153* @see #add(int,int)1154* @see #set(int,int)1155* @since 1.21156*/1157@Override1158public void roll(int field, int amount) {1159// If amount == 0, do nothing even the given field is out of1160// range. This is tested by JCK.1161if (amount == 0) {1162return;1163}11641165if (field < 0 || field >= ZONE_OFFSET) {1166throw new IllegalArgumentException();1167}11681169// Sync the time and calendar fields.1170complete();11711172int min = getMinimum(field);1173int max = getMaximum(field);11741175switch (field) {1176case AM_PM:1177case ERA:1178case YEAR:1179case MINUTE:1180case SECOND:1181case MILLISECOND:1182// These fields are handled simply, since they have fixed minima1183// and maxima. The field DAY_OF_MONTH is almost as simple. Other1184// fields are complicated, since the range within they must roll1185// varies depending on the date.1186break;11871188case HOUR:1189case HOUR_OF_DAY:1190{1191int rolledValue = getRolledValue(internalGet(field), amount, min, max);1192int hourOfDay = rolledValue;1193if (field == HOUR && internalGet(AM_PM) == PM) {1194hourOfDay += 12;1195}11961197// Create the current date/time value to perform wall-clock-based1198// roll.1199CalendarDate d = calsys.getCalendarDate(time, getZone());1200d.setHours(hourOfDay);1201time = calsys.getTime(d);12021203// If we stay on the same wall-clock time, try the next or previous hour.1204if (internalGet(HOUR_OF_DAY) == d.getHours()) {1205hourOfDay = getRolledValue(rolledValue, amount > 0 ? +1 : -1, min, max);1206if (field == HOUR && internalGet(AM_PM) == PM) {1207hourOfDay += 12;1208}1209d.setHours(hourOfDay);1210time = calsys.getTime(d);1211}1212// Get the new hourOfDay value which might have changed due to a DST transition.1213hourOfDay = d.getHours();1214// Update the hour related fields1215internalSet(HOUR_OF_DAY, hourOfDay);1216internalSet(AM_PM, hourOfDay / 12);1217internalSet(HOUR, hourOfDay % 12);12181219// Time zone offset and/or daylight saving might have changed.1220int zoneOffset = d.getZoneOffset();1221int saving = d.getDaylightSaving();1222internalSet(ZONE_OFFSET, zoneOffset - saving);1223internalSet(DST_OFFSET, saving);1224return;1225}12261227case MONTH:1228// Rolling the month involves both pinning the final value to [0, 11]1229// and adjusting the DAY_OF_MONTH if necessary. We only adjust the1230// DAY_OF_MONTH if, after updating the MONTH field, it is illegal.1231// E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.1232{1233if (!isCutoverYear(cdate.getNormalizedYear())) {1234int mon = (internalGet(MONTH) + amount) % 12;1235if (mon < 0) {1236mon += 12;1237}1238set(MONTH, mon);12391240// Keep the day of month in the range. We don't want to spill over1241// into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->1242// mar3.1243int monthLen = monthLength(mon);1244if (internalGet(DAY_OF_MONTH) > monthLen) {1245set(DAY_OF_MONTH, monthLen);1246}1247} else {1248// We need to take care of different lengths in1249// year and month due to the cutover.1250int yearLength = getActualMaximum(MONTH) + 1;1251int mon = (internalGet(MONTH) + amount) % yearLength;1252if (mon < 0) {1253mon += yearLength;1254}1255set(MONTH, mon);1256int monthLen = getActualMaximum(DAY_OF_MONTH);1257if (internalGet(DAY_OF_MONTH) > monthLen) {1258set(DAY_OF_MONTH, monthLen);1259}1260}1261return;1262}12631264case WEEK_OF_YEAR:1265{1266int y = cdate.getNormalizedYear();1267max = getActualMaximum(WEEK_OF_YEAR);1268set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));1269int woy = internalGet(WEEK_OF_YEAR);1270int value = woy + amount;1271if (!isCutoverYear(y)) {1272int weekYear = getWeekYear();1273if (weekYear == y) {1274// If the new value is in between min and max1275// (exclusive), then we can use the value.1276if (value > min && value < max) {1277set(WEEK_OF_YEAR, value);1278return;1279}1280long fd = getCurrentFixedDate();1281// Make sure that the min week has the current DAY_OF_WEEK1282// in the calendar year1283long day1 = fd - (7 * (woy - min));1284if (calsys.getYearFromFixedDate(day1) != y) {1285min++;1286}12871288// Make sure the same thing for the max week1289fd += 7 * (max - internalGet(WEEK_OF_YEAR));1290if (calsys.getYearFromFixedDate(fd) != y) {1291max--;1292}1293} else {1294// When WEEK_OF_YEAR and YEAR are out of sync,1295// adjust woy and amount to stay in the calendar year.1296if (weekYear > y) {1297if (amount < 0) {1298amount++;1299}1300woy = max;1301} else {1302if (amount > 0) {1303amount -= woy - max;1304}1305woy = min;1306}1307}1308set(field, getRolledValue(woy, amount, min, max));1309return;1310}13111312// Handle cutover here.1313long fd = getCurrentFixedDate();1314BaseCalendar cal;1315if (gregorianCutoverYear == gregorianCutoverYearJulian) {1316cal = getCutoverCalendarSystem();1317} else if (y == gregorianCutoverYear) {1318cal = gcal;1319} else {1320cal = getJulianCalendarSystem();1321}1322long day1 = fd - (7 * (woy - min));1323// Make sure that the min week has the current DAY_OF_WEEK1324if (cal.getYearFromFixedDate(day1) != y) {1325min++;1326}13271328// Make sure the same thing for the max week1329fd += 7 * (max - woy);1330cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();1331if (cal.getYearFromFixedDate(fd) != y) {1332max--;1333}1334// value: the new WEEK_OF_YEAR which must be converted1335// to month and day of month.1336value = getRolledValue(woy, amount, min, max) - 1;1337BaseCalendar.Date d = getCalendarDate(day1 + value * 7);1338set(MONTH, d.getMonth() - 1);1339set(DAY_OF_MONTH, d.getDayOfMonth());1340return;1341}13421343case WEEK_OF_MONTH:1344{1345boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());1346// dow: relative day of week from first day of week1347int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();1348if (dow < 0) {1349dow += 7;1350}13511352long fd = getCurrentFixedDate();1353long month1; // fixed date of the first day (usually 1) of the month1354int monthLength; // actual month length1355if (isCutoverYear) {1356month1 = getFixedDateMonth1(cdate, fd);1357monthLength = actualMonthLength();1358} else {1359month1 = fd - internalGet(DAY_OF_MONTH) + 1;1360monthLength = calsys.getMonthLength(cdate);1361}13621363// the first day of week of the month.1364long monthDay1st = BaseCalendar.getDayOfWeekDateOnOrBefore(month1 + 6,1365getFirstDayOfWeek());1366// if the week has enough days to form a week, the1367// week starts from the previous month.1368if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {1369monthDay1st -= 7;1370}1371max = getActualMaximum(field);13721373// value: the new WEEK_OF_MONTH value1374int value = getRolledValue(internalGet(field), amount, 1, max) - 1;13751376// nfd: fixed date of the rolled date1377long nfd = monthDay1st + value * 7 + dow;13781379// Unlike WEEK_OF_YEAR, we need to change day of week if the1380// nfd is out of the month.1381if (nfd < month1) {1382nfd = month1;1383} else if (nfd >= (month1 + monthLength)) {1384nfd = month1 + monthLength - 1;1385}1386int dayOfMonth;1387if (isCutoverYear) {1388// If we are in the cutover year, convert nfd to1389// its calendar date and use dayOfMonth.1390BaseCalendar.Date d = getCalendarDate(nfd);1391dayOfMonth = d.getDayOfMonth();1392} else {1393dayOfMonth = (int)(nfd - month1) + 1;1394}1395set(DAY_OF_MONTH, dayOfMonth);1396return;1397}13981399case DAY_OF_MONTH:1400{1401if (!isCutoverYear(cdate.getNormalizedYear())) {1402max = calsys.getMonthLength(cdate);1403break;1404}14051406// Cutover year handling1407long fd = getCurrentFixedDate();1408long month1 = getFixedDateMonth1(cdate, fd);1409// It may not be a regular month. Convert the date and range to1410// the relative values, perform the roll, and1411// convert the result back to the rolled date.1412int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);1413BaseCalendar.Date d = getCalendarDate(month1 + value);1414assert d.getMonth()-1 == internalGet(MONTH);1415set(DAY_OF_MONTH, d.getDayOfMonth());1416return;1417}14181419case DAY_OF_YEAR:1420{1421max = getActualMaximum(field);1422if (!isCutoverYear(cdate.getNormalizedYear())) {1423break;1424}14251426// Handle cutover here.1427long fd = getCurrentFixedDate();1428long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;1429int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);1430BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);1431set(MONTH, d.getMonth() - 1);1432set(DAY_OF_MONTH, d.getDayOfMonth());1433return;1434}14351436case DAY_OF_WEEK:1437{1438if (!isCutoverYear(cdate.getNormalizedYear())) {1439// If the week of year is in the same year, we can1440// just change DAY_OF_WEEK.1441int weekOfYear = internalGet(WEEK_OF_YEAR);1442if (weekOfYear > 1 && weekOfYear < 52) {1443set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]1444max = SATURDAY;1445break;1446}1447}14481449// We need to handle it in a different way around year1450// boundaries and in the cutover year. Note that1451// changing era and year values violates the roll1452// rule: not changing larger calendar fields...1453amount %= 7;1454if (amount == 0) {1455return;1456}1457long fd = getCurrentFixedDate();1458long dowFirst = BaseCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());1459fd += amount;1460if (fd < dowFirst) {1461fd += 7;1462} else if (fd >= dowFirst + 7) {1463fd -= 7;1464}1465BaseCalendar.Date d = getCalendarDate(fd);1466set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));1467set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());1468return;1469}14701471case DAY_OF_WEEK_IN_MONTH:1472{1473min = 1; // after normalized, min should be 1.1474if (!isCutoverYear(cdate.getNormalizedYear())) {1475int dom = internalGet(DAY_OF_MONTH);1476int monthLength = calsys.getMonthLength(cdate);1477int lastDays = monthLength % 7;1478max = monthLength / 7;1479int x = (dom - 1) % 7;1480if (x < lastDays) {1481max++;1482}1483set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));1484break;1485}14861487// Cutover year handling1488long fd = getCurrentFixedDate();1489long month1 = getFixedDateMonth1(cdate, fd);1490int monthLength = actualMonthLength();1491int lastDays = monthLength % 7;1492max = monthLength / 7;1493int x = (int)(fd - month1) % 7;1494if (x < lastDays) {1495max++;1496}1497int value = getRolledValue(internalGet(field), amount, min, max) - 1;1498fd = month1 + value * 7 + x;1499BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();1500BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);1501cal.getCalendarDateFromFixedDate(d, fd);1502set(DAY_OF_MONTH, d.getDayOfMonth());1503return;1504}1505}15061507set(field, getRolledValue(internalGet(field), amount, min, max));1508}15091510/**1511* Returns the minimum value for the given calendar field of this1512* <code>GregorianCalendar</code> instance. The minimum value is1513* defined as the smallest value returned by the {@link1514* Calendar#get(int) get} method for any possible time value,1515* taking into consideration the current values of the1516* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1517* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1518* {@link #getGregorianChange() getGregorianChange} and1519* {@link Calendar#getTimeZone() getTimeZone} methods.1520*1521* @param field the calendar field.1522* @return the minimum value for the given calendar field.1523* @see #getMaximum(int)1524* @see #getGreatestMinimum(int)1525* @see #getLeastMaximum(int)1526* @see #getActualMinimum(int)1527* @see #getActualMaximum(int)1528*/1529@Override1530public int getMinimum(int field) {1531return MIN_VALUES[field];1532}15331534/**1535* Returns the maximum value for the given calendar field of this1536* <code>GregorianCalendar</code> instance. The maximum value is1537* defined as the largest value returned by the {@link1538* Calendar#get(int) get} method for any possible time value,1539* taking into consideration the current values of the1540* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1541* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1542* {@link #getGregorianChange() getGregorianChange} and1543* {@link Calendar#getTimeZone() getTimeZone} methods.1544*1545* @param field the calendar field.1546* @return the maximum value for the given calendar field.1547* @see #getMinimum(int)1548* @see #getGreatestMinimum(int)1549* @see #getLeastMaximum(int)1550* @see #getActualMinimum(int)1551* @see #getActualMaximum(int)1552*/1553@Override1554public int getMaximum(int field) {1555switch (field) {1556case MONTH:1557case DAY_OF_MONTH:1558case DAY_OF_YEAR:1559case WEEK_OF_YEAR:1560case WEEK_OF_MONTH:1561case DAY_OF_WEEK_IN_MONTH:1562case YEAR:1563{1564// On or after Gregorian 200-3-1, Julian and Gregorian1565// calendar dates are the same or Gregorian dates are1566// larger (i.e., there is a "gap") after 300-3-1.1567if (gregorianCutoverYear > 200) {1568break;1569}1570// There might be "overlapping" dates.1571GregorianCalendar gc = (GregorianCalendar) clone();1572gc.setLenient(true);1573gc.setTimeInMillis(gregorianCutover);1574int v1 = gc.getActualMaximum(field);1575gc.setTimeInMillis(gregorianCutover-1);1576int v2 = gc.getActualMaximum(field);1577return Math.max(MAX_VALUES[field], Math.max(v1, v2));1578}1579}1580return MAX_VALUES[field];1581}15821583/**1584* Returns the highest minimum value for the given calendar field1585* of this <code>GregorianCalendar</code> instance. The highest1586* minimum value is defined as the largest value returned by1587* {@link #getActualMinimum(int)} for any possible time value,1588* taking into consideration the current values of the1589* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1590* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1591* {@link #getGregorianChange() getGregorianChange} and1592* {@link Calendar#getTimeZone() getTimeZone} methods.1593*1594* @param field the calendar field.1595* @return the highest minimum value for the given calendar field.1596* @see #getMinimum(int)1597* @see #getMaximum(int)1598* @see #getLeastMaximum(int)1599* @see #getActualMinimum(int)1600* @see #getActualMaximum(int)1601*/1602@Override1603public int getGreatestMinimum(int field) {1604if (field == DAY_OF_MONTH) {1605BaseCalendar.Date d = getGregorianCutoverDate();1606long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);1607d = getCalendarDate(mon1);1608return Math.max(MIN_VALUES[field], d.getDayOfMonth());1609}1610return MIN_VALUES[field];1611}16121613/**1614* Returns the lowest maximum value for the given calendar field1615* of this <code>GregorianCalendar</code> instance. The lowest1616* maximum value is defined as the smallest value returned by1617* {@link #getActualMaximum(int)} for any possible time value,1618* taking into consideration the current values of the1619* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1620* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1621* {@link #getGregorianChange() getGregorianChange} and1622* {@link Calendar#getTimeZone() getTimeZone} methods.1623*1624* @param field the calendar field1625* @return the lowest maximum value for the given calendar field.1626* @see #getMinimum(int)1627* @see #getMaximum(int)1628* @see #getGreatestMinimum(int)1629* @see #getActualMinimum(int)1630* @see #getActualMaximum(int)1631*/1632@Override1633public int getLeastMaximum(int field) {1634switch (field) {1635case MONTH:1636case DAY_OF_MONTH:1637case DAY_OF_YEAR:1638case WEEK_OF_YEAR:1639case WEEK_OF_MONTH:1640case DAY_OF_WEEK_IN_MONTH:1641case YEAR:1642{1643GregorianCalendar gc = (GregorianCalendar) clone();1644gc.setLenient(true);1645gc.setTimeInMillis(gregorianCutover);1646int v1 = gc.getActualMaximum(field);1647gc.setTimeInMillis(gregorianCutover-1);1648int v2 = gc.getActualMaximum(field);1649return Math.min(LEAST_MAX_VALUES[field], Math.min(v1, v2));1650}1651}1652return LEAST_MAX_VALUES[field];1653}16541655/**1656* Returns the minimum value that this calendar field could have,1657* taking into consideration the given time value and the current1658* values of the1659* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1660* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1661* {@link #getGregorianChange() getGregorianChange} and1662* {@link Calendar#getTimeZone() getTimeZone} methods.1663*1664* <p>For example, if the Gregorian change date is January 10,1665* 1970 and the date of this <code>GregorianCalendar</code> is1666* January 20, 1970, the actual minimum value of the1667* <code>DAY_OF_MONTH</code> field is 10 because the previous date1668* of January 10, 1970 is December 27, 1996 (in the Julian1669* calendar). Therefore, December 28, 1969 to January 9, 19701670* don't exist.1671*1672* @param field the calendar field1673* @return the minimum of the given field for the time value of1674* this <code>GregorianCalendar</code>1675* @see #getMinimum(int)1676* @see #getMaximum(int)1677* @see #getGreatestMinimum(int)1678* @see #getLeastMaximum(int)1679* @see #getActualMaximum(int)1680* @since 1.21681*/1682@Override1683public int getActualMinimum(int field) {1684if (field == DAY_OF_MONTH) {1685GregorianCalendar gc = getNormalizedCalendar();1686int year = gc.cdate.getNormalizedYear();1687if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {1688long month1 = getFixedDateMonth1(gc.cdate, gc.calsys.getFixedDate(gc.cdate));1689BaseCalendar.Date d = getCalendarDate(month1);1690return d.getDayOfMonth();1691}1692}1693return getMinimum(field);1694}16951696/**1697* Returns the maximum value that this calendar field could have,1698* taking into consideration the given time value and the current1699* values of the1700* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1701* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1702* {@link #getGregorianChange() getGregorianChange} and1703* {@link Calendar#getTimeZone() getTimeZone} methods.1704* For example, if the date of this instance is February 1, 2004,1705* the actual maximum value of the <code>DAY_OF_MONTH</code> field1706* is 29 because 2004 is a leap year, and if the date of this1707* instance is February 1, 2005, it's 28.1708*1709* <p>This method calculates the maximum value of {@link1710* Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link1711* Calendar#YEAR YEAR} (calendar year) value, not the <a1712* href="#week_year">week year</a>. Call {@link1713* #getWeeksInWeekYear()} to get the maximum value of {@code1714* WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.1715*1716* @param field the calendar field1717* @return the maximum of the given field for the time value of1718* this <code>GregorianCalendar</code>1719* @see #getMinimum(int)1720* @see #getMaximum(int)1721* @see #getGreatestMinimum(int)1722* @see #getLeastMaximum(int)1723* @see #getActualMinimum(int)1724* @since 1.21725*/1726@Override1727public int getActualMaximum(int field) {1728final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|1729HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|1730ZONE_OFFSET_MASK|DST_OFFSET_MASK;1731if ((fieldsForFixedMax & (1<<field)) != 0) {1732return getMaximum(field);1733}17341735GregorianCalendar gc = getNormalizedCalendar();1736BaseCalendar.Date date = gc.cdate;1737BaseCalendar cal = gc.calsys;1738int normalizedYear = date.getNormalizedYear();17391740int value = -1;1741switch (field) {1742case MONTH:1743{1744if (!gc.isCutoverYear(normalizedYear)) {1745value = DECEMBER;1746break;1747}17481749// January 1 of the next year may or may not exist.1750long nextJan1;1751do {1752nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);1753} while (nextJan1 < gregorianCutoverDate);1754BaseCalendar.Date d = (BaseCalendar.Date) date.clone();1755cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);1756value = d.getMonth() - 1;1757}1758break;17591760case DAY_OF_MONTH:1761{1762value = cal.getMonthLength(date);1763if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {1764break;1765}17661767// Handle cutover year.1768long fd = gc.getCurrentFixedDate();1769if (fd >= gregorianCutoverDate) {1770break;1771}1772int monthLength = gc.actualMonthLength();1773long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;1774// Convert the fixed date to its calendar date.1775BaseCalendar.Date d = gc.getCalendarDate(monthEnd);1776value = d.getDayOfMonth();1777}1778break;17791780case DAY_OF_YEAR:1781{1782if (!gc.isCutoverYear(normalizedYear)) {1783value = cal.getYearLength(date);1784break;1785}17861787// Handle cutover year.1788long jan1;1789if (gregorianCutoverYear == gregorianCutoverYearJulian) {1790BaseCalendar cocal = gc.getCutoverCalendarSystem();1791jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);1792} else if (normalizedYear == gregorianCutoverYearJulian) {1793jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);1794} else {1795jan1 = gregorianCutoverDate;1796}1797// January 1 of the next year may or may not exist.1798long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);1799if (nextJan1 < gregorianCutoverDate) {1800nextJan1 = gregorianCutoverDate;1801}1802assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),1803date.getDayOfMonth(), date);1804assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),1805date.getDayOfMonth(), date);1806value = (int)(nextJan1 - jan1);1807}1808break;18091810case WEEK_OF_YEAR:1811{1812if (!gc.isCutoverYear(normalizedYear)) {1813// Get the day of week of January 1 of the year1814CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);1815d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);1816int dayOfWeek = cal.getDayOfWeek(d);1817// Normalize the day of week with the firstDayOfWeek value1818dayOfWeek -= getFirstDayOfWeek();1819if (dayOfWeek < 0) {1820dayOfWeek += 7;1821}1822value = 52;1823int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;1824if ((magic == 6) ||1825(date.isLeapYear() && (magic == 5 || magic == 12))) {1826value++;1827}1828break;1829}18301831if (gc == this) {1832gc = (GregorianCalendar) gc.clone();1833}1834int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);1835gc.set(DAY_OF_YEAR, maxDayOfYear);1836value = gc.get(WEEK_OF_YEAR);1837if (internalGet(YEAR) != gc.getWeekYear()) {1838gc.set(DAY_OF_YEAR, maxDayOfYear - 7);1839value = gc.get(WEEK_OF_YEAR);1840}1841}1842break;18431844case WEEK_OF_MONTH:1845{1846if (!gc.isCutoverYear(normalizedYear)) {1847CalendarDate d = cal.newCalendarDate(null);1848d.setDate(date.getYear(), date.getMonth(), 1);1849int dayOfWeek = cal.getDayOfWeek(d);1850int monthLength = cal.getMonthLength(d);1851dayOfWeek -= getFirstDayOfWeek();1852if (dayOfWeek < 0) {1853dayOfWeek += 7;1854}1855int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week1856value = 3;1857if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {1858value++;1859}1860monthLength -= nDaysFirstWeek + 7 * 3;1861if (monthLength > 0) {1862value++;1863if (monthLength > 7) {1864value++;1865}1866}1867break;1868}18691870// Cutover year handling1871if (gc == this) {1872gc = (GregorianCalendar) gc.clone();1873}1874int y = gc.internalGet(YEAR);1875int m = gc.internalGet(MONTH);1876do {1877value = gc.get(WEEK_OF_MONTH);1878gc.add(WEEK_OF_MONTH, +1);1879} while (gc.get(YEAR) == y && gc.get(MONTH) == m);1880}1881break;18821883case DAY_OF_WEEK_IN_MONTH:1884{1885// may be in the Gregorian cutover month1886int ndays, dow1;1887int dow = date.getDayOfWeek();1888if (!gc.isCutoverYear(normalizedYear)) {1889BaseCalendar.Date d = (BaseCalendar.Date) date.clone();1890ndays = cal.getMonthLength(d);1891d.setDayOfMonth(1);1892cal.normalize(d);1893dow1 = d.getDayOfWeek();1894} else {1895// Let a cloned GregorianCalendar take care of the cutover cases.1896if (gc == this) {1897gc = (GregorianCalendar) clone();1898}1899ndays = gc.actualMonthLength();1900gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));1901dow1 = gc.get(DAY_OF_WEEK);1902}1903int x = dow - dow1;1904if (x < 0) {1905x += 7;1906}1907ndays -= x;1908value = (ndays + 6) / 7;1909}1910break;19111912case YEAR:1913/* The year computation is no different, in principle, from the1914* others, however, the range of possible maxima is large. In1915* addition, the way we know we've exceeded the range is different.1916* For these reasons, we use the special case code below to handle1917* this field.1918*1919* The actual maxima for YEAR depend on the type of calendar:1920*1921* Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE1922* Julian = Dec 2, 292269055 BCE - Jan 3, 292272993 CE1923* Hybrid = Dec 2, 292269055 BCE - Aug 17, 292278994 CE1924*1925* We know we've exceeded the maximum when either the month, date,1926* time, or era changes in response to setting the year. We don't1927* check for month, date, and time here because the year and era are1928* sufficient to detect an invalid year setting. NOTE: If code is1929* added to check the month and date in the future for some reason,1930* Feb 29 must be allowed to shift to Mar 1 when setting the year.1931*/1932{1933if (gc == this) {1934gc = (GregorianCalendar) clone();1935}19361937// Calculate the millisecond offset from the beginning1938// of the year of this calendar and adjust the max1939// year value if we are beyond the limit in the max1940// year.1941long current = gc.getYearOffsetInMillis();19421943if (gc.internalGetEra() == CE) {1944gc.setTimeInMillis(Long.MAX_VALUE);1945value = gc.get(YEAR);1946long maxEnd = gc.getYearOffsetInMillis();1947if (current > maxEnd) {1948value--;1949}1950} else {1951CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?1952gcal : getJulianCalendarSystem();1953CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());1954long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();1955maxEnd *= 60;1956maxEnd += d.getMinutes();1957maxEnd *= 60;1958maxEnd += d.getSeconds();1959maxEnd *= 1000;1960maxEnd += d.getMillis();1961value = d.getYear();1962if (value <= 0) {1963assert mincal == gcal;1964value = 1 - value;1965}1966if (current < maxEnd) {1967value--;1968}1969}1970}1971break;19721973default:1974throw new ArrayIndexOutOfBoundsException(field);1975}1976return value;1977}19781979/**1980* Returns the millisecond offset from the beginning of this1981* year. This Calendar object must have been normalized.1982*/1983private long getYearOffsetInMillis() {1984long t = (internalGet(DAY_OF_YEAR) - 1) * 24;1985t += internalGet(HOUR_OF_DAY);1986t *= 60;1987t += internalGet(MINUTE);1988t *= 60;1989t += internalGet(SECOND);1990t *= 1000;1991return t + internalGet(MILLISECOND) -1992(internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));1993}19941995@Override1996public Object clone()1997{1998GregorianCalendar other = (GregorianCalendar) super.clone();19992000other.gdate = (BaseCalendar.Date) gdate.clone();2001if (cdate != null) {2002if (cdate != gdate) {2003other.cdate = (BaseCalendar.Date) cdate.clone();2004} else {2005other.cdate = other.gdate;2006}2007}2008other.originalFields = null;2009other.zoneOffsets = null;2010return other;2011}20122013@Override2014public TimeZone getTimeZone() {2015TimeZone zone = super.getTimeZone();2016// To share the zone by CalendarDates2017gdate.setZone(zone);2018if (cdate != null && cdate != gdate) {2019cdate.setZone(zone);2020}2021return zone;2022}20232024@Override2025public void setTimeZone(TimeZone zone) {2026super.setTimeZone(zone);2027// To share the zone by CalendarDates2028gdate.setZone(zone);2029if (cdate != null && cdate != gdate) {2030cdate.setZone(zone);2031}2032}20332034/**2035* Returns {@code true} indicating this {@code GregorianCalendar}2036* supports week dates.2037*2038* @return {@code true} (always)2039* @see #getWeekYear()2040* @see #setWeekDate(int,int,int)2041* @see #getWeeksInWeekYear()2042* @since 1.72043*/2044@Override2045public final boolean isWeekDateSupported() {2046return true;2047}20482049/**2050* Returns the <a href="#week_year">week year</a> represented by this2051* {@code GregorianCalendar}. The dates in the weeks between 1 and the2052* maximum week number of the week year have the same week year value2053* that may be one year before or after the {@link Calendar#YEAR YEAR}2054* (calendar year) value.2055*2056* <p>This method calls {@link Calendar#complete()} before2057* calculating the week year.2058*2059* @return the week year represented by this {@code GregorianCalendar}.2060* If the {@link Calendar#ERA ERA} value is {@link #BC}, the year is2061* represented by 0 or a negative number: BC 1 is 0, BC 22062* is -1, BC 3 is -2, and so on.2063* @throws IllegalArgumentException2064* if any of the calendar fields is invalid in non-lenient mode.2065* @see #isWeekDateSupported()2066* @see #getWeeksInWeekYear()2067* @see Calendar#getFirstDayOfWeek()2068* @see Calendar#getMinimalDaysInFirstWeek()2069* @since 1.72070*/2071@Override2072public int getWeekYear() {2073int year = get(YEAR); // implicitly calls complete()2074if (internalGetEra() == BCE) {2075year = 1 - year;2076}20772078// Fast path for the Gregorian calendar years that are never2079// affected by the Julian-Gregorian transition2080if (year > gregorianCutoverYear + 1) {2081int weekOfYear = internalGet(WEEK_OF_YEAR);2082if (internalGet(MONTH) == JANUARY) {2083if (weekOfYear >= 52) {2084--year;2085}2086} else {2087if (weekOfYear == 1) {2088++year;2089}2090}2091return year;2092}20932094// General (slow) path2095int dayOfYear = internalGet(DAY_OF_YEAR);2096int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);2097int minimalDays = getMinimalDaysInFirstWeek();20982099// Quickly check the possibility of year adjustments before2100// cloning this GregorianCalendar.2101if (dayOfYear > minimalDays && dayOfYear < (maxDayOfYear - 6)) {2102return year;2103}21042105// Create a clone to work on the calculation2106GregorianCalendar cal = (GregorianCalendar) clone();2107cal.setLenient(true);2108// Use GMT so that intermediate date calculations won't2109// affect the time of day fields.2110cal.setTimeZone(TimeZone.getTimeZone("GMT"));2111// Go to the first day of the year, which is usually January 1.2112cal.set(DAY_OF_YEAR, 1);2113cal.complete();21142115// Get the first day of the first day-of-week in the year.2116int delta = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);2117if (delta != 0) {2118if (delta < 0) {2119delta += 7;2120}2121cal.add(DAY_OF_YEAR, delta);2122}2123int minDayOfYear = cal.get(DAY_OF_YEAR);2124if (dayOfYear < minDayOfYear) {2125if (minDayOfYear <= minimalDays) {2126--year;2127}2128} else {2129cal.set(YEAR, year + 1);2130cal.set(DAY_OF_YEAR, 1);2131cal.complete();2132int del = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);2133if (del != 0) {2134if (del < 0) {2135del += 7;2136}2137cal.add(DAY_OF_YEAR, del);2138}2139minDayOfYear = cal.get(DAY_OF_YEAR) - 1;2140if (minDayOfYear == 0) {2141minDayOfYear = 7;2142}2143if (minDayOfYear >= minimalDays) {2144int days = maxDayOfYear - dayOfYear + 1;2145if (days <= (7 - minDayOfYear)) {2146++year;2147}2148}2149}2150return year;2151}21522153/**2154* Sets this {@code GregorianCalendar} to the date given by the2155* date specifiers - <a href="#week_year">{@code weekYear}</a>,2156* {@code weekOfYear}, and {@code dayOfWeek}. {@code weekOfYear}2157* follows the <a href="#week_and_year">{@code WEEK_OF_YEAR}2158* numbering</a>. The {@code dayOfWeek} value must be one of the2159* {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} values: {@link2160* Calendar#SUNDAY SUNDAY} to {@link Calendar#SATURDAY SATURDAY}.2161*2162* <p>Note that the numeric day-of-week representation differs from2163* the ISO 8601 standard, and that the {@code weekOfYear}2164* numbering is compatible with the standard when {@code2165* getFirstDayOfWeek()} is {@code MONDAY} and {@code2166* getMinimalDaysInFirstWeek()} is 4.2167*2168* <p>Unlike the {@code set} method, all of the calendar fields2169* and the instant of time value are calculated upon return.2170*2171* <p>If {@code weekOfYear} is out of the valid week-of-year2172* range in {@code weekYear}, the {@code weekYear}2173* and {@code weekOfYear} values are adjusted in lenient2174* mode, or an {@code IllegalArgumentException} is thrown in2175* non-lenient mode.2176*2177* @param weekYear the week year2178* @param weekOfYear the week number based on {@code weekYear}2179* @param dayOfWeek the day of week value: one of the constants2180* for the {@link #DAY_OF_WEEK DAY_OF_WEEK} field:2181* {@link Calendar#SUNDAY SUNDAY}, ...,2182* {@link Calendar#SATURDAY SATURDAY}.2183* @exception IllegalArgumentException2184* if any of the given date specifiers is invalid,2185* or if any of the calendar fields are inconsistent2186* with the given date specifiers in non-lenient mode2187* @see GregorianCalendar#isWeekDateSupported()2188* @see Calendar#getFirstDayOfWeek()2189* @see Calendar#getMinimalDaysInFirstWeek()2190* @since 1.72191*/2192@Override2193public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {2194if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {2195throw new IllegalArgumentException("invalid dayOfWeek: " + dayOfWeek);2196}21972198// To avoid changing the time of day fields by date2199// calculations, use a clone with the GMT time zone.2200GregorianCalendar gc = (GregorianCalendar) clone();2201gc.setLenient(true);2202int era = gc.get(ERA);2203gc.clear();2204gc.setTimeZone(TimeZone.getTimeZone("GMT"));2205gc.set(ERA, era);2206gc.set(YEAR, weekYear);2207gc.set(WEEK_OF_YEAR, 1);2208gc.set(DAY_OF_WEEK, getFirstDayOfWeek());2209int days = dayOfWeek - getFirstDayOfWeek();2210if (days < 0) {2211days += 7;2212}2213days += 7 * (weekOfYear - 1);2214if (days != 0) {2215gc.add(DAY_OF_YEAR, days);2216} else {2217gc.complete();2218}22192220if (!isLenient() &&2221(gc.getWeekYear() != weekYear2222|| gc.internalGet(WEEK_OF_YEAR) != weekOfYear2223|| gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {2224throw new IllegalArgumentException();2225}22262227set(ERA, gc.internalGet(ERA));2228set(YEAR, gc.internalGet(YEAR));2229set(MONTH, gc.internalGet(MONTH));2230set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));22312232// to avoid throwing an IllegalArgumentException in2233// non-lenient, set WEEK_OF_YEAR internally2234internalSet(WEEK_OF_YEAR, weekOfYear);2235complete();2236}22372238/**2239* Returns the number of weeks in the <a href="#week_year">week year</a>2240* represented by this {@code GregorianCalendar}.2241*2242* <p>For example, if this {@code GregorianCalendar}'s date is2243* December 31, 2008 with <a href="#iso8601_compatible_setting">the ISO2244* 8601 compatible setting</a>, this method will return 53 for the2245* period: December 29, 2008 to January 3, 2010 while {@link2246* #getActualMaximum(int) getActualMaximum(WEEK_OF_YEAR)} will return2247* 52 for the period: December 31, 2007 to December 28, 2008.2248*2249* @return the number of weeks in the week year.2250* @see Calendar#WEEK_OF_YEAR2251* @see #getWeekYear()2252* @see #getActualMaximum(int)2253* @since 1.72254*/2255@Override2256public int getWeeksInWeekYear() {2257GregorianCalendar gc = getNormalizedCalendar();2258int weekYear = gc.getWeekYear();2259if (weekYear == gc.internalGet(YEAR)) {2260return gc.getActualMaximum(WEEK_OF_YEAR);2261}22622263// Use the 2nd week for calculating the max of WEEK_OF_YEAR2264if (gc == this) {2265gc = (GregorianCalendar) gc.clone();2266}2267gc.setWeekDate(weekYear, 2, internalGet(DAY_OF_WEEK));2268return gc.getActualMaximum(WEEK_OF_YEAR);2269}22702271/////////////////////////////2272// Time => Fields computation2273/////////////////////////////22742275/**2276* The fixed date corresponding to gdate. If the value is2277* Long.MIN_VALUE, the fixed date value is unknown. Currently,2278* Julian calendar dates are not cached.2279*/2280transient private long cachedFixedDate = Long.MIN_VALUE;22812282/**2283* Converts the time value (millisecond offset from the <a2284* href="Calendar.html#Epoch">Epoch</a>) to calendar field values.2285* The time is <em>not</em>2286* recomputed first; to recompute the time, then the fields, call the2287* <code>complete</code> method.2288*2289* @see Calendar#complete2290*/2291@Override2292protected void computeFields() {2293int mask;2294if (isPartiallyNormalized()) {2295// Determine which calendar fields need to be computed.2296mask = getSetStateFields();2297int fieldMask = ~mask & ALL_FIELDS;2298// We have to call computTime in case calsys == null in2299// order to set calsys and cdate. (6263644)2300if (fieldMask != 0 || calsys == null) {2301mask |= computeFields(fieldMask,2302mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));2303assert mask == ALL_FIELDS;2304}2305} else {2306mask = ALL_FIELDS;2307computeFields(mask, 0);2308}2309// After computing all the fields, set the field state to `COMPUTED'.2310setFieldsComputed(mask);2311}23122313/**2314* This computeFields implements the conversion from UTC2315* (millisecond offset from the Epoch) to calendar2316* field values. fieldMask specifies which fields to change the2317* setting state to COMPUTED, although all fields are set to2318* the correct values. This is required to fix 4685354.2319*2320* @param fieldMask a bit mask to specify which fields to change2321* the setting state.2322* @param tzMask a bit mask to specify which time zone offset2323* fields to be used for time calculations2324* @return a new field mask that indicates what field values have2325* actually been set.2326*/2327private int computeFields(int fieldMask, int tzMask) {2328int zoneOffset = 0;2329TimeZone tz = getZone();2330if (zoneOffsets == null) {2331zoneOffsets = new int[2];2332}2333if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {2334if (tz instanceof ZoneInfo) {2335zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);2336} else {2337zoneOffset = tz.getOffset(time);2338zoneOffsets[0] = tz.getRawOffset();2339zoneOffsets[1] = zoneOffset - zoneOffsets[0];2340}2341}2342if (tzMask != 0) {2343if (isFieldSet(tzMask, ZONE_OFFSET)) {2344zoneOffsets[0] = internalGet(ZONE_OFFSET);2345}2346if (isFieldSet(tzMask, DST_OFFSET)) {2347zoneOffsets[1] = internalGet(DST_OFFSET);2348}2349zoneOffset = zoneOffsets[0] + zoneOffsets[1];2350}23512352// By computing time and zoneOffset separately, we can take2353// the wider range of time+zoneOffset than the previous2354// implementation.2355long fixedDate = zoneOffset / ONE_DAY;2356int timeOfDay = zoneOffset % (int)ONE_DAY;2357fixedDate += time / ONE_DAY;2358timeOfDay += (int) (time % ONE_DAY);2359if (timeOfDay >= ONE_DAY) {2360timeOfDay -= ONE_DAY;2361++fixedDate;2362} else {2363while (timeOfDay < 0) {2364timeOfDay += ONE_DAY;2365--fixedDate;2366}2367}2368fixedDate += EPOCH_OFFSET;23692370int era = CE;2371int year;2372if (fixedDate >= gregorianCutoverDate) {2373// Handle Gregorian dates.2374assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized()2375: "cache control: not normalized";2376assert cachedFixedDate == Long.MIN_VALUE ||2377gcal.getFixedDate(gdate.getNormalizedYear(),2378gdate.getMonth(),2379gdate.getDayOfMonth(), gdate)2380== cachedFixedDate2381: "cache control: inconsictency" +2382", cachedFixedDate=" + cachedFixedDate +2383", computed=" +2384gcal.getFixedDate(gdate.getNormalizedYear(),2385gdate.getMonth(),2386gdate.getDayOfMonth(),2387gdate) +2388", date=" + gdate;23892390// See if we can use gdate to avoid date calculation.2391if (fixedDate != cachedFixedDate) {2392gcal.getCalendarDateFromFixedDate(gdate, fixedDate);2393cachedFixedDate = fixedDate;2394}23952396year = gdate.getYear();2397if (year <= 0) {2398year = 1 - year;2399era = BCE;2400}2401calsys = gcal;2402cdate = gdate;2403assert cdate.getDayOfWeek() > 0 : "dow="+cdate.getDayOfWeek()+", date="+cdate;2404} else {2405// Handle Julian calendar dates.2406calsys = getJulianCalendarSystem();2407cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());2408jcal.getCalendarDateFromFixedDate(cdate, fixedDate);2409Era e = cdate.getEra();2410if (e == jeras[0]) {2411era = BCE;2412}2413year = cdate.getYear();2414}24152416// Always set the ERA and YEAR values.2417internalSet(ERA, era);2418internalSet(YEAR, year);2419int mask = fieldMask | (ERA_MASK|YEAR_MASK);24202421int month = cdate.getMonth() - 1; // 0-based2422int dayOfMonth = cdate.getDayOfMonth();24232424// Set the basic date fields.2425if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))2426!= 0) {2427internalSet(MONTH, month);2428internalSet(DAY_OF_MONTH, dayOfMonth);2429internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());2430mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;2431}24322433if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK2434|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {2435if (timeOfDay != 0) {2436int hours = timeOfDay / ONE_HOUR;2437internalSet(HOUR_OF_DAY, hours);2438internalSet(AM_PM, hours / 12); // Assume AM == 02439internalSet(HOUR, hours % 12);2440int r = timeOfDay % ONE_HOUR;2441internalSet(MINUTE, r / ONE_MINUTE);2442r %= ONE_MINUTE;2443internalSet(SECOND, r / ONE_SECOND);2444internalSet(MILLISECOND, r % ONE_SECOND);2445} else {2446internalSet(HOUR_OF_DAY, 0);2447internalSet(AM_PM, AM);2448internalSet(HOUR, 0);2449internalSet(MINUTE, 0);2450internalSet(SECOND, 0);2451internalSet(MILLISECOND, 0);2452}2453mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK2454|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);2455}24562457if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {2458internalSet(ZONE_OFFSET, zoneOffsets[0]);2459internalSet(DST_OFFSET, zoneOffsets[1]);2460mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);2461}24622463if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {2464int normalizedYear = cdate.getNormalizedYear();2465long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);2466int dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;2467long fixedDateMonth1 = fixedDate - dayOfMonth + 1;2468int cutoverGap = 0;2469int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;2470int relativeDayOfMonth = dayOfMonth - 1;24712472// If we are in the cutover year, we need some special handling.2473if (normalizedYear == cutoverYear) {2474// Need to take care of the "missing" days.2475if (gregorianCutoverYearJulian <= gregorianCutoverYear) {2476// We need to find out where we are. The cutover2477// gap could even be more than one year. (One2478// year difference in ~48667 years.)2479fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);2480if (fixedDate >= gregorianCutoverDate) {2481fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);2482}2483}2484int realDayOfYear = (int)(fixedDate - fixedDateJan1) + 1;2485cutoverGap = dayOfYear - realDayOfYear;2486dayOfYear = realDayOfYear;2487relativeDayOfMonth = (int)(fixedDate - fixedDateMonth1);2488}2489internalSet(DAY_OF_YEAR, dayOfYear);2490internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);24912492int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);24932494// The spec is to calculate WEEK_OF_YEAR in the2495// ISO8601-style. This creates problems, though.2496if (weekOfYear == 0) {2497// If the date belongs to the last week of the2498// previous year, use the week number of "12/31" of2499// the "previous" year. Again, if the previous year is2500// the Gregorian cutover year, we need to take care of2501// it. Usually the previous day of January 1 is2502// December 31, which is not always true in2503// GregorianCalendar.2504long fixedDec31 = fixedDateJan1 - 1;2505long prevJan1 = fixedDateJan1 - 365;2506if (normalizedYear > (cutoverYear + 1)) {2507if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {2508--prevJan1;2509}2510} else if (normalizedYear <= gregorianCutoverYearJulian) {2511if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {2512--prevJan1;2513}2514} else {2515BaseCalendar calForJan1 = calsys;2516//int prevYear = normalizedYear - 1;2517int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();2518if (prevYear == gregorianCutoverYear) {2519calForJan1 = getCutoverCalendarSystem();2520if (calForJan1 == jcal) {2521prevJan1 = calForJan1.getFixedDate(prevYear,2522BaseCalendar.JANUARY,25231,2524null);2525} else {2526prevJan1 = gregorianCutoverDate;2527calForJan1 = gcal;2528}2529} else if (prevYear <= gregorianCutoverYearJulian) {2530calForJan1 = getJulianCalendarSystem();2531prevJan1 = calForJan1.getFixedDate(prevYear,2532BaseCalendar.JANUARY,25331,2534null);2535}2536}2537weekOfYear = getWeekNumber(prevJan1, fixedDec31);2538} else {2539if (normalizedYear > gregorianCutoverYear ||2540normalizedYear < (gregorianCutoverYearJulian - 1)) {2541// Regular years2542if (weekOfYear >= 52) {2543long nextJan1 = fixedDateJan1 + 365;2544if (cdate.isLeapYear()) {2545nextJan1++;2546}2547long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,2548getFirstDayOfWeek());2549int ndays = (int)(nextJan1st - nextJan1);2550if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {2551// The first days forms a week in which the date is included.2552weekOfYear = 1;2553}2554}2555} else {2556BaseCalendar calForJan1 = calsys;2557int nextYear = normalizedYear + 1;2558if (nextYear == (gregorianCutoverYearJulian + 1) &&2559nextYear < gregorianCutoverYear) {2560// In case the gap is more than one year.2561nextYear = gregorianCutoverYear;2562}2563if (nextYear == gregorianCutoverYear) {2564calForJan1 = getCutoverCalendarSystem();2565}25662567long nextJan1;2568if (nextYear > gregorianCutoverYear2569|| gregorianCutoverYearJulian == gregorianCutoverYear2570|| nextYear == gregorianCutoverYearJulian) {2571nextJan1 = calForJan1.getFixedDate(nextYear,2572BaseCalendar.JANUARY,25731,2574null);2575} else {2576nextJan1 = gregorianCutoverDate;2577calForJan1 = gcal;2578}25792580long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,2581getFirstDayOfWeek());2582int ndays = (int)(nextJan1st - nextJan1);2583if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {2584// The first days forms a week in which the date is included.2585weekOfYear = 1;2586}2587}2588}2589internalSet(WEEK_OF_YEAR, weekOfYear);2590internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));2591mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK);2592}2593return mask;2594}25952596/**2597* Returns the number of weeks in a period between fixedDay1 and2598* fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule2599* is applied to calculate the number of weeks.2600*2601* @param fixedDay1 the fixed date of the first day of the period2602* @param fixedDate the fixed date of the last day of the period2603* @return the number of weeks of the given period2604*/2605private int getWeekNumber(long fixedDay1, long fixedDate) {2606// We can always use `gcal' since Julian and Gregorian are the2607// same thing for this calculation.2608long fixedDay1st = Gregorian.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,2609getFirstDayOfWeek());2610int ndays = (int)(fixedDay1st - fixedDay1);2611assert ndays <= 7;2612if (ndays >= getMinimalDaysInFirstWeek()) {2613fixedDay1st -= 7;2614}2615int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);2616if (normalizedDayOfPeriod >= 0) {2617return normalizedDayOfPeriod / 7 + 1;2618}2619return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;2620}26212622/**2623* Converts calendar field values to the time value (millisecond2624* offset from the <a href="Calendar.html#Epoch">Epoch</a>).2625*2626* @exception IllegalArgumentException if any calendar fields are invalid.2627*/2628@Override2629protected void computeTime() {2630// In non-lenient mode, perform brief checking of calendar2631// fields which have been set externally. Through this2632// checking, the field values are stored in originalFields[]2633// to see if any of them are normalized later.2634if (!isLenient()) {2635if (originalFields == null) {2636originalFields = new int[FIELD_COUNT];2637}2638for (int field = 0; field < FIELD_COUNT; field++) {2639int value = internalGet(field);2640if (isExternallySet(field)) {2641// Quick validation for any out of range values2642if (value < getMinimum(field) || value > getMaximum(field)) {2643throw new IllegalArgumentException(getFieldName(field));2644}2645}2646originalFields[field] = value;2647}2648}26492650// Let the super class determine which calendar fields to be2651// used to calculate the time.2652int fieldMask = selectFields();26532654// The year defaults to the epoch start. We don't check2655// fieldMask for YEAR because YEAR is a mandatory field to2656// determine the date.2657int year = isSet(YEAR) ? internalGet(YEAR) : EPOCH_YEAR;26582659int era = internalGetEra();2660if (era == BCE) {2661year = 1 - year;2662} else if (era != CE) {2663// Even in lenient mode we disallow ERA values other than CE & BCE.2664// (The same normalization rule as add()/roll() could be2665// applied here in lenient mode. But this checking is kept2666// unchanged for compatibility as of 1.5.)2667throw new IllegalArgumentException("Invalid era");2668}26692670// If year is 0 or negative, we need to set the ERA value later.2671if (year <= 0 && !isSet(ERA)) {2672fieldMask |= ERA_MASK;2673setFieldsComputed(ERA_MASK);2674}26752676// Calculate the time of day. We rely on the convention that2677// an UNSET field has 0.2678long timeOfDay = 0;2679if (isFieldSet(fieldMask, HOUR_OF_DAY)) {2680timeOfDay += (long) internalGet(HOUR_OF_DAY);2681} else {2682timeOfDay += internalGet(HOUR);2683// The default value of AM_PM is 0 which designates AM.2684if (isFieldSet(fieldMask, AM_PM)) {2685timeOfDay += 12 * internalGet(AM_PM);2686}2687}2688timeOfDay *= 60;2689timeOfDay += internalGet(MINUTE);2690timeOfDay *= 60;2691timeOfDay += internalGet(SECOND);2692timeOfDay *= 1000;2693timeOfDay += internalGet(MILLISECOND);26942695// Convert the time of day to the number of days and the2696// millisecond offset from midnight.2697long fixedDate = timeOfDay / ONE_DAY;2698timeOfDay %= ONE_DAY;2699while (timeOfDay < 0) {2700timeOfDay += ONE_DAY;2701--fixedDate;2702}27032704// Calculate the fixed date since January 1, 1 (Gregorian).2705calculateFixedDate: {2706long gfd, jfd;2707if (year > gregorianCutoverYear && year > gregorianCutoverYearJulian) {2708gfd = fixedDate + getFixedDate(gcal, year, fieldMask);2709if (gfd >= gregorianCutoverDate) {2710fixedDate = gfd;2711break calculateFixedDate;2712}2713jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2714} else if (year < gregorianCutoverYear && year < gregorianCutoverYearJulian) {2715jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2716if (jfd < gregorianCutoverDate) {2717fixedDate = jfd;2718break calculateFixedDate;2719}2720gfd = jfd;2721} else {2722jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2723gfd = fixedDate + getFixedDate(gcal, year, fieldMask);2724}27252726// Now we have to determine which calendar date it is.27272728// If the date is relative from the beginning of the year2729// in the Julian calendar, then use jfd;2730if (isFieldSet(fieldMask, DAY_OF_YEAR) || isFieldSet(fieldMask, WEEK_OF_YEAR)) {2731if (gregorianCutoverYear == gregorianCutoverYearJulian) {2732fixedDate = jfd;2733break calculateFixedDate;2734} else if (year == gregorianCutoverYear) {2735fixedDate = gfd;2736break calculateFixedDate;2737}2738}27392740if (gfd >= gregorianCutoverDate) {2741if (jfd >= gregorianCutoverDate) {2742fixedDate = gfd;2743} else {2744// The date is in an "overlapping" period. No way2745// to disambiguate it. Determine it using the2746// previous date calculation.2747if (calsys == gcal || calsys == null) {2748fixedDate = gfd;2749} else {2750fixedDate = jfd;2751}2752}2753} else {2754if (jfd < gregorianCutoverDate) {2755fixedDate = jfd;2756} else {2757// The date is in a "missing" period.2758if (!isLenient()) {2759throw new IllegalArgumentException("the specified date doesn't exist");2760}2761// Take the Julian date for compatibility, which2762// will produce a Gregorian date.2763fixedDate = jfd;2764}2765}2766}27672768// millis represents local wall-clock time in milliseconds.2769long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;27702771// Compute the time zone offset and DST offset. There are two potential2772// ambiguities here. We'll assume a 2:00 am (wall time) switchover time2773// for discussion purposes here.2774// 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am2775// can be in standard or in DST depending. However, 2:00 am is an invalid2776// representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).2777// We assume standard time.2778// 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am2779// can be in standard or DST. Both are valid representations (the rep2780// jumps from 1:59:59 DST to 1:00:00 Std).2781// Again, we assume standard time.2782// We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET2783// or DST_OFFSET fields; then we use those fields.2784TimeZone zone = getZone();2785if (zoneOffsets == null) {2786zoneOffsets = new int[2];2787}2788int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);2789if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {2790if (zone instanceof ZoneInfo) {2791((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets);2792} else {2793int gmtOffset = isFieldSet(fieldMask, ZONE_OFFSET) ?2794internalGet(ZONE_OFFSET) : zone.getRawOffset();2795zone.getOffsets(millis - gmtOffset, zoneOffsets);2796}2797}2798if (tzMask != 0) {2799if (isFieldSet(tzMask, ZONE_OFFSET)) {2800zoneOffsets[0] = internalGet(ZONE_OFFSET);2801}2802if (isFieldSet(tzMask, DST_OFFSET)) {2803zoneOffsets[1] = internalGet(DST_OFFSET);2804}2805}28062807// Adjust the time zone offset values to get the UTC time.2808millis -= zoneOffsets[0] + zoneOffsets[1];28092810// Set this calendar's time in milliseconds2811time = millis;28122813int mask = computeFields(fieldMask | getSetStateFields(), tzMask);28142815if (!isLenient()) {2816for (int field = 0; field < FIELD_COUNT; field++) {2817if (!isExternallySet(field)) {2818continue;2819}2820if (originalFields[field] != internalGet(field)) {2821String s = originalFields[field] + " -> " + internalGet(field);2822// Restore the original field values2823System.arraycopy(originalFields, 0, fields, 0, fields.length);2824throw new IllegalArgumentException(getFieldName(field) + ": " + s);2825}2826}2827}2828setFieldsNormalized(mask);2829}28302831/**2832* Computes the fixed date under either the Gregorian or the2833* Julian calendar, using the given year and the specified calendar fields.2834*2835* @param cal the CalendarSystem to be used for the date calculation2836* @param year the normalized year number, with 0 indicating the2837* year 1 BCE, -1 indicating 2 BCE, etc.2838* @param fieldMask the calendar fields to be used for the date calculation2839* @return the fixed date2840* @see Calendar#selectFields2841*/2842private long getFixedDate(BaseCalendar cal, int year, int fieldMask) {2843int month = JANUARY;2844if (isFieldSet(fieldMask, MONTH)) {2845// No need to check if MONTH has been set (no isSet(MONTH)2846// call) since its unset value happens to be JANUARY (0).2847month = internalGet(MONTH);28482849// If the month is out of range, adjust it into range2850if (month > DECEMBER) {2851year += month / 12;2852month %= 12;2853} else if (month < JANUARY) {2854int[] rem = new int[1];2855year += CalendarUtils.floorDivide(month, 12, rem);2856month = rem[0];2857}2858}28592860// Get the fixed date since Jan 1, 1 (Gregorian). We are on2861// the first day of either `month' or January in 'year'.2862long fixedDate = cal.getFixedDate(year, month + 1, 1,2863cal == gcal ? gdate : null);2864if (isFieldSet(fieldMask, MONTH)) {2865// Month-based calculations2866if (isFieldSet(fieldMask, DAY_OF_MONTH)) {2867// We are on the first day of the month. Just add the2868// offset if DAY_OF_MONTH is set. If the isSet call2869// returns false, that means DAY_OF_MONTH has been2870// selected just because of the selected2871// combination. We don't need to add any since the2872// default value is the 1st.2873if (isSet(DAY_OF_MONTH)) {2874// To avoid underflow with DAY_OF_MONTH-1, add2875// DAY_OF_MONTH, then subtract 1.2876fixedDate += internalGet(DAY_OF_MONTH);2877fixedDate--;2878}2879} else {2880if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {2881long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,2882getFirstDayOfWeek());2883// If we have enough days in the first week, then2884// move to the previous week.2885if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {2886firstDayOfWeek -= 7;2887}2888if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2889firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,2890internalGet(DAY_OF_WEEK));2891}2892// In lenient mode, we treat days of the previous2893// months as a part of the specified2894// WEEK_OF_MONTH. See 4633646.2895fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);2896} else {2897int dayOfWeek;2898if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2899dayOfWeek = internalGet(DAY_OF_WEEK);2900} else {2901dayOfWeek = getFirstDayOfWeek();2902}2903// We are basing this on the day-of-week-in-month. The only2904// trickiness occurs if the day-of-week-in-month is2905// negative.2906int dowim;2907if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {2908dowim = internalGet(DAY_OF_WEEK_IN_MONTH);2909} else {2910dowim = 1;2911}2912if (dowim >= 0) {2913fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,2914dayOfWeek);2915} else {2916// Go to the first day of the next week of2917// the specified week boundary.2918int lastDate = monthLength(month, year) + (7 * (dowim + 1));2919// Then, get the day of week date on or before the last date.2920fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,2921dayOfWeek);2922}2923}2924}2925} else {2926if (year == gregorianCutoverYear && cal == gcal2927&& fixedDate < gregorianCutoverDate2928&& gregorianCutoverYear != gregorianCutoverYearJulian) {2929// January 1 of the year doesn't exist. Use2930// gregorianCutoverDate as the first day of the2931// year.2932fixedDate = gregorianCutoverDate;2933}2934// We are on the first day of the year.2935if (isFieldSet(fieldMask, DAY_OF_YEAR)) {2936// Add the offset, then subtract 1. (Make sure to avoid underflow.)2937fixedDate += internalGet(DAY_OF_YEAR);2938fixedDate--;2939} else {2940long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,2941getFirstDayOfWeek());2942// If we have enough days in the first week, then move2943// to the previous week.2944if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {2945firstDayOfWeek -= 7;2946}2947if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2948int dayOfWeek = internalGet(DAY_OF_WEEK);2949if (dayOfWeek != getFirstDayOfWeek()) {2950firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,2951dayOfWeek);2952}2953}2954fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);2955}2956}29572958return fixedDate;2959}29602961/**2962* Returns this object if it's normalized (all fields and time are2963* in sync). Otherwise, a cloned object is returned after calling2964* complete() in lenient mode.2965*/2966private GregorianCalendar getNormalizedCalendar() {2967GregorianCalendar gc;2968if (isFullyNormalized()) {2969gc = this;2970} else {2971// Create a clone and normalize the calendar fields2972gc = (GregorianCalendar) this.clone();2973gc.setLenient(true);2974gc.complete();2975}2976return gc;2977}29782979/**2980* Returns the Julian calendar system instance (singleton). 'jcal'2981* and 'jeras' are set upon the return.2982*/2983private static synchronized BaseCalendar getJulianCalendarSystem() {2984if (jcal == null) {2985jcal = (JulianCalendar) CalendarSystem.forName("julian");2986jeras = jcal.getEras();2987}2988return jcal;2989}29902991/**2992* Returns the calendar system for dates before the cutover date2993* in the cutover year. If the cutover date is January 1, the2994* method returns Gregorian. Otherwise, Julian.2995*/2996private BaseCalendar getCutoverCalendarSystem() {2997if (gregorianCutoverYearJulian < gregorianCutoverYear) {2998return gcal;2999}3000return getJulianCalendarSystem();3001}30023003/**3004* Determines if the specified year (normalized) is the Gregorian3005* cutover year. This object must have been normalized.3006*/3007private boolean isCutoverYear(int normalizedYear) {3008int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;3009return normalizedYear == cutoverYear;3010}30113012/**3013* Returns the fixed date of the first day of the year (usually3014* January 1) before the specified date.3015*3016* @param date the date for which the first day of the year is3017* calculated. The date has to be in the cut-over year (Gregorian3018* or Julian).3019* @param fixedDate the fixed date representation of the date3020*/3021private long getFixedDateJan1(BaseCalendar.Date date, long fixedDate) {3022assert date.getNormalizedYear() == gregorianCutoverYear ||3023date.getNormalizedYear() == gregorianCutoverYearJulian;3024if (gregorianCutoverYear != gregorianCutoverYearJulian) {3025if (fixedDate >= gregorianCutoverDate) {3026// Dates before the cutover date don't exist3027// in the same (Gregorian) year. So, no3028// January 1 exists in the year. Use the3029// cutover date as the first day of the year.3030return gregorianCutoverDate;3031}3032}3033// January 1 of the normalized year should exist.3034BaseCalendar juliancal = getJulianCalendarSystem();3035return juliancal.getFixedDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1, null);3036}30373038/**3039* Returns the fixed date of the first date of the month (usually3040* the 1st of the month) before the specified date.3041*3042* @param date the date for which the first day of the month is3043* calculated. The date has to be in the cut-over year (Gregorian3044* or Julian).3045* @param fixedDate the fixed date representation of the date3046*/3047private long getFixedDateMonth1(BaseCalendar.Date date, long fixedDate) {3048assert date.getNormalizedYear() == gregorianCutoverYear ||3049date.getNormalizedYear() == gregorianCutoverYearJulian;3050BaseCalendar.Date gCutover = getGregorianCutoverDate();3051if (gCutover.getMonth() == BaseCalendar.JANUARY3052&& gCutover.getDayOfMonth() == 1) {3053// The cutover happened on January 1.3054return fixedDate - date.getDayOfMonth() + 1;3055}30563057long fixedDateMonth1;3058// The cutover happened sometime during the year.3059if (date.getMonth() == gCutover.getMonth()) {3060// The cutover happened in the month.3061BaseCalendar.Date jLastDate = getLastJulianDate();3062if (gregorianCutoverYear == gregorianCutoverYearJulian3063&& gCutover.getMonth() == jLastDate.getMonth()) {3064// The "gap" fits in the same month.3065fixedDateMonth1 = jcal.getFixedDate(date.getNormalizedYear(),3066date.getMonth(),30671,3068null);3069} else {3070// Use the cutover date as the first day of the month.3071fixedDateMonth1 = gregorianCutoverDate;3072}3073} else {3074// The cutover happened before the month.3075fixedDateMonth1 = fixedDate - date.getDayOfMonth() + 1;3076}30773078return fixedDateMonth1;3079}30803081/**3082* Returns a CalendarDate produced from the specified fixed date.3083*3084* @param fd the fixed date3085*/3086private BaseCalendar.Date getCalendarDate(long fd) {3087BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();3088BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);3089cal.getCalendarDateFromFixedDate(d, fd);3090return d;3091}30923093/**3094* Returns the Gregorian cutover date as a BaseCalendar.Date. The3095* date is a Gregorian date.3096*/3097private BaseCalendar.Date getGregorianCutoverDate() {3098return getCalendarDate(gregorianCutoverDate);3099}31003101/**3102* Returns the day before the Gregorian cutover date as a3103* BaseCalendar.Date. The date is a Julian date.3104*/3105private BaseCalendar.Date getLastJulianDate() {3106return getCalendarDate(gregorianCutoverDate - 1);3107}31083109/**3110* Returns the length of the specified month in the specified3111* year. The year number must be normalized.3112*3113* @see #isLeapYear(int)3114*/3115private int monthLength(int month, int year) {3116return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];3117}31183119/**3120* Returns the length of the specified month in the year provided3121* by internalGet(YEAR).3122*3123* @see #isLeapYear(int)3124*/3125private int monthLength(int month) {3126int year = internalGet(YEAR);3127if (internalGetEra() == BCE) {3128year = 1 - year;3129}3130return monthLength(month, year);3131}31323133private int actualMonthLength() {3134int year = cdate.getNormalizedYear();3135if (year != gregorianCutoverYear && year != gregorianCutoverYearJulian) {3136return calsys.getMonthLength(cdate);3137}3138BaseCalendar.Date date = (BaseCalendar.Date) cdate.clone();3139long fd = calsys.getFixedDate(date);3140long month1 = getFixedDateMonth1(date, fd);3141long next1 = month1 + calsys.getMonthLength(date);3142if (next1 < gregorianCutoverDate) {3143return (int)(next1 - month1);3144}3145if (cdate != gdate) {3146date = (BaseCalendar.Date) gcal.newCalendarDate(TimeZone.NO_TIMEZONE);3147}3148gcal.getCalendarDateFromFixedDate(date, next1);3149next1 = getFixedDateMonth1(date, next1);3150return (int)(next1 - month1);3151}31523153/**3154* Returns the length (in days) of the specified year. The year3155* must be normalized.3156*/3157private int yearLength(int year) {3158return isLeapYear(year) ? 366 : 365;3159}31603161/**3162* Returns the length (in days) of the year provided by3163* internalGet(YEAR).3164*/3165private int yearLength() {3166int year = internalGet(YEAR);3167if (internalGetEra() == BCE) {3168year = 1 - year;3169}3170return yearLength(year);3171}31723173/**3174* After adjustments such as add(MONTH), add(YEAR), we don't want the3175* month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar3176* 3, we want it to go to Feb 28. Adjustments which might run into this3177* problem call this method to retain the proper month.3178*/3179private void pinDayOfMonth() {3180int year = internalGet(YEAR);3181int monthLen;3182if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {3183monthLen = monthLength(internalGet(MONTH));3184} else {3185GregorianCalendar gc = getNormalizedCalendar();3186monthLen = gc.getActualMaximum(DAY_OF_MONTH);3187}3188int dom = internalGet(DAY_OF_MONTH);3189if (dom > monthLen) {3190set(DAY_OF_MONTH, monthLen);3191}3192}31933194/**3195* Returns the fixed date value of this object. The time value and3196* calendar fields must be in synch.3197*/3198private long getCurrentFixedDate() {3199return (calsys == gcal) ? cachedFixedDate : calsys.getFixedDate(cdate);3200}32013202/**3203* Returns the new value after 'roll'ing the specified value and amount.3204*/3205private static int getRolledValue(int value, int amount, int min, int max) {3206assert value >= min && value <= max;3207int range = max - min + 1;3208amount %= range;3209int n = value + amount;3210if (n > max) {3211n -= range;3212} else if (n < min) {3213n += range;3214}3215assert n >= min && n <= max;3216return n;3217}32183219/**3220* Returns the ERA. We need a special method for this because the3221* default ERA is CE, but a zero (unset) ERA is BCE.3222*/3223private int internalGetEra() {3224return isSet(ERA) ? internalGet(ERA) : CE;3225}32263227/**3228* Updates internal state.3229*/3230private void readObject(ObjectInputStream stream)3231throws IOException, ClassNotFoundException {3232stream.defaultReadObject();3233if (gdate == null) {3234gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());3235cachedFixedDate = Long.MIN_VALUE;3236}3237setGregorianChange(gregorianCutover);3238}32393240/**3241* Converts this object to a {@code ZonedDateTime} that represents3242* the same point on the time-line as this {@code GregorianCalendar}.3243* <p>3244* Since this object supports a Julian-Gregorian cutover date and3245* {@code ZonedDateTime} does not, it is possible that the resulting year,3246* month and day will have different values. The result will represent the3247* correct date in the ISO calendar system, which will also be the same value3248* for Modified Julian Days.3249*3250* @return a zoned date-time representing the same point on the time-line3251* as this gregorian calendar3252* @since 1.83253*/3254public ZonedDateTime toZonedDateTime() {3255return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getTimeInMillis()),3256getTimeZone().toZoneId());3257}32583259/**3260* Obtains an instance of {@code GregorianCalendar} with the default locale3261* from a {@code ZonedDateTime} object.3262* <p>3263* Since {@code ZonedDateTime} does not support a Julian-Gregorian cutover3264* date and uses ISO calendar system, the return GregorianCalendar is a pure3265* Gregorian calendar and uses ISO 8601 standard for week definitions,3266* which has {@code MONDAY} as the {@link Calendar#getFirstDayOfWeek()3267* FirstDayOfWeek} and {@code 4} as the value of the3268* {@link Calendar#getMinimalDaysInFirstWeek() MinimalDaysInFirstWeek}.3269* <p>3270* {@code ZoneDateTime} can store points on the time-line further in the3271* future and further in the past than {@code GregorianCalendar}. In this3272* scenario, this method will throw an {@code IllegalArgumentException}3273* exception.3274*3275* @param zdt the zoned date-time object to convert3276* @return the gregorian calendar representing the same point on the3277* time-line as the zoned date-time provided3278* @exception NullPointerException if {@code zdt} is null3279* @exception IllegalArgumentException if the zoned date-time is too3280* large to represent as a {@code GregorianCalendar}3281* @since 1.83282*/3283public static GregorianCalendar from(ZonedDateTime zdt) {3284GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zdt.getZone()));3285cal.setGregorianChange(new Date(Long.MIN_VALUE));3286cal.setFirstDayOfWeek(MONDAY);3287cal.setMinimalDaysInFirstWeek(4);3288try {3289cal.setTimeInMillis(Math.addExact(Math.multiplyExact(zdt.toEpochSecond(), 1000),3290zdt.get(ChronoField.MILLI_OF_SECOND)));3291} catch (ArithmeticException ex) {3292throw new IllegalArgumentException(ex);3293}3294return cal;3295}3296}329732983299