Path: blob/master/src/java.base/share/classes/java/util/GregorianCalendar.java
41152 views
/*1* Copyright (c) 1996, 2020, 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} is a concrete subclass of56* {@code Calendar} and provides the standard calendar system57* used by most of the world.58*59* <p> {@code GregorianCalendar} 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}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} 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} may be used for all years to generate81* meaningful and consistent results. However, dates obtained using82* {@code GregorianCalendar} 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* <h2><a id="week_and_year">Week Of Year and Week Year</a></h2>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 id="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 id="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* <h3>Week Of Month</h3>136*137* <p>Values calculated for the {@code WEEK_OF_MONTH} 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()} contiguous days in that month,141* ending on the day before {@code getFirstDayOfWeek()}. Unlike142* week 1 of a year, week 1 of a month may be shorter than 7 days, need143* not start on {@code getFirstDayOfWeek()}, and will not include days of144* the previous month. Days of a month before week 1 have a145* {@code WEEK_OF_MONTH} of 0.146*147* <p>For example, if {@code getFirstDayOfWeek()} is {@code SUNDAY}148* and {@code getMinimalDaysInFirstWeek()} 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} of 1. Thursday, January 1 through151* Saturday, January 3 have a {@code WEEK_OF_MONTH} of 0. If152* {@code getMinimalDaysInFirstWeek()} is changed to 3, then January 1153* through January 3 have a {@code WEEK_OF_MONTH} of 1.154*155* <h3>Default Fields Values</h3>156*157* <p>The {@code clear} method sets calendar field(s)158* undefined. {@code GregorianCalendar} uses the following159* default value for each calendar field if its value is undefined.160*161* <table class="striped" style="text-align: left; width: 66%;">162* <caption style="display:none">GregorianCalendar default field values</caption>163* <thead>164* <tr>165* <th scope="col">166* Field167* </th>168* <th scope="col">169* Default Value170* </th>171* </tr>172* </thead>173* <tbody>174* <tr>175* <th scope="row">176* {@code ERA}177* </th>178* <td>179* {@code AD}180* </td>181* </tr>182* <tr>183* <th scope="row">184* {@code YEAR}185* </th>186* <td>187* {@code 1970}188* </td>189* </tr>190* <tr>191* <th scope="row">192* {@code MONTH}193* </th>194* <td>195* {@code JANUARY}196* </td>197* </tr>198* <tr>199* <th scope="row">200* {@code DAY_OF_MONTH}201* </th>202* <td>203* {@code 1}204* </td>205* </tr>206* <tr>207* <th scope="row">208* {@code DAY_OF_WEEK}209* </th>210* <td>211* {@code the first day of week}212* </td>213* </tr>214* <tr>215* <th scope="row">216* {@code WEEK_OF_MONTH}217* </th>218* <td>219* {@code 0}220* </td>221* </tr>222* <tr>223* <th scope="row">224* {@code DAY_OF_WEEK_IN_MONTH}225* </th>226* <td>227* {@code 1}228* </td>229* </tr>230* <tr>231* <th scope="row">232* {@code AM_PM}233* </th>234* <td>235* {@code AM}236* </td>237* </tr>238* <tr>239* <th scope="row">240* {@code HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND}241* </th>242* <td>243* {@code 0}244* </td>245* </tr>246* </tbody>247* </table>248* <br>Default values are not applicable for the fields not listed above.249*250* <p>251* <strong>Example:</strong>252* <blockquote>253* <pre>254* // get the supported ids for GMT-08:00 (Pacific Standard Time)255* String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);256* // if no ids were returned, something is wrong. get out.257* if (ids.length == 0)258* System.exit(0);259*260* // begin output261* System.out.println("Current Time");262*263* // create a Pacific Standard Time time zone264* SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);265*266* // set up rules for Daylight Saving Time267* pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);268* pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);269*270* // create a GregorianCalendar with the Pacific Daylight time zone271* // and the current date and time272* Calendar calendar = new GregorianCalendar(pdt);273* Date trialTime = new Date();274* calendar.setTime(trialTime);275*276* // print out a bunch of interesting things277* System.out.println("ERA: " + calendar.get(Calendar.ERA));278* System.out.println("YEAR: " + calendar.get(Calendar.YEAR));279* System.out.println("MONTH: " + calendar.get(Calendar.MONTH));280* System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));281* System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));282* System.out.println("DATE: " + calendar.get(Calendar.DATE));283* System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));284* System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));285* System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));286* System.out.println("DAY_OF_WEEK_IN_MONTH: "287* + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));288* System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));289* System.out.println("HOUR: " + calendar.get(Calendar.HOUR));290* System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));291* System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));292* System.out.println("SECOND: " + calendar.get(Calendar.SECOND));293* System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));294* System.out.println("ZONE_OFFSET: "295* + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));296* System.out.println("DST_OFFSET: "297* + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));298* 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 1.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} 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} to {@code AD} 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} field indicating380* the common era (Anno Domini), also known as CE.381* The sequence of years at the transition from {@code BC} to {@code AD} 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")498@java.io.Serial499static final long serialVersionUID = -8125100834729963327L;500501// Reference to the sun.util.calendar.Gregorian instance (singleton).502private static final Gregorian gcal =503CalendarSystem.getGregorianCalendar();504505// Reference to the JulianCalendar instance (singleton), set as needed. See506// getJulianCalendarSystem().507private static JulianCalendar jcal;508509// JulianCalendar eras. See getJulianCalendarSystem().510private static Era[] jeras;511512// The default value of gregorianCutover.513static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;514515/////////////////////516// Instance Variables517/////////////////////518519/**520* The point at which the Gregorian calendar rules are used, measured in521* milliseconds from the standard epoch. Default is October 15, 1582522* (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,523* 1582 (Julian) is followed by October 15, 1582 (Gregorian). This524* corresponds to Julian day number 2299161.525* @serial526*/527private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;528529/**530* The fixed date of the gregorianCutover.531*/532private transient long gregorianCutoverDate =533(((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736534535/**536* The normalized year of the gregorianCutover in Gregorian, with537* 0 representing 1 BCE, -1 representing 2 BCE, etc.538*/539private transient int gregorianCutoverYear = 1582;540541/**542* The normalized year of the gregorianCutover in Julian, with 0543* representing 1 BCE, -1 representing 2 BCE, etc.544*/545private transient int gregorianCutoverYearJulian = 1582;546547/**548* gdate always has a sun.util.calendar.Gregorian.Date instance to549* avoid overhead of creating it. The assumption is that most550* applications will need only Gregorian calendar calculations.551*/552private transient BaseCalendar.Date gdate;553554/**555* Reference to either gdate or a JulianCalendar.Date556* instance. After calling complete(), this value is guaranteed to557* be set.558*/559private transient BaseCalendar.Date cdate;560561/**562* The CalendarSystem used to calculate the date in cdate. After563* calling complete(), this value is guaranteed to be set and564* consistent with the cdate value.565*/566private transient BaseCalendar calsys;567568/**569* Temporary int[2] to get time zone offsets. zoneOffsets[0] gets570* the GMT offset value and zoneOffsets[1] gets the DST saving571* value.572*/573private transient int[] zoneOffsets;574575/**576* Temporary storage for saving original fields[] values in577* non-lenient mode.578*/579private transient int[] originalFields;580581///////////////582// Constructors583///////////////584585/**586* Constructs a default {@code GregorianCalendar} using the current time587* in the default time zone with the default588* {@link Locale.Category#FORMAT FORMAT} locale.589*/590public GregorianCalendar() {591this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));592setZoneShared(true);593}594595/**596* Constructs a {@code GregorianCalendar} based on the current time597* in the given time zone with the default598* {@link Locale.Category#FORMAT FORMAT} locale.599*600* @param zone the given time zone.601*/602public GregorianCalendar(TimeZone zone) {603this(zone, Locale.getDefault(Locale.Category.FORMAT));604}605606/**607* Constructs a {@code GregorianCalendar} based on the current time608* in the default time zone with the given locale.609*610* @param aLocale the given locale.611*/612public GregorianCalendar(Locale aLocale) {613this(TimeZone.getDefaultRef(), aLocale);614setZoneShared(true);615}616617/**618* Constructs a {@code GregorianCalendar} based on the current time619* in the given time zone with the given locale.620*621* @param zone the given time zone.622* @param aLocale the given locale.623*/624public GregorianCalendar(TimeZone zone, Locale aLocale) {625super(zone, aLocale);626gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);627setTimeInMillis(System.currentTimeMillis());628}629630/**631* Constructs a {@code GregorianCalendar} with the given date set632* in the default time zone with the default locale.633*634* @param year the value used to set the {@code YEAR} calendar field in the calendar.635* @param month the value used to set the {@code MONTH} calendar field in the calendar.636* Month value is 0-based. e.g., 0 for January.637* @param dayOfMonth the value used to set the {@code DAY_OF_MONTH} calendar field in the calendar.638*/639public GregorianCalendar(int year, int month, int dayOfMonth) {640this(year, month, dayOfMonth, 0, 0, 0, 0);641}642643/**644* Constructs a {@code GregorianCalendar} with the given date645* and time set for the default time zone with the default locale.646*647* @param year the value used to set the {@code YEAR} calendar field in the calendar.648* @param month the value used to set the {@code MONTH} calendar field in the calendar.649* Month value is 0-based. e.g., 0 for January.650* @param dayOfMonth the value used to set the {@code DAY_OF_MONTH} calendar field in the calendar.651* @param hourOfDay the value used to set the {@code HOUR_OF_DAY} calendar field652* in the calendar.653* @param minute the value used to set the {@code MINUTE} calendar field654* in the calendar.655*/656public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,657int minute) {658this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);659}660661/**662* Constructs a GregorianCalendar with the given date663* and time set for the default time zone with the default locale.664*665* @param year the value used to set the {@code YEAR} calendar field in the calendar.666* @param month the value used to set the {@code MONTH} calendar field in the calendar.667* Month value is 0-based. e.g., 0 for January.668* @param dayOfMonth the value used to set the {@code DAY_OF_MONTH} calendar field in the calendar.669* @param hourOfDay the value used to set the {@code HOUR_OF_DAY} calendar field670* in the calendar.671* @param minute the value used to set the {@code MINUTE} calendar field672* in the calendar.673* @param second the value used to set the {@code SECOND} calendar field674* in the calendar.675*/676public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,677int minute, int second) {678this(year, month, dayOfMonth, hourOfDay, minute, second, 0);679}680681/**682* Constructs a {@code GregorianCalendar} with the given date683* and time set for the default time zone with the default locale.684*685* @param year the value used to set the {@code YEAR} calendar field in the calendar.686* @param month the value used to set the {@code MONTH} calendar field in the calendar.687* Month value is 0-based. e.g., 0 for January.688* @param dayOfMonth the value used to set the {@code DAY_OF_MONTH} calendar field in the calendar.689* @param hourOfDay the value used to set the {@code HOUR_OF_DAY} calendar field690* in the calendar.691* @param minute the value used to set the {@code MINUTE} calendar field692* in the calendar.693* @param second the value used to set the {@code SECOND} calendar field694* in the calendar.695* @param millis the value used to set the {@code MILLISECOND} calendar field696*/697GregorianCalendar(int year, int month, int dayOfMonth,698int hourOfDay, int minute, int second, int millis) {699super();700gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());701this.set(YEAR, year);702this.set(MONTH, month);703this.set(DAY_OF_MONTH, dayOfMonth);704705// Set AM_PM and HOUR here to set their stamp values before706// setting HOUR_OF_DAY (6178071).707if (hourOfDay >= 12 && hourOfDay <= 23) {708// If hourOfDay is a valid PM hour, set the correct PM values709// so that it won't throw an exception in case it's set to710// non-lenient later.711this.internalSet(AM_PM, PM);712this.internalSet(HOUR, hourOfDay - 12);713} else {714// The default value for AM_PM is AM.715// We don't care any out of range value here for leniency.716this.internalSet(HOUR, hourOfDay);717}718// The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)719setFieldsComputed(HOUR_MASK|AM_PM_MASK);720721this.set(HOUR_OF_DAY, hourOfDay);722this.set(MINUTE, minute);723this.set(SECOND, second);724// should be changed to set() when this constructor is made725// public.726this.internalSet(MILLISECOND, millis);727}728729/**730* Constructs an empty GregorianCalendar.731*732* @param zone the given time zone733* @param locale the given locale734* @param flag the flag requesting an empty instance735*/736GregorianCalendar(TimeZone zone, Locale locale, boolean flag) {737super(zone, locale);738gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());739}740741/////////////////742// Public methods743/////////////////744745/**746* Sets the {@code GregorianCalendar} change date. This is the point when the switch747* from Julian dates to Gregorian dates occurred. Default is October 15,748* 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.749* <p>750* To obtain a pure Julian calendar, set the change date to751* {@code Date(Long.MAX_VALUE)}. To obtain a pure Gregorian calendar,752* set the change date to {@code Date(Long.MIN_VALUE)}.753*754* @param date the given Gregorian cutover date.755*/756public void setGregorianChange(Date date) {757long cutoverTime = date.getTime();758if (cutoverTime == gregorianCutover) {759return;760}761// Before changing the cutover date, make sure to have the762// time of this calendar.763complete();764setGregorianChange(cutoverTime);765}766767private void setGregorianChange(long cutoverTime) {768gregorianCutover = cutoverTime;769gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)770+ EPOCH_OFFSET;771772// To provide the "pure" Julian calendar as advertised.773// Strictly speaking, the last millisecond should be a774// Gregorian date. However, the API doc specifies that setting775// the cutover date to Long.MAX_VALUE will make this calendar776// a pure Julian calendar. (See 4167995)777if (cutoverTime == Long.MAX_VALUE) {778gregorianCutoverDate++;779}780781BaseCalendar.Date d = getGregorianCutoverDate();782783// Set the cutover year (in the Gregorian year numbering)784gregorianCutoverYear = d.getYear();785786BaseCalendar julianCal = getJulianCalendarSystem();787d = (BaseCalendar.Date) julianCal.newCalendarDate(TimeZone.NO_TIMEZONE);788julianCal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);789gregorianCutoverYearJulian = d.getNormalizedYear();790791if (time < gregorianCutover) {792// The field values are no longer valid under the new793// cutover date.794setUnnormalized();795}796}797798/**799* Gets the Gregorian Calendar change date. This is the point when the800* switch from Julian dates to Gregorian dates occurred. Default is801* October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian802* calendar.803*804* @return the Gregorian cutover date for this {@code GregorianCalendar} object.805*/806public final Date getGregorianChange() {807return new Date(gregorianCutover);808}809810/**811* Determines if the given year is a leap year. Returns {@code true} if812* the given year is a leap year. To specify BC year numbers,813* {@code 1 - year number} must be given. For example, year BC 4 is814* specified as -3.815*816* @param year the given year.817* @return {@code true} if the given year is a leap year; {@code false} otherwise.818*/819public boolean isLeapYear(int year) {820if ((year & 3) != 0) {821return false;822}823824if (year > gregorianCutoverYear) {825return (year%100 != 0) || (year%400 == 0); // Gregorian826}827if (year < gregorianCutoverYearJulian) {828return true; // Julian829}830boolean gregorian;831// If the given year is the Gregorian cutover year, we need to832// determine which calendar system to be applied to February in the year.833if (gregorianCutoverYear == gregorianCutoverYearJulian) {834BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian835gregorian = d.getMonth() < BaseCalendar.MARCH;836} else {837gregorian = year == gregorianCutoverYear;838}839return gregorian ? (year%100 != 0) || (year%400 == 0) : true;840}841842/**843* Returns {@code "gregory"} as the calendar type.844*845* @return {@code "gregory"}846* @since 1.8847*/848@Override849public String getCalendarType() {850return "gregory";851}852853/**854* Compares this {@code GregorianCalendar} to the specified855* {@code Object}. The result is {@code true} if and856* only if the argument is a {@code GregorianCalendar} object857* that represents the same time value (millisecond offset from858* the <a href="Calendar.html#Epoch">Epoch</a>) under the same859* {@code Calendar} parameters and Gregorian change date as860* this object.861*862* @param obj the object to compare with.863* @return {@code true} if this object is equal to {@code obj};864* {@code false} otherwise.865* @see Calendar#compareTo(Calendar)866*/867@Override868public boolean equals(Object obj) {869return obj instanceof GregorianCalendar &&870super.equals(obj) &&871gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;872}873874/**875* Generates the hash code for this {@code GregorianCalendar} object.876*/877@Override878public int hashCode() {879return super.hashCode() ^ (int)gregorianCutoverDate;880}881882/**883* Adds the specified (signed) amount of time to the given calendar field,884* based on the calendar's rules.885*886* <p><em>Add rule 1</em>. The value of {@code field}887* after the call minus the value of {@code field} before the888* call is {@code amount}, modulo any overflow that has occurred in889* {@code field}. Overflow occurs when a field value exceeds its890* range and, as a result, the next larger field is incremented or891* decremented and the field value is adjusted back into its range.</p>892*893* <p><em>Add rule 2</em>. If a smaller field is expected to be894* invariant, but it is impossible for it to be equal to its895* prior value because of changes in its minimum or maximum after896* {@code field} is changed, then its value is adjusted to be as close897* as possible to its expected value. A smaller field represents a898* smaller unit of time. {@code HOUR} is a smaller field than899* {@code DAY_OF_MONTH}. No adjustment is made to smaller fields900* that are not expected to be invariant. The calendar system901* determines what fields are expected to be invariant.</p>902*903* @param field the calendar field.904* @param amount the amount of date or time to be added to the field.905* @throws IllegalArgumentException if {@code field} is906* {@code ZONE_OFFSET}, {@code DST_OFFSET}, or unknown,907* or if any calendar fields have out-of-range values in908* non-lenient mode.909*/910@Override911public void add(int field, int amount) {912// If amount == 0, do nothing even the given field is out of913// range. This is tested by JCK.914if (amount == 0) {915return; // Do nothing!916}917918if (field < 0 || field >= ZONE_OFFSET) {919throw new IllegalArgumentException();920}921922// Sync the time and calendar fields.923complete();924925if (field == YEAR) {926int year = internalGet(YEAR);927if (internalGetEra() == CE) {928year += amount;929if (year > 0) {930set(YEAR, year);931} else { // year <= 0932set(YEAR, 1 - year);933// if year == 0, you get 1 BCE.934set(ERA, BCE);935}936}937else { // era == BCE938year -= amount;939if (year > 0) {940set(YEAR, year);941} else { // year <= 0942set(YEAR, 1 - year);943// if year == 0, you get 1 CE944set(ERA, CE);945}946}947pinDayOfMonth();948} else if (field == MONTH) {949int month = internalGet(MONTH) + amount;950int year = internalGet(YEAR);951int y_amount;952953if (month >= 0) {954y_amount = month/12;955} else {956y_amount = (month+1)/12 - 1;957}958if (y_amount != 0) {959if (internalGetEra() == CE) {960year += y_amount;961if (year > 0) {962set(YEAR, year);963} else { // year <= 0964set(YEAR, 1 - year);965// if year == 0, you get 1 BCE966set(ERA, BCE);967}968}969else { // era == BCE970year -= y_amount;971if (year > 0) {972set(YEAR, year);973} else { // year <= 0974set(YEAR, 1 - year);975// if year == 0, you get 1 CE976set(ERA, CE);977}978}979}980981if (month >= 0) {982set(MONTH, month % 12);983} else {984// month < 0985month %= 12;986if (month < 0) {987month += 12;988}989set(MONTH, JANUARY + month);990}991pinDayOfMonth();992} else if (field == ERA) {993int era = internalGet(ERA) + amount;994if (era < 0) {995era = 0;996}997if (era > 1) {998era = 1;999}1000set(ERA, era);1001} else {1002long delta = amount;1003long timeOfDay = 0;1004switch (field) {1005// Handle the time fields here. Convert the given1006// amount to milliseconds and call setTimeInMillis.1007case HOUR:1008case HOUR_OF_DAY:1009delta *= 60 * 60 * 1000; // hours to minutes1010break;10111012case MINUTE:1013delta *= 60 * 1000; // minutes to seconds1014break;10151016case SECOND:1017delta *= 1000; // seconds to milliseconds1018break;10191020case MILLISECOND:1021break;10221023// Handle week, day and AM_PM fields which involves1024// time zone offset change adjustment. Convert the1025// given amount to the number of days.1026case WEEK_OF_YEAR:1027case WEEK_OF_MONTH:1028case DAY_OF_WEEK_IN_MONTH:1029delta *= 7;1030break;10311032case DAY_OF_MONTH: // synonym of DATE1033case DAY_OF_YEAR:1034case DAY_OF_WEEK:1035break;10361037case AM_PM:1038// Convert the amount to the number of days (delta)1039// and +12 or -12 hours (timeOfDay).1040delta = amount / 2;1041timeOfDay = 12 * (amount % 2);1042break;1043}10441045// The time fields don't require time zone offset change1046// adjustment.1047if (field >= HOUR) {1048setTimeInMillis(time + delta);1049return;1050}10511052// The rest of the fields (week, day or AM_PM fields)1053// require time zone offset (both GMT and DST) change1054// adjustment.10551056// Translate the current time to the fixed date and time1057// of the day.1058long fd = getCurrentFixedDate();1059timeOfDay += internalGet(HOUR_OF_DAY);1060timeOfDay *= 60;1061timeOfDay += internalGet(MINUTE);1062timeOfDay *= 60;1063timeOfDay += internalGet(SECOND);1064timeOfDay *= 1000;1065timeOfDay += internalGet(MILLISECOND);1066if (timeOfDay >= ONE_DAY) {1067fd++;1068timeOfDay -= ONE_DAY;1069} else if (timeOfDay < 0) {1070fd--;1071timeOfDay += ONE_DAY;1072}10731074fd += delta; // fd is the expected fixed date after the calculation1075int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);1076setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);1077zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);1078// If the time zone offset has changed, then adjust the difference.1079if (zoneOffset != 0) {1080setTimeInMillis(time + zoneOffset);1081long fd2 = getCurrentFixedDate();1082// If the adjustment has changed the date, then take1083// the previous one.1084if (fd2 != fd) {1085setTimeInMillis(time - zoneOffset);1086}1087}1088}1089}10901091/**1092* Adds or subtracts (up/down) a single unit of time on the given time1093* field without changing larger fields.1094* <p>1095* <em>Example</em>: Consider a {@code GregorianCalendar}1096* originally set to December 31, 1999. Calling {@link #roll(int,boolean) roll(Calendar.MONTH, true)}1097* sets the calendar to January 31, 1999. The {@code YEAR} field is unchanged1098* because it is a larger field than {@code MONTH}.</p>1099*1100* @param up indicates if the value of the specified calendar field is to be1101* rolled up or rolled down. Use {@code true} if rolling up, {@code false} otherwise.1102* @throws IllegalArgumentException if {@code field} is1103* {@code ZONE_OFFSET}, {@code DST_OFFSET}, or unknown,1104* or if any calendar fields have out-of-range values in1105* non-lenient mode.1106* @see #add(int,int)1107* @see #set(int,int)1108*/1109@Override1110public void roll(int field, boolean up) {1111roll(field, up ? +1 : -1);1112}11131114/**1115* Adds a signed amount to the specified calendar field without changing larger fields.1116* A negative roll amount means to subtract from field without changing1117* larger fields. If the specified amount is 0, this method performs nothing.1118*1119* <p>This method calls {@link #complete()} before adding the1120* amount so that all the calendar fields are normalized. If there1121* is any calendar field having an out-of-range value in non-lenient mode, then an1122* {@code IllegalArgumentException} is thrown.1123*1124* <p>1125* <em>Example</em>: Consider a {@code GregorianCalendar}1126* originally set to August 31, 1999. Calling <code>roll(Calendar.MONTH,1127* 8)</code> sets the calendar to April 30, <strong>1999</strong>. Using a1128* {@code GregorianCalendar}, the {@code DAY_OF_MONTH} field cannot1129* be 31 in the month April. {@code DAY_OF_MONTH} is set to the closest possible1130* value, 30. The {@code YEAR} field maintains the value of 1999 because it1131* is a larger field than {@code MONTH}.1132* <p>1133* <em>Example</em>: Consider a {@code GregorianCalendar}1134* originally set to Sunday June 6, 1999. Calling1135* {@code roll(Calendar.WEEK_OF_MONTH, -1)} sets the calendar to1136* Tuesday June 1, 1999, whereas calling1137* {@code add(Calendar.WEEK_OF_MONTH, -1)} sets the calendar to1138* Sunday May 30, 1999. This is because the roll rule imposes an1139* additional constraint: The {@code MONTH} must not change when the1140* {@code WEEK_OF_MONTH} is rolled. Taken together with add rule 1,1141* the resultant date must be between Tuesday June 1 and Saturday June1142* 5. According to add rule 2, the {@code DAY_OF_WEEK}, an invariant1143* when changing the {@code WEEK_OF_MONTH}, is set to Tuesday, the1144* closest possible value to Sunday (where Sunday is the first day of the1145* week).</p>1146*1147* @param field the calendar field.1148* @param amount the signed amount to add to {@code field}.1149* @throws IllegalArgumentException if {@code field} is1150* {@code ZONE_OFFSET}, {@code DST_OFFSET}, or unknown,1151* or if any calendar fields have out-of-range values in1152* non-lenient mode.1153* @see #roll(int,boolean)1154* @see #add(int,int)1155* @see #set(int,int)1156* @since 1.21157*/1158@Override1159public void roll(int field, int amount) {1160// If amount == 0, do nothing even the given field is out of1161// range. This is tested by JCK.1162if (amount == 0) {1163return;1164}11651166if (field < 0 || field >= ZONE_OFFSET) {1167throw new IllegalArgumentException();1168}11691170// Sync the time and calendar fields.1171complete();11721173int min = getMinimum(field);1174int max = getMaximum(field);11751176switch (field) {1177case AM_PM:1178case ERA:1179case YEAR:1180case MINUTE:1181case SECOND:1182case MILLISECOND:1183// These fields are handled simply, since they have fixed minima1184// and maxima. The field DAY_OF_MONTH is almost as simple. Other1185// fields are complicated, since the range within they must roll1186// varies depending on the date.1187break;11881189case HOUR:1190case HOUR_OF_DAY:1191{1192int rolledValue = getRolledValue(internalGet(field), amount, min, max);1193int hourOfDay = rolledValue;1194if (field == HOUR && internalGet(AM_PM) == PM) {1195hourOfDay += 12;1196}11971198// Create the current date/time value to perform wall-clock-based1199// roll.1200CalendarDate d = calsys.getCalendarDate(time, getZone());1201d.setHours(hourOfDay);1202time = calsys.getTime(d);12031204// If we stay on the same wall-clock time, try the next or previous hour.1205if (internalGet(HOUR_OF_DAY) == d.getHours()) {1206hourOfDay = getRolledValue(rolledValue, amount > 0 ? +1 : -1, min, max);1207if (field == HOUR && internalGet(AM_PM) == PM) {1208hourOfDay += 12;1209}1210d.setHours(hourOfDay);1211time = calsys.getTime(d);1212}1213// Get the new hourOfDay value which might have changed due to a DST transition.1214hourOfDay = d.getHours();1215// Update the hour related fields1216internalSet(HOUR_OF_DAY, hourOfDay);1217internalSet(AM_PM, hourOfDay / 12);1218internalSet(HOUR, hourOfDay % 12);12191220// Time zone offset and/or daylight saving might have changed.1221int zoneOffset = d.getZoneOffset();1222int saving = d.getDaylightSaving();1223internalSet(ZONE_OFFSET, zoneOffset - saving);1224internalSet(DST_OFFSET, saving);1225return;1226}12271228case MONTH:1229// Rolling the month involves both pinning the final value to [0, 11]1230// and adjusting the DAY_OF_MONTH if necessary. We only adjust the1231// DAY_OF_MONTH if, after updating the MONTH field, it is illegal.1232// E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.1233{1234if (!isCutoverYear(cdate.getNormalizedYear())) {1235int mon = (internalGet(MONTH) + amount) % 12;1236if (mon < 0) {1237mon += 12;1238}1239set(MONTH, mon);12401241// Keep the day of month in the range. We don't want to spill over1242// into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->1243// mar3.1244int monthLen = monthLength(mon);1245if (internalGet(DAY_OF_MONTH) > monthLen) {1246set(DAY_OF_MONTH, monthLen);1247}1248} else {1249// We need to take care of different lengths in1250// year and month due to the cutover.1251int yearLength = getActualMaximum(MONTH) + 1;1252int mon = (internalGet(MONTH) + amount) % yearLength;1253if (mon < 0) {1254mon += yearLength;1255}1256set(MONTH, mon);1257int monthLen = getActualMaximum(DAY_OF_MONTH);1258if (internalGet(DAY_OF_MONTH) > monthLen) {1259set(DAY_OF_MONTH, monthLen);1260}1261}1262return;1263}12641265case WEEK_OF_YEAR:1266{1267int y = cdate.getNormalizedYear();1268max = getActualMaximum(WEEK_OF_YEAR);1269set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));1270int woy = internalGet(WEEK_OF_YEAR);1271int value = woy + amount;1272if (!isCutoverYear(y)) {1273int weekYear = getWeekYear();1274if (weekYear == y) {1275// If the new value is in between min and max1276// (exclusive), then we can use the value.1277if (value > min && value < max) {1278set(WEEK_OF_YEAR, value);1279return;1280}1281long fd = getCurrentFixedDate();1282// Make sure that the min week has the current DAY_OF_WEEK1283// in the calendar year1284long day1 = fd - (7 * (woy - min));1285if (calsys.getYearFromFixedDate(day1) != y) {1286min++;1287}12881289// Make sure the same thing for the max week1290fd += 7 * (max - internalGet(WEEK_OF_YEAR));1291if (calsys.getYearFromFixedDate(fd) != y) {1292max--;1293}1294} else {1295// When WEEK_OF_YEAR and YEAR are out of sync,1296// adjust woy and amount to stay in the calendar year.1297if (weekYear > y) {1298if (amount < 0) {1299amount++;1300}1301woy = max;1302} else {1303if (amount > 0) {1304amount -= woy - max;1305}1306woy = min;1307}1308}1309set(field, getRolledValue(woy, amount, min, max));1310return;1311}13121313// Handle cutover here.1314long fd = getCurrentFixedDate();1315BaseCalendar cal;1316if (gregorianCutoverYear == gregorianCutoverYearJulian) {1317cal = getCutoverCalendarSystem();1318} else if (y == gregorianCutoverYear) {1319cal = gcal;1320} else {1321cal = getJulianCalendarSystem();1322}1323long day1 = fd - (7 * (woy - min));1324// Make sure that the min week has the current DAY_OF_WEEK1325if (cal.getYearFromFixedDate(day1) != y) {1326min++;1327}13281329// Make sure the same thing for the max week1330fd += 7 * (max - woy);1331cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();1332if (cal.getYearFromFixedDate(fd) != y) {1333max--;1334}1335// value: the new WEEK_OF_YEAR which must be converted1336// to month and day of month.1337value = getRolledValue(woy, amount, min, max) - 1;1338BaseCalendar.Date d = getCalendarDate(day1 + value * 7);1339set(MONTH, d.getMonth() - 1);1340set(DAY_OF_MONTH, d.getDayOfMonth());1341return;1342}13431344case WEEK_OF_MONTH:1345{1346boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());1347// dow: relative day of week from first day of week1348int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();1349if (dow < 0) {1350dow += 7;1351}13521353long fd = getCurrentFixedDate();1354long month1; // fixed date of the first day (usually 1) of the month1355int monthLength; // actual month length1356if (isCutoverYear) {1357month1 = getFixedDateMonth1(cdate, fd);1358monthLength = actualMonthLength();1359} else {1360month1 = fd - internalGet(DAY_OF_MONTH) + 1;1361monthLength = calsys.getMonthLength(cdate);1362}13631364// the first day of week of the month.1365long monthDay1st = BaseCalendar.getDayOfWeekDateOnOrBefore(month1 + 6,1366getFirstDayOfWeek());1367// if the week has enough days to form a week, the1368// week starts from the previous month.1369if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {1370monthDay1st -= 7;1371}1372max = getActualMaximum(field);13731374// value: the new WEEK_OF_MONTH value1375int value = getRolledValue(internalGet(field), amount, 1, max) - 1;13761377// nfd: fixed date of the rolled date1378long nfd = monthDay1st + value * 7 + dow;13791380// Unlike WEEK_OF_YEAR, we need to change day of week if the1381// nfd is out of the month.1382if (nfd < month1) {1383nfd = month1;1384} else if (nfd >= (month1 + monthLength)) {1385nfd = month1 + monthLength - 1;1386}1387int dayOfMonth;1388if (isCutoverYear) {1389// If we are in the cutover year, convert nfd to1390// its calendar date and use dayOfMonth.1391BaseCalendar.Date d = getCalendarDate(nfd);1392dayOfMonth = d.getDayOfMonth();1393} else {1394dayOfMonth = (int)(nfd - month1) + 1;1395}1396set(DAY_OF_MONTH, dayOfMonth);1397return;1398}13991400case DAY_OF_MONTH:1401{1402if (!isCutoverYear(cdate.getNormalizedYear())) {1403max = calsys.getMonthLength(cdate);1404break;1405}14061407// Cutover year handling1408long fd = getCurrentFixedDate();1409long month1 = getFixedDateMonth1(cdate, fd);1410// It may not be a regular month. Convert the date and range to1411// the relative values, perform the roll, and1412// convert the result back to the rolled date.1413int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);1414BaseCalendar.Date d = getCalendarDate(month1 + value);1415assert d.getMonth()-1 == internalGet(MONTH);1416set(DAY_OF_MONTH, d.getDayOfMonth());1417return;1418}14191420case DAY_OF_YEAR:1421{1422max = getActualMaximum(field);1423if (!isCutoverYear(cdate.getNormalizedYear())) {1424break;1425}14261427// Handle cutover here.1428long fd = getCurrentFixedDate();1429long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;1430int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);1431BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);1432set(MONTH, d.getMonth() - 1);1433set(DAY_OF_MONTH, d.getDayOfMonth());1434return;1435}14361437case DAY_OF_WEEK:1438{1439if (!isCutoverYear(cdate.getNormalizedYear())) {1440// If the week of year is in the same year, we can1441// just change DAY_OF_WEEK.1442int weekOfYear = internalGet(WEEK_OF_YEAR);1443if (weekOfYear > 1 && weekOfYear < 52) {1444set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]1445max = SATURDAY;1446break;1447}1448}14491450// We need to handle it in a different way around year1451// boundaries and in the cutover year. Note that1452// changing era and year values violates the roll1453// rule: not changing larger calendar fields...1454amount %= 7;1455if (amount == 0) {1456return;1457}1458long fd = getCurrentFixedDate();1459long dowFirst = BaseCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());1460fd += amount;1461if (fd < dowFirst) {1462fd += 7;1463} else if (fd >= dowFirst + 7) {1464fd -= 7;1465}1466BaseCalendar.Date d = getCalendarDate(fd);1467set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));1468set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());1469return;1470}14711472case DAY_OF_WEEK_IN_MONTH:1473{1474min = 1; // after normalized, min should be 1.1475if (!isCutoverYear(cdate.getNormalizedYear())) {1476int dom = internalGet(DAY_OF_MONTH);1477int monthLength = calsys.getMonthLength(cdate);1478int lastDays = monthLength % 7;1479max = monthLength / 7;1480int x = (dom - 1) % 7;1481if (x < lastDays) {1482max++;1483}1484set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));1485break;1486}14871488// Cutover year handling1489long fd = getCurrentFixedDate();1490long month1 = getFixedDateMonth1(cdate, fd);1491int monthLength = actualMonthLength();1492int lastDays = monthLength % 7;1493max = monthLength / 7;1494int x = (int)(fd - month1) % 7;1495if (x < lastDays) {1496max++;1497}1498int value = getRolledValue(internalGet(field), amount, min, max) - 1;1499fd = month1 + value * 7 + x;1500BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();1501BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);1502cal.getCalendarDateFromFixedDate(d, fd);1503set(DAY_OF_MONTH, d.getDayOfMonth());1504return;1505}1506}15071508set(field, getRolledValue(internalGet(field), amount, min, max));1509}15101511/**1512* Returns the minimum value for the given calendar field of this1513* {@code GregorianCalendar} instance. The minimum value is1514* defined as the smallest value returned by the {@link1515* Calendar#get(int) get} method for any possible time value,1516* taking into consideration the current values of the1517* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1518* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1519* {@link #getGregorianChange() getGregorianChange} and1520* {@link Calendar#getTimeZone() getTimeZone} methods.1521*1522* @param field the calendar field.1523* @return the minimum value for the given calendar field.1524* @see #getMaximum(int)1525* @see #getGreatestMinimum(int)1526* @see #getLeastMaximum(int)1527* @see #getActualMinimum(int)1528* @see #getActualMaximum(int)1529*/1530@Override1531public int getMinimum(int field) {1532return MIN_VALUES[field];1533}15341535/**1536* Returns the maximum value for the given calendar field of this1537* {@code GregorianCalendar} instance. The maximum value is1538* defined as the largest value returned by the {@link1539* Calendar#get(int) get} method for any possible time value,1540* taking into consideration the current values of the1541* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1542* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1543* {@link #getGregorianChange() getGregorianChange} and1544* {@link Calendar#getTimeZone() getTimeZone} methods.1545*1546* @param field the calendar field.1547* @return the maximum value for the given calendar field.1548* @see #getMinimum(int)1549* @see #getGreatestMinimum(int)1550* @see #getLeastMaximum(int)1551* @see #getActualMinimum(int)1552* @see #getActualMaximum(int)1553*/1554@Override1555public int getMaximum(int field) {1556switch (field) {1557case MONTH, DAY_OF_MONTH, DAY_OF_YEAR, WEEK_OF_YEAR, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, YEAR -> {1558// On or after Gregorian 200-3-1, Julian and Gregorian1559// calendar dates are the same or Gregorian dates are1560// larger (i.e., there is a "gap") after 300-3-1.1561if (gregorianCutoverYear > 200) {1562break;1563}1564// There might be "overlapping" dates.1565GregorianCalendar gc = (GregorianCalendar) clone();1566gc.setLenient(true);1567gc.setTimeInMillis(gregorianCutover);1568int v1 = gc.getActualMaximum(field);1569gc.setTimeInMillis(gregorianCutover - 1);1570int v2 = gc.getActualMaximum(field);1571return Math.max(MAX_VALUES[field], Math.max(v1, v2));1572}1573}1574return MAX_VALUES[field];1575}15761577/**1578* Returns the highest minimum value for the given calendar field1579* of this {@code GregorianCalendar} instance. The highest1580* minimum value is defined as the largest value returned by1581* {@link #getActualMinimum(int)} for any possible time value,1582* taking into consideration the current values of the1583* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1584* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1585* {@link #getGregorianChange() getGregorianChange} and1586* {@link Calendar#getTimeZone() getTimeZone} methods.1587*1588* @param field the calendar field.1589* @return the highest minimum value for the given calendar field.1590* @see #getMinimum(int)1591* @see #getMaximum(int)1592* @see #getLeastMaximum(int)1593* @see #getActualMinimum(int)1594* @see #getActualMaximum(int)1595*/1596@Override1597public int getGreatestMinimum(int field) {1598if (field == DAY_OF_MONTH) {1599BaseCalendar.Date d = getGregorianCutoverDate();1600long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);1601d = getCalendarDate(mon1);1602return Math.max(MIN_VALUES[field], d.getDayOfMonth());1603}1604return MIN_VALUES[field];1605}16061607/**1608* Returns the lowest maximum value for the given calendar field1609* of this {@code GregorianCalendar} instance. The lowest1610* maximum value is defined as the smallest value returned by1611* {@link #getActualMaximum(int)} for any possible time value,1612* taking into consideration the current values of the1613* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1614* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1615* {@link #getGregorianChange() getGregorianChange} and1616* {@link Calendar#getTimeZone() getTimeZone} methods.1617*1618* @param field the calendar field1619* @return the lowest maximum value for the given calendar field.1620* @see #getMinimum(int)1621* @see #getMaximum(int)1622* @see #getGreatestMinimum(int)1623* @see #getActualMinimum(int)1624* @see #getActualMaximum(int)1625*/1626@Override1627public int getLeastMaximum(int field) {1628switch (field) {1629case MONTH, DAY_OF_MONTH, DAY_OF_YEAR, WEEK_OF_YEAR, WEEK_OF_MONTH, DAY_OF_WEEK_IN_MONTH, YEAR -> {1630GregorianCalendar gc = (GregorianCalendar) clone();1631gc.setLenient(true);1632gc.setTimeInMillis(gregorianCutover);1633int v1 = gc.getActualMaximum(field);1634gc.setTimeInMillis(gregorianCutover - 1);1635int v2 = gc.getActualMaximum(field);1636return Math.min(LEAST_MAX_VALUES[field], Math.min(v1, v2));1637}1638}1639return LEAST_MAX_VALUES[field];1640}16411642/**1643* Returns the minimum value that this calendar field could have,1644* taking into consideration the given time value and the current1645* values of the1646* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1647* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1648* {@link #getGregorianChange() getGregorianChange} and1649* {@link Calendar#getTimeZone() getTimeZone} methods.1650*1651* <p>For example, if the Gregorian change date is January 10,1652* 1970 and the date of this {@code GregorianCalendar} is1653* January 20, 1970, the actual minimum value of the1654* {@code DAY_OF_MONTH} field is 10 because the previous date1655* of January 10, 1970 is December 27, 1996 (in the Julian1656* calendar). Therefore, December 28, 1969 to January 9, 19701657* don't exist.1658*1659* @param field the calendar field1660* @return the minimum of the given field for the time value of1661* this {@code GregorianCalendar}1662* @see #getMinimum(int)1663* @see #getMaximum(int)1664* @see #getGreatestMinimum(int)1665* @see #getLeastMaximum(int)1666* @see #getActualMaximum(int)1667* @since 1.21668*/1669@Override1670public int getActualMinimum(int field) {1671if (field == DAY_OF_MONTH) {1672GregorianCalendar gc = getNormalizedCalendar();1673int year = gc.cdate.getNormalizedYear();1674if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {1675long month1 = getFixedDateMonth1(gc.cdate, gc.calsys.getFixedDate(gc.cdate));1676BaseCalendar.Date d = getCalendarDate(month1);1677return d.getDayOfMonth();1678}1679}1680return getMinimum(field);1681}16821683/**1684* Returns the maximum value that this calendar field could have,1685* taking into consideration the given time value and the current1686* values of the1687* {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},1688* {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},1689* {@link #getGregorianChange() getGregorianChange} and1690* {@link Calendar#getTimeZone() getTimeZone} methods.1691* For example, if the date of this instance is February 1, 2004,1692* the actual maximum value of the {@code DAY_OF_MONTH} field1693* is 29 because 2004 is a leap year, and if the date of this1694* instance is February 1, 2005, it's 28.1695*1696* <p>This method calculates the maximum value of {@link1697* Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link1698* Calendar#YEAR YEAR} (calendar year) value, not the <a1699* href="#week_year">week year</a>. Call {@link1700* #getWeeksInWeekYear()} to get the maximum value of {@code1701* WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.1702*1703* @param field the calendar field1704* @return the maximum of the given field for the time value of1705* this {@code GregorianCalendar}1706* @see #getMinimum(int)1707* @see #getMaximum(int)1708* @see #getGreatestMinimum(int)1709* @see #getLeastMaximum(int)1710* @see #getActualMinimum(int)1711* @since 1.21712*/1713@Override1714public int getActualMaximum(int field) {1715final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|1716HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|1717ZONE_OFFSET_MASK|DST_OFFSET_MASK;1718if ((fieldsForFixedMax & (1<<field)) != 0) {1719return getMaximum(field);1720}17211722GregorianCalendar gc = getNormalizedCalendar();1723BaseCalendar.Date date = gc.cdate;1724BaseCalendar cal = gc.calsys;1725int normalizedYear = date.getNormalizedYear();17261727int value = -1;1728switch (field) {1729case MONTH -> {1730if (!gc.isCutoverYear(normalizedYear)) {1731value = DECEMBER;1732break;1733}17341735// January 1 of the next year may or may not exist.1736long nextJan1;1737do {1738nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);1739} while (nextJan1 < gregorianCutoverDate);1740BaseCalendar.Date d = (BaseCalendar.Date) date.clone();1741cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);1742value = d.getMonth() - 1;1743}1744case DAY_OF_MONTH -> {1745value = cal.getMonthLength(date);1746if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {1747break;1748}17491750// Handle cutover year.1751long fd = gc.getCurrentFixedDate();1752if (fd >= gregorianCutoverDate) {1753break;1754}1755int monthLength = gc.actualMonthLength();1756long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;1757// Convert the fixed date to its calendar date.1758BaseCalendar.Date d = gc.getCalendarDate(monthEnd);1759value = d.getDayOfMonth();1760}1761case DAY_OF_YEAR -> {1762if (!gc.isCutoverYear(normalizedYear)) {1763value = cal.getYearLength(date);1764break;1765}17661767// Handle cutover year.1768long jan1;1769if (gregorianCutoverYear == gregorianCutoverYearJulian) {1770BaseCalendar cocal = gc.getCutoverCalendarSystem();1771jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);1772} else if (normalizedYear == gregorianCutoverYearJulian) {1773jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);1774} else {1775jan1 = gregorianCutoverDate;1776}1777// January 1 of the next year may or may not exist.1778long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);1779if (nextJan1 < gregorianCutoverDate) {1780nextJan1 = gregorianCutoverDate;1781}1782assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),1783date.getDayOfMonth(), date);1784assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),1785date.getDayOfMonth(), date);1786value = (int)(nextJan1 - jan1);1787}1788case WEEK_OF_YEAR -> {1789if (!gc.isCutoverYear(normalizedYear)) {1790// Get the day of week of January 1 of the year1791CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);1792d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);1793int dayOfWeek = cal.getDayOfWeek(d);1794// Normalize the day of week with the firstDayOfWeek value1795dayOfWeek -= getFirstDayOfWeek();1796if (dayOfWeek < 0) {1797dayOfWeek += 7;1798}1799value = 52;1800int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;1801if ((magic == 6) ||1802(date.isLeapYear() && (magic == 5 || magic == 12))) {1803value++;1804}1805break;1806}18071808if (gc == this) {1809gc = (GregorianCalendar) gc.clone();1810}1811int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);1812gc.set(DAY_OF_YEAR, maxDayOfYear);1813value = gc.get(WEEK_OF_YEAR);1814if (internalGet(YEAR) != gc.getWeekYear()) {1815gc.set(DAY_OF_YEAR, maxDayOfYear - 7);1816value = gc.get(WEEK_OF_YEAR);1817}1818}1819case WEEK_OF_MONTH -> {1820if (!gc.isCutoverYear(normalizedYear)) {1821CalendarDate d = cal.newCalendarDate(null);1822d.setDate(date.getYear(), date.getMonth(), 1);1823int dayOfWeek = cal.getDayOfWeek(d);1824int monthLength = cal.getMonthLength(d);1825dayOfWeek -= getFirstDayOfWeek();1826if (dayOfWeek < 0) {1827dayOfWeek += 7;1828}1829int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week1830value = 3;1831if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {1832value++;1833}1834monthLength -= nDaysFirstWeek + 7 * 3;1835if (monthLength > 0) {1836value++;1837if (monthLength > 7) {1838value++;1839}1840}1841break;1842}18431844// Cutover year handling1845if (gc == this) {1846gc = (GregorianCalendar) gc.clone();1847}1848int y = gc.internalGet(YEAR);1849int m = gc.internalGet(MONTH);1850do {1851value = gc.get(WEEK_OF_MONTH);1852gc.add(WEEK_OF_MONTH, +1);1853} while (gc.get(YEAR) == y && gc.get(MONTH) == m);1854}1855case DAY_OF_WEEK_IN_MONTH -> {1856// may be in the Gregorian cutover month1857int ndays, dow1;1858int dow = date.getDayOfWeek();1859if (!gc.isCutoverYear(normalizedYear)) {1860BaseCalendar.Date d = (BaseCalendar.Date) date.clone();1861ndays = cal.getMonthLength(d);1862d.setDayOfMonth(1);1863cal.normalize(d);1864dow1 = d.getDayOfWeek();1865} else {1866// Let a cloned GregorianCalendar take care of the cutover cases.1867if (gc == this) {1868gc = (GregorianCalendar) clone();1869}1870ndays = gc.actualMonthLength();1871gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));1872dow1 = gc.get(DAY_OF_WEEK);1873}1874int x = dow - dow1;1875if (x < 0) {1876x += 7;1877}1878ndays -= x;1879value = (ndays + 6) / 7;1880}1881case YEAR -> {1882/* The year computation is no different, in principle, from the1883* others, however, the range of possible maxima is large. In1884* addition, the way we know we've exceeded the range is different.1885* For these reasons, we use the special case code below to handle1886* this field.1887*1888* The actual maxima for YEAR depend on the type of calendar:1889*1890* Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE1891* Julian = Dec 2, 292269055 BCE - Jan 3, 292272993 CE1892* Hybrid = Dec 2, 292269055 BCE - Aug 17, 292278994 CE1893*1894* We know we've exceeded the maximum when either the month, date,1895* time, or era changes in response to setting the year. We don't1896* check for month, date, and time here because the year and era are1897* sufficient to detect an invalid year setting. NOTE: If code is1898* added to check the month and date in the future for some reason,1899* Feb 29 must be allowed to shift to Mar 1 when setting the year.1900*/1901if (gc == this) {1902gc = (GregorianCalendar) clone();1903}19041905// Calculate the millisecond offset from the beginning1906// of the year of this calendar and adjust the max1907// year value if we are beyond the limit in the max1908// year.1909long current = gc.getYearOffsetInMillis();19101911if (gc.internalGetEra() == CE) {1912gc.setTimeInMillis(Long.MAX_VALUE);1913value = gc.get(YEAR);1914long maxEnd = gc.getYearOffsetInMillis();1915if (current > maxEnd) {1916value--;1917}1918} else {1919CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?1920gcal : getJulianCalendarSystem();1921CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());1922long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();1923maxEnd *= 60;1924maxEnd += d.getMinutes();1925maxEnd *= 60;1926maxEnd += d.getSeconds();1927maxEnd *= 1000;1928maxEnd += d.getMillis();1929value = d.getYear();1930if (value <= 0) {1931assert mincal == gcal;1932value = 1 - value;1933}1934if (current < maxEnd) {1935value--;1936}1937}1938}1939default -> throw new ArrayIndexOutOfBoundsException(field);1940}1941return value;1942}19431944/**1945* Returns the millisecond offset from the beginning of this1946* year. This Calendar object must have been normalized.1947*/1948private long getYearOffsetInMillis() {1949long t = (internalGet(DAY_OF_YEAR) - 1) * 24;1950t += internalGet(HOUR_OF_DAY);1951t *= 60;1952t += internalGet(MINUTE);1953t *= 60;1954t += internalGet(SECOND);1955t *= 1000;1956return t + internalGet(MILLISECOND) -1957(internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));1958}19591960@Override1961public Object clone()1962{1963GregorianCalendar other = (GregorianCalendar) super.clone();19641965other.gdate = (BaseCalendar.Date) gdate.clone();1966if (cdate != null) {1967if (cdate != gdate) {1968other.cdate = (BaseCalendar.Date) cdate.clone();1969} else {1970other.cdate = other.gdate;1971}1972}1973other.originalFields = null;1974other.zoneOffsets = null;1975return other;1976}19771978@Override1979public TimeZone getTimeZone() {1980TimeZone zone = super.getTimeZone();1981// To share the zone by CalendarDates1982gdate.setZone(zone);1983if (cdate != null && cdate != gdate) {1984cdate.setZone(zone);1985}1986return zone;1987}19881989@Override1990public void setTimeZone(TimeZone zone) {1991super.setTimeZone(zone);1992// To share the zone by CalendarDates1993gdate.setZone(zone);1994if (cdate != null && cdate != gdate) {1995cdate.setZone(zone);1996}1997}19981999/**2000* Returns {@code true} indicating this {@code GregorianCalendar}2001* supports week dates.2002*2003* @return {@code true} (always)2004* @see #getWeekYear()2005* @see #setWeekDate(int,int,int)2006* @see #getWeeksInWeekYear()2007* @since 1.72008*/2009@Override2010public final boolean isWeekDateSupported() {2011return true;2012}20132014/**2015* Returns the <a href="#week_year">week year</a> represented by this2016* {@code GregorianCalendar}. The dates in the weeks between 1 and the2017* maximum week number of the week year have the same week year value2018* that may be one year before or after the {@link Calendar#YEAR YEAR}2019* (calendar year) value.2020*2021* <p>This method calls {@link Calendar#complete()} before2022* calculating the week year.2023*2024* @return the week year represented by this {@code GregorianCalendar}.2025* If the {@link Calendar#ERA ERA} value is {@link #BC}, the year is2026* represented by 0 or a negative number: BC 1 is 0, BC 22027* is -1, BC 3 is -2, and so on.2028* @throws IllegalArgumentException2029* if any of the calendar fields is invalid in non-lenient mode.2030* @see #isWeekDateSupported()2031* @see #getWeeksInWeekYear()2032* @see Calendar#getFirstDayOfWeek()2033* @see Calendar#getMinimalDaysInFirstWeek()2034* @since 1.72035*/2036@Override2037public int getWeekYear() {2038int year = get(YEAR); // implicitly calls complete()2039if (internalGetEra() == BCE) {2040year = 1 - year;2041}20422043// Fast path for the Gregorian calendar years that are never2044// affected by the Julian-Gregorian transition2045if (year > gregorianCutoverYear + 1) {2046int weekOfYear = internalGet(WEEK_OF_YEAR);2047if (internalGet(MONTH) == JANUARY) {2048if (weekOfYear >= 52) {2049--year;2050}2051} else {2052if (weekOfYear == 1) {2053++year;2054}2055}2056return year;2057}20582059// General (slow) path2060int dayOfYear = internalGet(DAY_OF_YEAR);2061int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);2062int minimalDays = getMinimalDaysInFirstWeek();20632064// Quickly check the possibility of year adjustments before2065// cloning this GregorianCalendar.2066if (dayOfYear > minimalDays && dayOfYear < (maxDayOfYear - 6)) {2067return year;2068}20692070// Create a clone to work on the calculation2071GregorianCalendar cal = (GregorianCalendar) clone();2072cal.setLenient(true);2073// Use GMT so that intermediate date calculations won't2074// affect the time of day fields.2075cal.setTimeZone(TimeZone.getTimeZone("GMT"));2076// Go to the first day of the year, which is usually January 1.2077cal.set(DAY_OF_YEAR, 1);2078cal.complete();20792080// Get the first day of the first day-of-week in the year.2081int delta = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);2082if (delta != 0) {2083if (delta < 0) {2084delta += 7;2085}2086cal.add(DAY_OF_YEAR, delta);2087}2088int minDayOfYear = cal.get(DAY_OF_YEAR);2089if (dayOfYear < minDayOfYear) {2090if (minDayOfYear <= minimalDays) {2091--year;2092}2093} else {2094cal.set(YEAR, year + 1);2095cal.set(DAY_OF_YEAR, 1);2096cal.complete();2097int del = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);2098if (del != 0) {2099if (del < 0) {2100del += 7;2101}2102cal.add(DAY_OF_YEAR, del);2103}2104minDayOfYear = cal.get(DAY_OF_YEAR) - 1;2105if (minDayOfYear == 0) {2106minDayOfYear = 7;2107}2108if (minDayOfYear >= minimalDays) {2109int days = maxDayOfYear - dayOfYear + 1;2110if (days <= (7 - minDayOfYear)) {2111++year;2112}2113}2114}2115return year;2116}21172118/**2119* Sets this {@code GregorianCalendar} to the date given by the2120* date specifiers - <a href="#week_year">{@code weekYear}</a>,2121* {@code weekOfYear}, and {@code dayOfWeek}. {@code weekOfYear}2122* follows the <a href="#week_and_year">{@code WEEK_OF_YEAR}2123* numbering</a>. The {@code dayOfWeek} value must be one of the2124* {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} values: {@link2125* Calendar#SUNDAY SUNDAY} to {@link Calendar#SATURDAY SATURDAY}.2126*2127* <p>Note that the numeric day-of-week representation differs from2128* the ISO 8601 standard, and that the {@code weekOfYear}2129* numbering is compatible with the standard when {@code2130* getFirstDayOfWeek()} is {@code MONDAY} and {@code2131* getMinimalDaysInFirstWeek()} is 4.2132*2133* <p>Unlike the {@code set} method, all of the calendar fields2134* and the instant of time value are calculated upon return.2135*2136* <p>If {@code weekOfYear} is out of the valid week-of-year2137* range in {@code weekYear}, the {@code weekYear}2138* and {@code weekOfYear} values are adjusted in lenient2139* mode, or an {@code IllegalArgumentException} is thrown in2140* non-lenient mode.2141*2142* @param weekYear the week year2143* @param weekOfYear the week number based on {@code weekYear}2144* @param dayOfWeek the day of week value: one of the constants2145* for the {@link #DAY_OF_WEEK DAY_OF_WEEK} field:2146* {@link Calendar#SUNDAY SUNDAY}, ...,2147* {@link Calendar#SATURDAY SATURDAY}.2148* @throws IllegalArgumentException2149* if any of the given date specifiers is invalid,2150* or if any of the calendar fields are inconsistent2151* with the given date specifiers in non-lenient mode2152* @see GregorianCalendar#isWeekDateSupported()2153* @see Calendar#getFirstDayOfWeek()2154* @see Calendar#getMinimalDaysInFirstWeek()2155* @since 1.72156*/2157@Override2158public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {2159if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {2160throw new IllegalArgumentException("invalid dayOfWeek: " + dayOfWeek);2161}21622163// To avoid changing the time of day fields by date2164// calculations, use a clone with the GMT time zone.2165GregorianCalendar gc = (GregorianCalendar) clone();2166gc.setLenient(true);2167int era = gc.get(ERA);2168gc.clear();2169gc.setTimeZone(TimeZone.getTimeZone("GMT"));2170gc.set(ERA, era);2171gc.set(YEAR, weekYear);2172gc.set(WEEK_OF_YEAR, 1);2173gc.set(DAY_OF_WEEK, getFirstDayOfWeek());2174int days = dayOfWeek - getFirstDayOfWeek();2175if (days < 0) {2176days += 7;2177}2178days += 7 * (weekOfYear - 1);2179if (days != 0) {2180gc.add(DAY_OF_YEAR, days);2181} else {2182gc.complete();2183}21842185if (!isLenient() &&2186(gc.getWeekYear() != weekYear2187|| gc.internalGet(WEEK_OF_YEAR) != weekOfYear2188|| gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {2189throw new IllegalArgumentException();2190}21912192set(ERA, gc.internalGet(ERA));2193set(YEAR, gc.internalGet(YEAR));2194set(MONTH, gc.internalGet(MONTH));2195set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));21962197// to avoid throwing an IllegalArgumentException in2198// non-lenient, set WEEK_OF_YEAR internally2199internalSet(WEEK_OF_YEAR, weekOfYear);2200complete();2201}22022203/**2204* Returns the number of weeks in the <a href="#week_year">week year</a>2205* represented by this {@code GregorianCalendar}.2206*2207* <p>For example, if this {@code GregorianCalendar}'s date is2208* December 31, 2008 with <a href="#iso8601_compatible_setting">the ISO2209* 8601 compatible setting</a>, this method will return 53 for the2210* period: December 29, 2008 to January 3, 2010 while {@link2211* #getActualMaximum(int) getActualMaximum(WEEK_OF_YEAR)} will return2212* 52 for the period: December 31, 2007 to December 28, 2008.2213*2214* @return the number of weeks in the week year.2215* @see Calendar#WEEK_OF_YEAR2216* @see #getWeekYear()2217* @see #getActualMaximum(int)2218* @since 1.72219*/2220@Override2221public int getWeeksInWeekYear() {2222GregorianCalendar gc = getNormalizedCalendar();2223int weekYear = gc.getWeekYear();2224if (weekYear == gc.internalGet(YEAR)) {2225return gc.getActualMaximum(WEEK_OF_YEAR);2226}22272228// Use the 2nd week for calculating the max of WEEK_OF_YEAR2229if (gc == this) {2230gc = (GregorianCalendar) gc.clone();2231}2232gc.setWeekDate(weekYear, 2, internalGet(DAY_OF_WEEK));2233return gc.getActualMaximum(WEEK_OF_YEAR);2234}22352236/////////////////////////////2237// Time => Fields computation2238/////////////////////////////22392240/**2241* The fixed date corresponding to gdate. If the value is2242* Long.MIN_VALUE, the fixed date value is unknown. Currently,2243* Julian calendar dates are not cached.2244*/2245private transient long cachedFixedDate = Long.MIN_VALUE;22462247/**2248* Converts the time value (millisecond offset from the <a2249* href="Calendar.html#Epoch">Epoch</a>) to calendar field values.2250* The time is <em>not</em>2251* recomputed first; to recompute the time, then the fields, call the2252* {@code complete} method.2253*2254* @see Calendar#complete2255*/2256@Override2257protected void computeFields() {2258int mask;2259if (isPartiallyNormalized()) {2260// Determine which calendar fields need to be computed.2261mask = getSetStateFields();2262int fieldMask = ~mask & ALL_FIELDS;2263// We have to call computTime in case calsys == null in2264// order to set calsys and cdate. (6263644)2265if (fieldMask != 0 || calsys == null) {2266mask |= computeFields(fieldMask,2267mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));2268assert mask == ALL_FIELDS;2269}2270} else {2271mask = ALL_FIELDS;2272computeFields(mask, 0);2273}2274// After computing all the fields, set the field state to `COMPUTED'.2275setFieldsComputed(mask);2276}22772278/**2279* This computeFields implements the conversion from UTC2280* (millisecond offset from the Epoch) to calendar2281* field values. fieldMask specifies which fields to change the2282* setting state to COMPUTED, although all fields are set to2283* the correct values. This is required to fix 4685354.2284*2285* @param fieldMask a bit mask to specify which fields to change2286* the setting state.2287* @param tzMask a bit mask to specify which time zone offset2288* fields to be used for time calculations2289* @return a new field mask that indicates what field values have2290* actually been set.2291*/2292private int computeFields(int fieldMask, int tzMask) {2293int zoneOffset = 0;2294TimeZone tz = getZone();2295if (zoneOffsets == null) {2296zoneOffsets = new int[2];2297}2298if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {2299if (tz instanceof ZoneInfo) {2300zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);2301} else {2302zoneOffset = tz.getOffset(time);2303zoneOffsets[0] = tz.getRawOffset();2304zoneOffsets[1] = zoneOffset - zoneOffsets[0];2305}2306}2307if (tzMask != 0) {2308if (isFieldSet(tzMask, ZONE_OFFSET)) {2309zoneOffsets[0] = internalGet(ZONE_OFFSET);2310}2311if (isFieldSet(tzMask, DST_OFFSET)) {2312zoneOffsets[1] = internalGet(DST_OFFSET);2313}2314zoneOffset = zoneOffsets[0] + zoneOffsets[1];2315}23162317// By computing time and zoneOffset separately, we can take2318// the wider range of time+zoneOffset than the previous2319// implementation.2320long fixedDate = zoneOffset / ONE_DAY;2321int timeOfDay = zoneOffset % (int)ONE_DAY;2322fixedDate += time / ONE_DAY;2323timeOfDay += (int) (time % ONE_DAY);2324if (timeOfDay >= ONE_DAY) {2325timeOfDay -= ONE_DAY;2326++fixedDate;2327} else {2328while (timeOfDay < 0) {2329timeOfDay += ONE_DAY;2330--fixedDate;2331}2332}2333fixedDate += EPOCH_OFFSET;23342335int era = CE;2336int year;2337if (fixedDate >= gregorianCutoverDate) {2338// Handle Gregorian dates.2339assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized()2340: "cache control: not normalized";2341assert cachedFixedDate == Long.MIN_VALUE ||2342gcal.getFixedDate(gdate.getNormalizedYear(),2343gdate.getMonth(),2344gdate.getDayOfMonth(), gdate)2345== cachedFixedDate2346: "cache control: inconsictency" +2347", cachedFixedDate=" + cachedFixedDate +2348", computed=" +2349gcal.getFixedDate(gdate.getNormalizedYear(),2350gdate.getMonth(),2351gdate.getDayOfMonth(),2352gdate) +2353", date=" + gdate;23542355// See if we can use gdate to avoid date calculation.2356if (fixedDate != cachedFixedDate) {2357gcal.getCalendarDateFromFixedDate(gdate, fixedDate);2358cachedFixedDate = fixedDate;2359}23602361year = gdate.getYear();2362if (year <= 0) {2363year = 1 - year;2364era = BCE;2365}2366calsys = gcal;2367cdate = gdate;2368assert cdate.getDayOfWeek() > 0 : "dow="+cdate.getDayOfWeek()+", date="+cdate;2369} else {2370// Handle Julian calendar dates.2371calsys = getJulianCalendarSystem();2372cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());2373jcal.getCalendarDateFromFixedDate(cdate, fixedDate);2374Era e = cdate.getEra();2375if (e == jeras[0]) {2376era = BCE;2377}2378year = cdate.getYear();2379}23802381// Always set the ERA and YEAR values.2382internalSet(ERA, era);2383internalSet(YEAR, year);2384int mask = fieldMask | (ERA_MASK|YEAR_MASK);23852386int month = cdate.getMonth() - 1; // 0-based2387int dayOfMonth = cdate.getDayOfMonth();23882389// Set the basic date fields.2390if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))2391!= 0) {2392internalSet(MONTH, month);2393internalSet(DAY_OF_MONTH, dayOfMonth);2394internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());2395mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;2396}23972398if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK2399|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {2400if (timeOfDay != 0) {2401int hours = timeOfDay / ONE_HOUR;2402internalSet(HOUR_OF_DAY, hours);2403internalSet(AM_PM, hours / 12); // Assume AM == 02404internalSet(HOUR, hours % 12);2405int r = timeOfDay % ONE_HOUR;2406internalSet(MINUTE, r / ONE_MINUTE);2407r %= ONE_MINUTE;2408internalSet(SECOND, r / ONE_SECOND);2409internalSet(MILLISECOND, r % ONE_SECOND);2410} else {2411internalSet(HOUR_OF_DAY, 0);2412internalSet(AM_PM, AM);2413internalSet(HOUR, 0);2414internalSet(MINUTE, 0);2415internalSet(SECOND, 0);2416internalSet(MILLISECOND, 0);2417}2418mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK2419|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);2420}24212422if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {2423internalSet(ZONE_OFFSET, zoneOffsets[0]);2424internalSet(DST_OFFSET, zoneOffsets[1]);2425mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);2426}24272428if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {2429int normalizedYear = cdate.getNormalizedYear();2430long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);2431int dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;2432long fixedDateMonth1 = fixedDate - dayOfMonth + 1;2433int cutoverGap = 0;2434int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;2435int relativeDayOfMonth = dayOfMonth - 1;24362437// If we are in the cutover year, we need some special handling.2438if (normalizedYear == cutoverYear) {2439// Need to take care of the "missing" days.2440if (gregorianCutoverYearJulian <= gregorianCutoverYear) {2441// We need to find out where we are. The cutover2442// gap could even be more than one year. (One2443// year difference in ~48667 years.)2444fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);2445if (fixedDate >= gregorianCutoverDate) {2446fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);2447}2448}2449int realDayOfYear = (int)(fixedDate - fixedDateJan1) + 1;2450cutoverGap = dayOfYear - realDayOfYear;2451dayOfYear = realDayOfYear;2452relativeDayOfMonth = (int)(fixedDate - fixedDateMonth1);2453}2454internalSet(DAY_OF_YEAR, dayOfYear);2455internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);24562457int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);24582459// The spec is to calculate WEEK_OF_YEAR in the2460// ISO8601-style. This creates problems, though.2461if (weekOfYear == 0) {2462// If the date belongs to the last week of the2463// previous year, use the week number of "12/31" of2464// the "previous" year. Again, if the previous year is2465// the Gregorian cutover year, we need to take care of2466// it. Usually the previous day of January 1 is2467// December 31, which is not always true in2468// GregorianCalendar.2469long fixedDec31 = fixedDateJan1 - 1;2470long prevJan1 = fixedDateJan1 - 365;2471if (normalizedYear > (cutoverYear + 1)) {2472if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {2473--prevJan1;2474}2475} else if (normalizedYear <= gregorianCutoverYearJulian) {2476if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {2477--prevJan1;2478}2479} else {2480BaseCalendar calForJan1 = calsys;2481//int prevYear = normalizedYear - 1;2482int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();2483if (prevYear == gregorianCutoverYear) {2484calForJan1 = getCutoverCalendarSystem();2485if (calForJan1 == jcal) {2486prevJan1 = calForJan1.getFixedDate(prevYear,2487BaseCalendar.JANUARY,24881,2489null);2490} else {2491prevJan1 = gregorianCutoverDate;2492calForJan1 = gcal;2493}2494} else if (prevYear <= gregorianCutoverYearJulian) {2495calForJan1 = getJulianCalendarSystem();2496prevJan1 = calForJan1.getFixedDate(prevYear,2497BaseCalendar.JANUARY,24981,2499null);2500}2501}2502weekOfYear = getWeekNumber(prevJan1, fixedDec31);2503} else {2504if (normalizedYear > gregorianCutoverYear ||2505normalizedYear < (gregorianCutoverYearJulian - 1)) {2506// Regular years2507if (weekOfYear >= 52) {2508long nextJan1 = fixedDateJan1 + 365;2509if (cdate.isLeapYear()) {2510nextJan1++;2511}2512long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,2513getFirstDayOfWeek());2514int ndays = (int)(nextJan1st - nextJan1);2515if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {2516// The first days forms a week in which the date is included.2517weekOfYear = 1;2518}2519}2520} else {2521BaseCalendar calForJan1 = calsys;2522int nextYear = normalizedYear + 1;2523if (nextYear == (gregorianCutoverYearJulian + 1) &&2524nextYear < gregorianCutoverYear) {2525// In case the gap is more than one year.2526nextYear = gregorianCutoverYear;2527}2528if (nextYear == gregorianCutoverYear) {2529calForJan1 = getCutoverCalendarSystem();2530}25312532long nextJan1;2533if (nextYear > gregorianCutoverYear2534|| gregorianCutoverYearJulian == gregorianCutoverYear2535|| nextYear == gregorianCutoverYearJulian) {2536nextJan1 = calForJan1.getFixedDate(nextYear,2537BaseCalendar.JANUARY,25381,2539null);2540} else {2541nextJan1 = gregorianCutoverDate;2542calForJan1 = gcal;2543}25442545long nextJan1st = BaseCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,2546getFirstDayOfWeek());2547int ndays = (int)(nextJan1st - nextJan1);2548if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {2549// The first days forms a week in which the date is included.2550weekOfYear = 1;2551}2552}2553}2554internalSet(WEEK_OF_YEAR, weekOfYear);2555internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));2556mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK);2557}2558return mask;2559}25602561/**2562* Returns the number of weeks in a period between fixedDay1 and2563* fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule2564* is applied to calculate the number of weeks.2565*2566* @param fixedDay1 the fixed date of the first day of the period2567* @param fixedDate the fixed date of the last day of the period2568* @return the number of weeks of the given period2569*/2570private int getWeekNumber(long fixedDay1, long fixedDate) {2571// We can always use `gcal' since Julian and Gregorian are the2572// same thing for this calculation.2573long fixedDay1st = Gregorian.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,2574getFirstDayOfWeek());2575int ndays = (int)(fixedDay1st - fixedDay1);2576assert ndays <= 7;2577if (ndays >= getMinimalDaysInFirstWeek()) {2578fixedDay1st -= 7;2579}2580int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);2581if (normalizedDayOfPeriod >= 0) {2582return normalizedDayOfPeriod / 7 + 1;2583}2584return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;2585}25862587/**2588* Converts calendar field values to the time value (millisecond2589* offset from the <a href="Calendar.html#Epoch">Epoch</a>).2590*2591* @throws IllegalArgumentException if any calendar fields are invalid.2592*/2593@Override2594protected void computeTime() {2595// In non-lenient mode, perform brief checking of calendar2596// fields which have been set externally. Through this2597// checking, the field values are stored in originalFields[]2598// to see if any of them are normalized later.2599if (!isLenient()) {2600if (originalFields == null) {2601originalFields = new int[FIELD_COUNT];2602}2603for (int field = 0; field < FIELD_COUNT; field++) {2604int value = internalGet(field);2605if (isExternallySet(field)) {2606// Quick validation for any out of range values2607if (value < getMinimum(field) || value > getMaximum(field)) {2608throw new IllegalArgumentException(getFieldName(field));2609}2610}2611originalFields[field] = value;2612}2613}26142615// Let the super class determine which calendar fields to be2616// used to calculate the time.2617int fieldMask = selectFields();26182619// The year defaults to the epoch start. We don't check2620// fieldMask for YEAR because YEAR is a mandatory field to2621// determine the date.2622int year = isSet(YEAR) ? internalGet(YEAR) : EPOCH_YEAR;26232624int era = internalGetEra();2625if (era == BCE) {2626year = 1 - year;2627} else if (era != CE) {2628// Even in lenient mode we disallow ERA values other than CE & BCE.2629// (The same normalization rule as add()/roll() could be2630// applied here in lenient mode. But this checking is kept2631// unchanged for compatibility as of 1.5.)2632throw new IllegalArgumentException("Invalid era");2633}26342635// If year is 0 or negative, we need to set the ERA value later.2636if (year <= 0 && !isSet(ERA)) {2637fieldMask |= ERA_MASK;2638setFieldsComputed(ERA_MASK);2639}26402641// Calculate the time of day. We rely on the convention that2642// an UNSET field has 0.2643long timeOfDay = 0;2644if (isFieldSet(fieldMask, HOUR_OF_DAY)) {2645timeOfDay += (long) internalGet(HOUR_OF_DAY);2646} else {2647timeOfDay += internalGet(HOUR);2648// The default value of AM_PM is 0 which designates AM.2649if (isFieldSet(fieldMask, AM_PM)) {2650timeOfDay += 12 * internalGet(AM_PM);2651}2652}2653timeOfDay *= 60;2654timeOfDay += internalGet(MINUTE);2655timeOfDay *= 60;2656timeOfDay += internalGet(SECOND);2657timeOfDay *= 1000;2658timeOfDay += internalGet(MILLISECOND);26592660// Convert the time of day to the number of days and the2661// millisecond offset from midnight.2662long fixedDate = timeOfDay / ONE_DAY;2663timeOfDay %= ONE_DAY;2664while (timeOfDay < 0) {2665timeOfDay += ONE_DAY;2666--fixedDate;2667}26682669// Calculate the fixed date since January 1, 1 (Gregorian).2670calculateFixedDate: {2671long gfd, jfd;2672if (year > gregorianCutoverYear && year > gregorianCutoverYearJulian) {2673gfd = fixedDate + getFixedDate(gcal, year, fieldMask);2674if (gfd >= gregorianCutoverDate) {2675fixedDate = gfd;2676break calculateFixedDate;2677}2678jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2679} else if (year < gregorianCutoverYear && year < gregorianCutoverYearJulian) {2680jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2681if (jfd < gregorianCutoverDate) {2682fixedDate = jfd;2683break calculateFixedDate;2684}2685gfd = jfd;2686} else {2687jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);2688gfd = fixedDate + getFixedDate(gcal, year, fieldMask);2689}26902691// Now we have to determine which calendar date it is.26922693// If the date is relative from the beginning of the year2694// in the Julian calendar, then use jfd;2695if (isFieldSet(fieldMask, DAY_OF_YEAR) || isFieldSet(fieldMask, WEEK_OF_YEAR)) {2696if (gregorianCutoverYear == gregorianCutoverYearJulian) {2697fixedDate = jfd;2698break calculateFixedDate;2699} else if (year == gregorianCutoverYear) {2700fixedDate = gfd;2701break calculateFixedDate;2702}2703}27042705if (gfd >= gregorianCutoverDate) {2706if (jfd >= gregorianCutoverDate) {2707fixedDate = gfd;2708} else {2709// The date is in an "overlapping" period. No way2710// to disambiguate it. Determine it using the2711// previous date calculation.2712if (calsys == gcal || calsys == null) {2713fixedDate = gfd;2714} else {2715fixedDate = jfd;2716}2717}2718} else {2719if (jfd < gregorianCutoverDate) {2720fixedDate = jfd;2721} else {2722// The date is in a "missing" period.2723if (!isLenient()) {2724throw new IllegalArgumentException("the specified date doesn't exist");2725}2726// Take the Julian date for compatibility, which2727// will produce a Gregorian date.2728fixedDate = jfd;2729}2730}2731}27322733// millis represents local wall-clock time in milliseconds.2734long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;27352736// Compute the time zone offset and DST offset. There are two potential2737// ambiguities here. We'll assume a 2:00 am (wall time) switchover time2738// for discussion purposes here.2739// 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am2740// can be in standard or in DST depending. However, 2:00 am is an invalid2741// representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).2742// We assume standard time.2743// 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am2744// can be in standard or DST. Both are valid representations (the rep2745// jumps from 1:59:59 DST to 1:00:00 Std).2746// Again, we assume standard time.2747// We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET2748// or DST_OFFSET fields; then we use those fields.2749TimeZone zone = getZone();2750if (zoneOffsets == null) {2751zoneOffsets = new int[2];2752}2753int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);2754if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {2755if (zone instanceof ZoneInfo) {2756((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets);2757} else {2758int gmtOffset = isFieldSet(fieldMask, ZONE_OFFSET) ?2759internalGet(ZONE_OFFSET) : zone.getRawOffset();2760zone.getOffsets(millis - gmtOffset, zoneOffsets);2761}2762}2763if (tzMask != 0) {2764if (isFieldSet(tzMask, ZONE_OFFSET)) {2765zoneOffsets[0] = internalGet(ZONE_OFFSET);2766}2767if (isFieldSet(tzMask, DST_OFFSET)) {2768zoneOffsets[1] = internalGet(DST_OFFSET);2769}2770}27712772// Adjust the time zone offset values to get the UTC time.2773millis -= zoneOffsets[0] + zoneOffsets[1];27742775// Set this calendar's time in milliseconds2776time = millis;27772778int mask = computeFields(fieldMask | getSetStateFields(), tzMask);27792780if (!isLenient()) {2781for (int field = 0; field < FIELD_COUNT; field++) {2782if (!isExternallySet(field)) {2783continue;2784}2785if (originalFields[field] != internalGet(field)) {2786String s = originalFields[field] + " -> " + internalGet(field);2787// Restore the original field values2788System.arraycopy(originalFields, 0, fields, 0, fields.length);2789throw new IllegalArgumentException(getFieldName(field) + ": " + s);2790}2791}2792}2793setFieldsNormalized(mask);2794}27952796/**2797* Computes the fixed date under either the Gregorian or the2798* Julian calendar, using the given year and the specified calendar fields.2799*2800* @param cal the CalendarSystem to be used for the date calculation2801* @param year the normalized year number, with 0 indicating the2802* year 1 BCE, -1 indicating 2 BCE, etc.2803* @param fieldMask the calendar fields to be used for the date calculation2804* @return the fixed date2805* @see Calendar#selectFields2806*/2807private long getFixedDate(BaseCalendar cal, int year, int fieldMask) {2808int month = JANUARY;2809if (isFieldSet(fieldMask, MONTH)) {2810// No need to check if MONTH has been set (no isSet(MONTH)2811// call) since its unset value happens to be JANUARY (0).2812month = internalGet(MONTH);28132814// If the month is out of range, adjust it into range2815if (month > DECEMBER) {2816year += month / 12;2817month %= 12;2818} else if (month < JANUARY) {2819int[] rem = new int[1];2820year += CalendarUtils.floorDivide(month, 12, rem);2821month = rem[0];2822}2823}28242825// Get the fixed date since Jan 1, 1 (Gregorian). We are on2826// the first day of either `month' or January in 'year'.2827long fixedDate = cal.getFixedDate(year, month + 1, 1,2828cal == gcal ? gdate : null);2829if (isFieldSet(fieldMask, MONTH)) {2830// Month-based calculations2831if (isFieldSet(fieldMask, DAY_OF_MONTH)) {2832// We are on the first day of the month. Just add the2833// offset if DAY_OF_MONTH is set. If the isSet call2834// returns false, that means DAY_OF_MONTH has been2835// selected just because of the selected2836// combination. We don't need to add any since the2837// default value is the 1st.2838if (isSet(DAY_OF_MONTH)) {2839// To avoid underflow with DAY_OF_MONTH-1, add2840// DAY_OF_MONTH, then subtract 1.2841fixedDate += internalGet(DAY_OF_MONTH);2842fixedDate--;2843}2844} else {2845if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {2846long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,2847getFirstDayOfWeek());2848// If we have enough days in the first week, then2849// move to the previous week.2850if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {2851firstDayOfWeek -= 7;2852}2853if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2854firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,2855internalGet(DAY_OF_WEEK));2856}2857// In lenient mode, we treat days of the previous2858// months as a part of the specified2859// WEEK_OF_MONTH. See 4633646.2860fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);2861} else {2862int dayOfWeek;2863if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2864dayOfWeek = internalGet(DAY_OF_WEEK);2865} else {2866dayOfWeek = getFirstDayOfWeek();2867}2868// We are basing this on the day-of-week-in-month. The only2869// trickiness occurs if the day-of-week-in-month is2870// negative.2871int dowim;2872if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {2873dowim = internalGet(DAY_OF_WEEK_IN_MONTH);2874} else {2875dowim = 1;2876}2877if (dowim >= 0) {2878fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,2879dayOfWeek);2880} else {2881// Go to the first day of the next week of2882// the specified week boundary.2883int lastDate = monthLength(month, year) + (7 * (dowim + 1));2884// Then, get the day of week date on or before the last date.2885fixedDate = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,2886dayOfWeek);2887}2888}2889}2890} else {2891if (year == gregorianCutoverYear && cal == gcal2892&& fixedDate < gregorianCutoverDate2893&& gregorianCutoverYear != gregorianCutoverYearJulian) {2894// January 1 of the year doesn't exist. Use2895// gregorianCutoverDate as the first day of the2896// year.2897fixedDate = gregorianCutoverDate;2898}2899// We are on the first day of the year.2900if (isFieldSet(fieldMask, DAY_OF_YEAR)) {2901// Add the offset, then subtract 1. (Make sure to avoid underflow.)2902fixedDate += internalGet(DAY_OF_YEAR);2903fixedDate--;2904} else {2905long firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,2906getFirstDayOfWeek());2907// If we have enough days in the first week, then move2908// to the previous week.2909if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {2910firstDayOfWeek -= 7;2911}2912if (isFieldSet(fieldMask, DAY_OF_WEEK)) {2913int dayOfWeek = internalGet(DAY_OF_WEEK);2914if (dayOfWeek != getFirstDayOfWeek()) {2915firstDayOfWeek = BaseCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,2916dayOfWeek);2917}2918}2919fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);2920}2921}29222923return fixedDate;2924}29252926/**2927* Returns this object if it's normalized (all fields and time are2928* in sync). Otherwise, a cloned object is returned after calling2929* complete() in lenient mode.2930*/2931private GregorianCalendar getNormalizedCalendar() {2932GregorianCalendar gc;2933if (isFullyNormalized()) {2934gc = this;2935} else {2936// Create a clone and normalize the calendar fields2937gc = (GregorianCalendar) this.clone();2938gc.setLenient(true);2939gc.complete();2940}2941return gc;2942}29432944/**2945* Returns the Julian calendar system instance (singleton). 'jcal'2946* and 'jeras' are set upon the return.2947*/2948private static synchronized BaseCalendar getJulianCalendarSystem() {2949if (jcal == null) {2950jcal = (JulianCalendar) CalendarSystem.forName("julian");2951jeras = jcal.getEras();2952}2953return jcal;2954}29552956/**2957* Returns the calendar system for dates before the cutover date2958* in the cutover year. If the cutover date is January 1, the2959* method returns Gregorian. Otherwise, Julian.2960*/2961private BaseCalendar getCutoverCalendarSystem() {2962if (gregorianCutoverYearJulian < gregorianCutoverYear) {2963return gcal;2964}2965return getJulianCalendarSystem();2966}29672968/**2969* Determines if the specified year (normalized) is the Gregorian2970* cutover year. This object must have been normalized.2971*/2972private boolean isCutoverYear(int normalizedYear) {2973int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;2974return normalizedYear == cutoverYear;2975}29762977/**2978* Returns the fixed date of the first day of the year (usually2979* January 1) before the specified date.2980*2981* @param date the date for which the first day of the year is2982* calculated. The date has to be in the cut-over year (Gregorian2983* or Julian).2984* @param fixedDate the fixed date representation of the date2985*/2986private long getFixedDateJan1(BaseCalendar.Date date, long fixedDate) {2987assert date.getNormalizedYear() == gregorianCutoverYear ||2988date.getNormalizedYear() == gregorianCutoverYearJulian;2989if (gregorianCutoverYear != gregorianCutoverYearJulian) {2990if (fixedDate >= gregorianCutoverDate) {2991// Dates before the cutover date don't exist2992// in the same (Gregorian) year. So, no2993// January 1 exists in the year. Use the2994// cutover date as the first day of the year.2995return gregorianCutoverDate;2996}2997}2998// January 1 of the normalized year should exist.2999BaseCalendar juliancal = getJulianCalendarSystem();3000return juliancal.getFixedDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1, null);3001}30023003/**3004* Returns the fixed date of the first date of the month (usually3005* the 1st of the month) before the specified date.3006*3007* @param date the date for which the first day of the month is3008* calculated. The date has to be in the cut-over year (Gregorian3009* or Julian).3010* @param fixedDate the fixed date representation of the date3011*/3012private long getFixedDateMonth1(BaseCalendar.Date date, long fixedDate) {3013assert date.getNormalizedYear() == gregorianCutoverYear ||3014date.getNormalizedYear() == gregorianCutoverYearJulian;3015BaseCalendar.Date gCutover = getGregorianCutoverDate();3016if (gCutover.getMonth() == BaseCalendar.JANUARY3017&& gCutover.getDayOfMonth() == 1) {3018// The cutover happened on January 1.3019return fixedDate - date.getDayOfMonth() + 1;3020}30213022long fixedDateMonth1;3023// The cutover happened sometime during the year.3024if (date.getMonth() == gCutover.getMonth()) {3025// The cutover happened in the month.3026BaseCalendar.Date jLastDate = getLastJulianDate();3027if (gregorianCutoverYear == gregorianCutoverYearJulian3028&& gCutover.getMonth() == jLastDate.getMonth()) {3029// The "gap" fits in the same month.3030fixedDateMonth1 = jcal.getFixedDate(date.getNormalizedYear(),3031date.getMonth(),30321,3033null);3034} else {3035// Use the cutover date as the first day of the month.3036fixedDateMonth1 = gregorianCutoverDate;3037}3038} else {3039// The cutover happened before the month.3040fixedDateMonth1 = fixedDate - date.getDayOfMonth() + 1;3041}30423043return fixedDateMonth1;3044}30453046/**3047* Returns a CalendarDate produced from the specified fixed date.3048*3049* @param fd the fixed date3050*/3051private BaseCalendar.Date getCalendarDate(long fd) {3052BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();3053BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);3054cal.getCalendarDateFromFixedDate(d, fd);3055return d;3056}30573058/**3059* Returns the Gregorian cutover date as a BaseCalendar.Date. The3060* date is a Gregorian date.3061*/3062private BaseCalendar.Date getGregorianCutoverDate() {3063return getCalendarDate(gregorianCutoverDate);3064}30653066/**3067* Returns the day before the Gregorian cutover date as a3068* BaseCalendar.Date. The date is a Julian date.3069*/3070private BaseCalendar.Date getLastJulianDate() {3071return getCalendarDate(gregorianCutoverDate - 1);3072}30733074/**3075* Returns the length of the specified month in the specified3076* year. The year number must be normalized.3077*3078* @see #isLeapYear(int)3079*/3080private int monthLength(int month, int year) {3081return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];3082}30833084/**3085* Returns the length of the specified month in the year provided3086* by internalGet(YEAR).3087*3088* @see #isLeapYear(int)3089*/3090private int monthLength(int month) {3091int year = internalGet(YEAR);3092if (internalGetEra() == BCE) {3093year = 1 - year;3094}3095return monthLength(month, year);3096}30973098private int actualMonthLength() {3099int year = cdate.getNormalizedYear();3100if (year != gregorianCutoverYear && year != gregorianCutoverYearJulian) {3101return calsys.getMonthLength(cdate);3102}3103BaseCalendar.Date date = (BaseCalendar.Date) cdate.clone();3104long fd = calsys.getFixedDate(date);3105long month1 = getFixedDateMonth1(date, fd);3106long next1 = month1 + calsys.getMonthLength(date);3107if (next1 < gregorianCutoverDate) {3108return (int)(next1 - month1);3109}3110if (cdate != gdate) {3111date = (BaseCalendar.Date) gcal.newCalendarDate(TimeZone.NO_TIMEZONE);3112}3113gcal.getCalendarDateFromFixedDate(date, next1);3114next1 = getFixedDateMonth1(date, next1);3115return (int)(next1 - month1);3116}31173118/**3119* Returns the length (in days) of the specified year. The year3120* must be normalized.3121*/3122private int yearLength(int year) {3123return isLeapYear(year) ? 366 : 365;3124}31253126/**3127* Returns the length (in days) of the year provided by3128* internalGet(YEAR).3129*/3130private int yearLength() {3131int year = internalGet(YEAR);3132if (internalGetEra() == BCE) {3133year = 1 - year;3134}3135return yearLength(year);3136}31373138/**3139* After adjustments such as add(MONTH), add(YEAR), we don't want the3140* month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar3141* 3, we want it to go to Feb 28. Adjustments which might run into this3142* problem call this method to retain the proper month.3143*/3144private void pinDayOfMonth() {3145int year = internalGet(YEAR);3146int monthLen;3147if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {3148monthLen = monthLength(internalGet(MONTH));3149} else {3150GregorianCalendar gc = getNormalizedCalendar();3151monthLen = gc.getActualMaximum(DAY_OF_MONTH);3152}3153int dom = internalGet(DAY_OF_MONTH);3154if (dom > monthLen) {3155set(DAY_OF_MONTH, monthLen);3156}3157}31583159/**3160* Returns the fixed date value of this object. The time value and3161* calendar fields must be in synch.3162*/3163private long getCurrentFixedDate() {3164return (calsys == gcal) ? cachedFixedDate : calsys.getFixedDate(cdate);3165}31663167/**3168* Returns the new value after 'roll'ing the specified value and amount.3169*/3170private static int getRolledValue(int value, int amount, int min, int max) {3171assert value >= min && value <= max;3172int range = max - min + 1;3173amount %= range;3174int n = value + amount;3175if (n > max) {3176n -= range;3177} else if (n < min) {3178n += range;3179}3180assert n >= min && n <= max;3181return n;3182}31833184/**3185* Returns the ERA. We need a special method for this because the3186* default ERA is CE, but a zero (unset) ERA is BCE.3187*/3188private int internalGetEra() {3189return isSet(ERA) ? internalGet(ERA) : CE;3190}31913192/**3193* Updates internal state.3194*/3195@java.io.Serial3196private void readObject(ObjectInputStream stream)3197throws IOException, ClassNotFoundException {3198stream.defaultReadObject();3199if (gdate == null) {3200gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());3201cachedFixedDate = Long.MIN_VALUE;3202}3203setGregorianChange(gregorianCutover);3204}32053206/**3207* Converts this object to a {@code ZonedDateTime} that represents3208* the same point on the time-line as this {@code GregorianCalendar}.3209* <p>3210* Since this object supports a Julian-Gregorian cutover date and3211* {@code ZonedDateTime} does not, it is possible that the resulting year,3212* month and day will have different values. The result will represent the3213* correct date in the ISO calendar system, which will also be the same value3214* for Modified Julian Days.3215*3216* @return a zoned date-time representing the same point on the time-line3217* as this gregorian calendar3218* @since 1.83219*/3220public ZonedDateTime toZonedDateTime() {3221return ZonedDateTime.ofInstant(Instant.ofEpochMilli(getTimeInMillis()),3222getTimeZone().toZoneId());3223}32243225/**3226* Obtains an instance of {@code GregorianCalendar} with the default locale3227* from a {@code ZonedDateTime} object.3228* <p>3229* Since {@code ZonedDateTime} does not support a Julian-Gregorian cutover3230* date and uses ISO calendar system, the return GregorianCalendar is a pure3231* Gregorian calendar and uses ISO 8601 standard for week definitions,3232* which has {@code MONDAY} as the {@link Calendar#getFirstDayOfWeek()3233* FirstDayOfWeek} and {@code 4} as the value of the3234* {@link Calendar#getMinimalDaysInFirstWeek() MinimalDaysInFirstWeek}.3235* <p>3236* {@code ZoneDateTime} can store points on the time-line further in the3237* future and further in the past than {@code GregorianCalendar}. In this3238* scenario, this method will throw an {@code IllegalArgumentException}3239* exception.3240*3241* @param zdt the zoned date-time object to convert3242* @return the gregorian calendar representing the same point on the3243* time-line as the zoned date-time provided3244* @throws NullPointerException if {@code zdt} is null3245* @throws IllegalArgumentException if the zoned date-time is too3246* large to represent as a {@code GregorianCalendar}3247* @since 1.83248*/3249public static GregorianCalendar from(ZonedDateTime zdt) {3250GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone(zdt.getZone()));3251cal.setGregorianChange(new Date(Long.MIN_VALUE));3252cal.setFirstDayOfWeek(MONDAY);3253cal.setMinimalDaysInFirstWeek(4);3254try {3255cal.setTimeInMillis(Math.addExact(Math.multiplyExact(zdt.toEpochSecond(), 1000),3256zdt.get(ChronoField.MILLI_OF_SECOND)));3257} catch (ArithmeticException ex) {3258throw new IllegalArgumentException(ex);3259}3260return cal;3261}3262}326332643265