0N/A/*
3624N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996-1998 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is copyrighted
0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
0N/A * materials are provided under terms of a License Agreement between Taligent
0N/A * and Sun. This technology is protected by multiple US and International
0N/A * patents. This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport sun.util.calendar.BaseCalendar;
0N/Aimport sun.util.calendar.CalendarDate;
0N/Aimport sun.util.calendar.CalendarSystem;
0N/Aimport sun.util.calendar.CalendarUtils;
0N/Aimport sun.util.calendar.Era;
0N/Aimport sun.util.calendar.Gregorian;
0N/Aimport sun.util.calendar.JulianCalendar;
0N/Aimport sun.util.calendar.ZoneInfo;
0N/A
0N/A/**
0N/A * <code>GregorianCalendar</code> is a concrete subclass of
0N/A * <code>Calendar</code> and provides the standard calendar system
0N/A * used by most of the world.
0N/A *
0N/A * <p> <code>GregorianCalendar</code> is a hybrid calendar that
0N/A * supports both the Julian and Gregorian calendar systems with the
0N/A * support of a single discontinuity, which corresponds by default to
0N/A * the Gregorian date when the Gregorian calendar was instituted
0N/A * (October 15, 1582 in some countries, later in others). The cutover
0N/A * date may be changed by the caller by calling {@link
0N/A * #setGregorianChange(Date) setGregorianChange()}.
0N/A *
0N/A * <p>
0N/A * Historically, in those countries which adopted the Gregorian calendar first,
0N/A * October 4, 1582 (Julian) was thus followed by October 15, 1582 (Gregorian). This calendar models
0N/A * this correctly. Before the Gregorian cutover, <code>GregorianCalendar</code>
0N/A * implements the Julian calendar. The only difference between the Gregorian
0N/A * and the Julian calendar is the leap year rule. The Julian calendar specifies
0N/A * leap years every four years, whereas the Gregorian calendar omits century
0N/A * years which are not divisible by 400.
0N/A *
0N/A * <p>
0N/A * <code>GregorianCalendar</code> implements <em>proleptic</em> Gregorian and
0N/A * Julian calendars. That is, dates are computed by extrapolating the current
0N/A * rules indefinitely far backward and forward in time. As a result,
0N/A * <code>GregorianCalendar</code> may be used for all years to generate
0N/A * meaningful and consistent results. However, dates obtained using
0N/A * <code>GregorianCalendar</code> are historically accurate only from March 1, 4
0N/A * AD onward, when modern Julian calendar rules were adopted. Before this date,
0N/A * leap year rules were applied irregularly, and before 45 BC the Julian
0N/A * calendar did not even exist.
0N/A *
0N/A * <p>
0N/A * Prior to the institution of the Gregorian calendar, New Year's Day was
0N/A * March 25. To avoid confusion, this calendar always uses January 1. A manual
0N/A * adjustment may be made if desired for dates that are prior to the Gregorian
0N/A * changeover and which fall between January 1 and March 24.
0N/A *
2702N/A * <h4><a name="week_and_year">Week Of Year and Week Year</a></h4>
2702N/A *
2702N/A * <p>Values calculated for the {@link Calendar#WEEK_OF_YEAR
2702N/A * WEEK_OF_YEAR} field range from 1 to 53. The first week of a
2702N/A * calendar year is the earliest seven day period starting on {@link
2702N/A * Calendar#getFirstDayOfWeek() getFirstDayOfWeek()} that contains at
2702N/A * least {@link Calendar#getMinimalDaysInFirstWeek()
2702N/A * getMinimalDaysInFirstWeek()} days from that year. It thus depends
2702N/A * on the values of {@code getMinimalDaysInFirstWeek()}, {@code
2702N/A * getFirstDayOfWeek()}, and the day of the week of January 1. Weeks
2702N/A * between week 1 of one year and week 1 of the following year
2702N/A * (exclusive) are numbered sequentially from 2 to 52 or 53 (except
2702N/A * for year(s) involved in the Julian-Gregorian transition).
2702N/A *
2702N/A * <p>The {@code getFirstDayOfWeek()} and {@code
2702N/A * getMinimalDaysInFirstWeek()} values are initialized using
2702N/A * locale-dependent resources when constructing a {@code
2702N/A * GregorianCalendar}. <a name="iso8601_compatible_setting">The week
2702N/A * determination is compatible</a> with the ISO 8601 standard when {@code
2702N/A * getFirstDayOfWeek()} is {@code MONDAY} and {@code
2702N/A * getMinimalDaysInFirstWeek()} is 4, which values are used in locales
2702N/A * where the standard is preferred. These values can explicitly be set by
2702N/A * calling {@link Calendar#setFirstDayOfWeek(int) setFirstDayOfWeek()} and
2702N/A * {@link Calendar#setMinimalDaysInFirstWeek(int)
2702N/A * setMinimalDaysInFirstWeek()}.
2702N/A *
2702N/A * <p>A <a name="week_year"><em>week year</em></a> is in sync with a
2702N/A * {@code WEEK_OF_YEAR} cycle. All weeks between the first and last
2702N/A * weeks (inclusive) have the same <em>week year</em> value.
2702N/A * Therefore, the first and last days of a week year may have
2702N/A * different calendar year values.
2702N/A *
2702N/A * <p>For example, January 1, 1998 is a Thursday. If {@code
2702N/A * getFirstDayOfWeek()} is {@code MONDAY} and {@code
2702N/A * getMinimalDaysInFirstWeek()} is 4 (ISO 8601 standard compatible
2702N/A * setting), then week 1 of 1998 starts on December 29, 1997, and ends
2702N/A * on January 4, 1998. The week year is 1998 for the last three days
2702N/A * of calendar year 1997. If, however, {@code getFirstDayOfWeek()} is
2702N/A * {@code SUNDAY}, then week 1 of 1998 starts on January 4, 1998, and
2702N/A * ends on January 10, 1998; the first three days of 1998 then are
2702N/A * part of week 53 of 1997 and their week year is 1997.
2702N/A *
2702N/A * <h4>Week Of Month</h4>
0N/A *
0N/A * <p>Values calculated for the <code>WEEK_OF_MONTH</code> field range from 0
0N/A * to 6. Week 1 of a month (the days with <code>WEEK_OF_MONTH =
0N/A * 1</code>) is the earliest set of at least
0N/A * <code>getMinimalDaysInFirstWeek()</code> contiguous days in that month,
0N/A * ending on the day before <code>getFirstDayOfWeek()</code>. Unlike
0N/A * week 1 of a year, week 1 of a month may be shorter than 7 days, need
0N/A * not start on <code>getFirstDayOfWeek()</code>, and will not include days of
0N/A * the previous month. Days of a month before week 1 have a
0N/A * <code>WEEK_OF_MONTH</code> of 0.
0N/A *
0N/A * <p>For example, if <code>getFirstDayOfWeek()</code> is <code>SUNDAY</code>
0N/A * and <code>getMinimalDaysInFirstWeek()</code> is 4, then the first week of
0N/A * January 1998 is Sunday, January 4 through Saturday, January 10. These days
0N/A * have a <code>WEEK_OF_MONTH</code> of 1. Thursday, January 1 through
0N/A * Saturday, January 3 have a <code>WEEK_OF_MONTH</code> of 0. If
0N/A * <code>getMinimalDaysInFirstWeek()</code> is changed to 3, then January 1
0N/A * through January 3 have a <code>WEEK_OF_MONTH</code> of 1.
0N/A *
2702N/A * <h4>Default Fields Values</h4>
2702N/A *
2702N/A * <p>The <code>clear</code> method sets calendar field(s)
0N/A * undefined. <code>GregorianCalendar</code> uses the following
0N/A * default value for each calendar field if its value is undefined.
0N/A *
0N/A * <table cellpadding="0" cellspacing="3" border="0"
0N/A * summary="GregorianCalendar default field values"
0N/A * style="text-align: left; width: 66%;">
0N/A * <tbody>
0N/A * <tr>
0N/A * <th style="vertical-align: top; background-color: rgb(204, 204, 255);
0N/A * text-align: center;">Field<br>
0N/A * </th>
0N/A * <th style="vertical-align: top; background-color: rgb(204, 204, 255);
0N/A * text-align: center;">Default Value<br>
0N/A * </th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>ERA<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>AD<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
0N/A * <code>YEAR<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
0N/A * <code>1970<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>MONTH<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>JANUARY<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
0N/A * <code>DAY_OF_MONTH<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
0N/A * <code>1<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>DAY_OF_WEEK<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>the first day of week<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
0N/A * <code>WEEK_OF_MONTH<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: top; background-color: rgb(238, 238, 255);">
0N/A * <code>0<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: top;">
0N/A * <code>DAY_OF_WEEK_IN_MONTH<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: top;">
0N/A * <code>1<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
0N/A * <code>AM_PM<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle; background-color: rgb(238, 238, 255);">
0N/A * <code>AM<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND<br></code>
0N/A * </td>
0N/A * <td style="vertical-align: middle;">
0N/A * <code>0<br></code>
0N/A * </td>
0N/A * </tr>
0N/A * </tbody>
0N/A * </table>
0N/A * <br>Default values are not applicable for the fields not listed above.
0N/A *
0N/A * <p>
0N/A * <strong>Example:</strong>
0N/A * <blockquote>
0N/A * <pre>
0N/A * // get the supported ids for GMT-08:00 (Pacific Standard Time)
0N/A * String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
0N/A * // if no ids were returned, something is wrong. get out.
0N/A * if (ids.length == 0)
0N/A * System.exit(0);
0N/A *
0N/A * // begin output
0N/A * System.out.println("Current Time");
0N/A *
0N/A * // create a Pacific Standard Time time zone
0N/A * SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
0N/A *
3624N/A * // set up rules for Daylight Saving Time
0N/A * pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
0N/A * pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
0N/A *
0N/A * // create a GregorianCalendar with the Pacific Daylight time zone
0N/A * // and the current date and time
0N/A * Calendar calendar = new GregorianCalendar(pdt);
0N/A * Date trialTime = new Date();
0N/A * calendar.setTime(trialTime);
0N/A *
0N/A * // print out a bunch of interesting things
0N/A * System.out.println("ERA: " + calendar.get(Calendar.ERA));
0N/A * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
0N/A * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
0N/A * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
0N/A * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
0N/A * System.out.println("DATE: " + calendar.get(Calendar.DATE));
0N/A * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
0N/A * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
0N/A * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
0N/A * System.out.println("DAY_OF_WEEK_IN_MONTH: "
0N/A * + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
0N/A * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
0N/A * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
0N/A * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
0N/A * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
0N/A * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
0N/A * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
0N/A * System.out.println("ZONE_OFFSET: "
0N/A * + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
0N/A * System.out.println("DST_OFFSET: "
0N/A * + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));
0N/A
0N/A * System.out.println("Current Time, with hour reset to 3");
0N/A * calendar.clear(Calendar.HOUR_OF_DAY); // so doesn't override
0N/A * calendar.set(Calendar.HOUR, 3);
0N/A * System.out.println("ERA: " + calendar.get(Calendar.ERA));
0N/A * System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
0N/A * System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
0N/A * System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
0N/A * System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
0N/A * System.out.println("DATE: " + calendar.get(Calendar.DATE));
0N/A * System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
0N/A * System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
0N/A * System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
0N/A * System.out.println("DAY_OF_WEEK_IN_MONTH: "
0N/A * + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
0N/A * System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
0N/A * System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
0N/A * System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
0N/A * System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
0N/A * System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
0N/A * System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
0N/A * System.out.println("ZONE_OFFSET: "
0N/A * + (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000))); // in hours
0N/A * System.out.println("DST_OFFSET: "
0N/A * + (calendar.get(Calendar.DST_OFFSET)/(60*60*1000))); // in hours
0N/A * </pre>
0N/A * </blockquote>
0N/A *
0N/A * @see TimeZone
0N/A * @author David Goldsmith, Mark Davis, Chen-Lieh Huang, Alan Liu
0N/A * @since JDK1.1
0N/A */
0N/Apublic class GregorianCalendar extends Calendar {
0N/A /*
0N/A * Implementation Notes
0N/A *
0N/A * The epoch is the number of days or milliseconds from some defined
0N/A * starting point. The epoch for java.util.Date is used here; that is,
0N/A * milliseconds from January 1, 1970 (Gregorian), midnight UTC. Other
0N/A * epochs which are used are January 1, year 1 (Gregorian), which is day 1
0N/A * of the Gregorian calendar, and December 30, year 0 (Gregorian), which is
0N/A * day 1 of the Julian calendar.
0N/A *
0N/A * We implement the proleptic Julian and Gregorian calendars. This means we
0N/A * implement the modern definition of the calendar even though the
0N/A * historical usage differs. For example, if the Gregorian change is set
0N/A * to new Date(Long.MIN_VALUE), we have a pure Gregorian calendar which
0N/A * labels dates preceding the invention of the Gregorian calendar in 1582 as
0N/A * if the calendar existed then.
0N/A *
0N/A * Likewise, with the Julian calendar, we assume a consistent
0N/A * 4-year leap year rule, even though the historical pattern of
0N/A * leap years is irregular, being every 3 years from 45 BCE
0N/A * through 9 BCE, then every 4 years from 8 CE onwards, with no
0N/A * leap years in-between. Thus date computations and functions
0N/A * such as isLeapYear() are not intended to be historically
0N/A * accurate.
0N/A */
0N/A
0N/A//////////////////
0N/A// Class Variables
0N/A//////////////////
0N/A
0N/A /**
0N/A * Value of the <code>ERA</code> field indicating
0N/A * the period before the common era (before Christ), also known as BCE.
0N/A * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
0N/A * ..., 2 BC, 1 BC, 1 AD, 2 AD,...
0N/A *
0N/A * @see #ERA
0N/A */
0N/A public static final int BC = 0;
0N/A
0N/A /**
0N/A * Value of the {@link #ERA} field indicating
0N/A * the period before the common era, the same value as {@link #BC}.
0N/A *
0N/A * @see #CE
0N/A */
0N/A static final int BCE = 0;
0N/A
0N/A /**
0N/A * Value of the <code>ERA</code> field indicating
0N/A * the common era (Anno Domini), also known as CE.
0N/A * The sequence of years at the transition from <code>BC</code> to <code>AD</code> is
0N/A * ..., 2 BC, 1 BC, 1 AD, 2 AD,...
0N/A *
0N/A * @see #ERA
0N/A */
0N/A public static final int AD = 1;
0N/A
0N/A /**
0N/A * Value of the {@link #ERA} field indicating
0N/A * the common era, the same value as {@link #AD}.
0N/A *
0N/A * @see #BCE
0N/A */
0N/A static final int CE = 1;
0N/A
0N/A private static final int EPOCH_OFFSET = 719163; // Fixed date of January 1, 1970 (Gregorian)
0N/A private static final int EPOCH_YEAR = 1970;
0N/A
0N/A static final int MONTH_LENGTH[]
0N/A = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
0N/A static final int LEAP_MONTH_LENGTH[]
0N/A = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
0N/A
0N/A // Useful millisecond constants. Although ONE_DAY and ONE_WEEK can fit
0N/A // into ints, they must be longs in order to prevent arithmetic overflow
0N/A // when performing (bug 4173516).
0N/A private static final int ONE_SECOND = 1000;
0N/A private static final int ONE_MINUTE = 60*ONE_SECOND;
0N/A private static final int ONE_HOUR = 60*ONE_MINUTE;
0N/A private static final long ONE_DAY = 24*ONE_HOUR;
0N/A private static final long ONE_WEEK = 7*ONE_DAY;
0N/A
0N/A /*
0N/A * <pre>
0N/A * Greatest Least
0N/A * Field name Minimum Minimum Maximum Maximum
0N/A * ---------- ------- ------- ------- -------
0N/A * ERA 0 0 1 1
0N/A * YEAR 1 1 292269054 292278994
0N/A * MONTH 0 0 11 11
0N/A * WEEK_OF_YEAR 1 1 52* 53
0N/A * WEEK_OF_MONTH 0 0 4* 6
0N/A * DAY_OF_MONTH 1 1 28* 31
0N/A * DAY_OF_YEAR 1 1 365* 366
0N/A * DAY_OF_WEEK 1 1 7 7
0N/A * DAY_OF_WEEK_IN_MONTH -1 -1 4* 6
0N/A * AM_PM 0 0 1 1
0N/A * HOUR 0 0 11 11
0N/A * HOUR_OF_DAY 0 0 23 23
0N/A * MINUTE 0 0 59 59
0N/A * SECOND 0 0 59 59
0N/A * MILLISECOND 0 0 999 999
0N/A * ZONE_OFFSET -13:00 -13:00 14:00 14:00
0N/A * DST_OFFSET 0:00 0:00 0:20 2:00
0N/A * </pre>
0N/A * *: depends on the Gregorian change date
0N/A */
0N/A static final int MIN_VALUES[] = {
0N/A BCE, // ERA
0N/A 1, // YEAR
0N/A JANUARY, // MONTH
0N/A 1, // WEEK_OF_YEAR
0N/A 0, // WEEK_OF_MONTH
0N/A 1, // DAY_OF_MONTH
0N/A 1, // DAY_OF_YEAR
0N/A SUNDAY, // DAY_OF_WEEK
0N/A 1, // DAY_OF_WEEK_IN_MONTH
0N/A AM, // AM_PM
0N/A 0, // HOUR
0N/A 0, // HOUR_OF_DAY
0N/A 0, // MINUTE
0N/A 0, // SECOND
0N/A 0, // MILLISECOND
0N/A -13*ONE_HOUR, // ZONE_OFFSET (UNIX compatibility)
0N/A 0 // DST_OFFSET
0N/A };
0N/A static final int LEAST_MAX_VALUES[] = {
0N/A CE, // ERA
0N/A 292269054, // YEAR
0N/A DECEMBER, // MONTH
0N/A 52, // WEEK_OF_YEAR
0N/A 4, // WEEK_OF_MONTH
0N/A 28, // DAY_OF_MONTH
0N/A 365, // DAY_OF_YEAR
0N/A SATURDAY, // DAY_OF_WEEK
0N/A 4, // DAY_OF_WEEK_IN
0N/A PM, // AM_PM
0N/A 11, // HOUR
0N/A 23, // HOUR_OF_DAY
0N/A 59, // MINUTE
0N/A 59, // SECOND
0N/A 999, // MILLISECOND
0N/A 14*ONE_HOUR, // ZONE_OFFSET
0N/A 20*ONE_MINUTE // DST_OFFSET (historical least maximum)
0N/A };
0N/A static final int MAX_VALUES[] = {
0N/A CE, // ERA
0N/A 292278994, // YEAR
0N/A DECEMBER, // MONTH
0N/A 53, // WEEK_OF_YEAR
0N/A 6, // WEEK_OF_MONTH
0N/A 31, // DAY_OF_MONTH
0N/A 366, // DAY_OF_YEAR
0N/A SATURDAY, // DAY_OF_WEEK
0N/A 6, // DAY_OF_WEEK_IN
0N/A PM, // AM_PM
0N/A 11, // HOUR
0N/A 23, // HOUR_OF_DAY
0N/A 59, // MINUTE
0N/A 59, // SECOND
0N/A 999, // MILLISECOND
0N/A 14*ONE_HOUR, // ZONE_OFFSET
0N/A 2*ONE_HOUR // DST_OFFSET (double summer time)
0N/A };
0N/A
0N/A // Proclaim serialization compatibility with JDK 1.1
0N/A static final long serialVersionUID = -8125100834729963327L;
0N/A
0N/A // Reference to the sun.util.calendar.Gregorian instance (singleton).
0N/A private static final Gregorian gcal =
0N/A CalendarSystem.getGregorianCalendar();
0N/A
0N/A // Reference to the JulianCalendar instance (singleton), set as needed. See
0N/A // getJulianCalendarSystem().
0N/A private static JulianCalendar jcal;
0N/A
0N/A // JulianCalendar eras. See getJulianCalendarSystem().
0N/A private static Era[] jeras;
0N/A
0N/A // The default value of gregorianCutover.
0N/A static final long DEFAULT_GREGORIAN_CUTOVER = -12219292800000L;
0N/A
0N/A/////////////////////
0N/A// Instance Variables
0N/A/////////////////////
0N/A
0N/A /**
0N/A * The point at which the Gregorian calendar rules are used, measured in
0N/A * milliseconds from the standard epoch. Default is October 15, 1582
0N/A * (Gregorian) 00:00:00 UTC or -12219292800000L. For this value, October 4,
0N/A * 1582 (Julian) is followed by October 15, 1582 (Gregorian). This
0N/A * corresponds to Julian day number 2299161.
0N/A * @serial
0N/A */
0N/A private long gregorianCutover = DEFAULT_GREGORIAN_CUTOVER;
0N/A
0N/A /**
0N/A * The fixed date of the gregorianCutover.
0N/A */
0N/A private transient long gregorianCutoverDate =
0N/A (((DEFAULT_GREGORIAN_CUTOVER + 1)/ONE_DAY) - 1) + EPOCH_OFFSET; // == 577736
0N/A
0N/A /**
0N/A * The normalized year of the gregorianCutover in Gregorian, with
0N/A * 0 representing 1 BCE, -1 representing 2 BCE, etc.
0N/A */
0N/A private transient int gregorianCutoverYear = 1582;
0N/A
0N/A /**
0N/A * The normalized year of the gregorianCutover in Julian, with 0
0N/A * representing 1 BCE, -1 representing 2 BCE, etc.
0N/A */
0N/A private transient int gregorianCutoverYearJulian = 1582;
0N/A
0N/A /**
0N/A * gdate always has a sun.util.calendar.Gregorian.Date instance to
0N/A * avoid overhead of creating it. The assumption is that most
0N/A * applications will need only Gregorian calendar calculations.
0N/A */
0N/A private transient BaseCalendar.Date gdate;
0N/A
0N/A /**
0N/A * Reference to either gdate or a JulianCalendar.Date
0N/A * instance. After calling complete(), this value is guaranteed to
0N/A * be set.
0N/A */
0N/A private transient BaseCalendar.Date cdate;
0N/A
0N/A /**
0N/A * The CalendarSystem used to calculate the date in cdate. After
0N/A * calling complete(), this value is guaranteed to be set and
0N/A * consistent with the cdate value.
0N/A */
0N/A private transient BaseCalendar calsys;
0N/A
0N/A /**
0N/A * Temporary int[2] to get time zone offsets. zoneOffsets[0] gets
0N/A * the GMT offset value and zoneOffsets[1] gets the DST saving
0N/A * value.
0N/A */
0N/A private transient int[] zoneOffsets;
0N/A
0N/A /**
0N/A * Temporary storage for saving original fields[] values in
0N/A * non-lenient mode.
0N/A */
0N/A private transient int[] originalFields;
0N/A
0N/A///////////////
0N/A// Constructors
0N/A///////////////
0N/A
0N/A /**
0N/A * Constructs a default <code>GregorianCalendar</code> using the current time
0N/A * in the default time zone with the default locale.
0N/A */
0N/A public GregorianCalendar() {
2700N/A this(TimeZone.getDefaultRef(), Locale.getDefault(Locale.Category.FORMAT));
0N/A setZoneShared(true);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> based on the current time
0N/A * in the given time zone with the default locale.
0N/A *
0N/A * @param zone the given time zone.
0N/A */
0N/A public GregorianCalendar(TimeZone zone) {
2700N/A this(zone, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> based on the current time
0N/A * in the default time zone with the given locale.
0N/A *
0N/A * @param aLocale the given locale.
0N/A */
0N/A public GregorianCalendar(Locale aLocale) {
0N/A this(TimeZone.getDefaultRef(), aLocale);
0N/A setZoneShared(true);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> based on the current time
0N/A * in the given time zone with the given locale.
0N/A *
0N/A * @param zone the given time zone.
0N/A * @param aLocale the given locale.
0N/A */
0N/A public GregorianCalendar(TimeZone zone, Locale aLocale) {
0N/A super(zone, aLocale);
0N/A gdate = (BaseCalendar.Date) gcal.newCalendarDate(zone);
0N/A setTimeInMillis(System.currentTimeMillis());
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> with the given date set
0N/A * in the default time zone with the default locale.
0N/A *
0N/A * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
0N/A * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
0N/A * Month value is 0-based. e.g., 0 for January.
0N/A * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
0N/A */
0N/A public GregorianCalendar(int year, int month, int dayOfMonth) {
0N/A this(year, month, dayOfMonth, 0, 0, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> with the given date
0N/A * and time set for the default time zone with the default locale.
0N/A *
0N/A * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
0N/A * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
0N/A * Month value is 0-based. e.g., 0 for January.
0N/A * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
0N/A * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
0N/A * in the calendar.
0N/A * @param minute the value used to set the <code>MINUTE</code> calendar field
0N/A * in the calendar.
0N/A */
0N/A public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
0N/A int minute) {
0N/A this(year, month, dayOfMonth, hourOfDay, minute, 0, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a GregorianCalendar with the given date
0N/A * and time set for the default time zone with the default locale.
0N/A *
0N/A * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
0N/A * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
0N/A * Month value is 0-based. e.g., 0 for January.
0N/A * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
0N/A * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
0N/A * in the calendar.
0N/A * @param minute the value used to set the <code>MINUTE</code> calendar field
0N/A * in the calendar.
0N/A * @param second the value used to set the <code>SECOND</code> calendar field
0N/A * in the calendar.
0N/A */
0N/A public GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay,
0N/A int minute, int second) {
0N/A this(year, month, dayOfMonth, hourOfDay, minute, second, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>GregorianCalendar</code> with the given date
0N/A * and time set for the default time zone with the default locale.
0N/A *
0N/A * @param year the value used to set the <code>YEAR</code> calendar field in the calendar.
0N/A * @param month the value used to set the <code>MONTH</code> calendar field in the calendar.
0N/A * Month value is 0-based. e.g., 0 for January.
0N/A * @param dayOfMonth the value used to set the <code>DAY_OF_MONTH</code> calendar field in the calendar.
0N/A * @param hourOfDay the value used to set the <code>HOUR_OF_DAY</code> calendar field
0N/A * in the calendar.
0N/A * @param minute the value used to set the <code>MINUTE</code> calendar field
0N/A * in the calendar.
0N/A * @param second the value used to set the <code>SECOND</code> calendar field
0N/A * in the calendar.
0N/A * @param millis the value used to set the <code>MILLISECOND</code> calendar field
0N/A */
0N/A GregorianCalendar(int year, int month, int dayOfMonth,
0N/A int hourOfDay, int minute, int second, int millis) {
0N/A super();
0N/A gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
0N/A this.set(YEAR, year);
0N/A this.set(MONTH, month);
0N/A this.set(DAY_OF_MONTH, dayOfMonth);
0N/A
0N/A // Set AM_PM and HOUR here to set their stamp values before
0N/A // setting HOUR_OF_DAY (6178071).
0N/A if (hourOfDay >= 12 && hourOfDay <= 23) {
0N/A // If hourOfDay is a valid PM hour, set the correct PM values
0N/A // so that it won't throw an exception in case it's set to
0N/A // non-lenient later.
0N/A this.internalSet(AM_PM, PM);
0N/A this.internalSet(HOUR, hourOfDay - 12);
0N/A } else {
0N/A // The default value for AM_PM is AM.
0N/A // We don't care any out of range value here for leniency.
0N/A this.internalSet(HOUR, hourOfDay);
0N/A }
0N/A // The stamp values of AM_PM and HOUR must be COMPUTED. (6440854)
0N/A setFieldsComputed(HOUR_MASK|AM_PM_MASK);
0N/A
0N/A this.set(HOUR_OF_DAY, hourOfDay);
0N/A this.set(MINUTE, minute);
0N/A this.set(SECOND, second);
0N/A // should be changed to set() when this constructor is made
0N/A // public.
0N/A this.internalSet(MILLISECOND, millis);
0N/A }
0N/A
0N/A/////////////////
0N/A// Public methods
0N/A/////////////////
0N/A
0N/A /**
0N/A * Sets the <code>GregorianCalendar</code> change date. This is the point when the switch
0N/A * from Julian dates to Gregorian dates occurred. Default is October 15,
0N/A * 1582 (Gregorian). Previous to this, dates will be in the Julian calendar.
0N/A * <p>
0N/A * To obtain a pure Julian calendar, set the change date to
0N/A * <code>Date(Long.MAX_VALUE)</code>. To obtain a pure Gregorian calendar,
0N/A * set the change date to <code>Date(Long.MIN_VALUE)</code>.
0N/A *
0N/A * @param date the given Gregorian cutover date.
0N/A */
0N/A public void setGregorianChange(Date date) {
0N/A long cutoverTime = date.getTime();
0N/A if (cutoverTime == gregorianCutover) {
0N/A return;
0N/A }
0N/A // Before changing the cutover date, make sure to have the
0N/A // time of this calendar.
0N/A complete();
0N/A setGregorianChange(cutoverTime);
0N/A }
0N/A
0N/A private void setGregorianChange(long cutoverTime) {
0N/A gregorianCutover = cutoverTime;
0N/A gregorianCutoverDate = CalendarUtils.floorDivide(cutoverTime, ONE_DAY)
0N/A + EPOCH_OFFSET;
0N/A
0N/A // To provide the "pure" Julian calendar as advertised.
0N/A // Strictly speaking, the last millisecond should be a
0N/A // Gregorian date. However, the API doc specifies that setting
0N/A // the cutover date to Long.MAX_VALUE will make this calendar
0N/A // a pure Julian calendar. (See 4167995)
0N/A if (cutoverTime == Long.MAX_VALUE) {
0N/A gregorianCutoverDate++;
0N/A }
0N/A
0N/A BaseCalendar.Date d = getGregorianCutoverDate();
0N/A
0N/A // Set the cutover year (in the Gregorian year numbering)
0N/A gregorianCutoverYear = d.getYear();
0N/A
0N/A BaseCalendar jcal = getJulianCalendarSystem();
0N/A d = (BaseCalendar.Date) jcal.newCalendarDate(TimeZone.NO_TIMEZONE);
0N/A jcal.getCalendarDateFromFixedDate(d, gregorianCutoverDate - 1);
0N/A gregorianCutoverYearJulian = d.getNormalizedYear();
0N/A
0N/A if (time < gregorianCutover) {
0N/A // The field values are no longer valid under the new
0N/A // cutover date.
0N/A setUnnormalized();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the Gregorian Calendar change date. This is the point when the
0N/A * switch from Julian dates to Gregorian dates occurred. Default is
0N/A * October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian
0N/A * calendar.
0N/A *
0N/A * @return the Gregorian cutover date for this <code>GregorianCalendar</code> object.
0N/A */
0N/A public final Date getGregorianChange() {
0N/A return new Date(gregorianCutover);
0N/A }
0N/A
0N/A /**
0N/A * Determines if the given year is a leap year. Returns <code>true</code> if
0N/A * the given year is a leap year. To specify BC year numbers,
0N/A * <code>1 - year number</code> must be given. For example, year BC 4 is
0N/A * specified as -3.
0N/A *
0N/A * @param year the given year.
0N/A * @return <code>true</code> if the given year is a leap year; <code>false</code> otherwise.
0N/A */
0N/A public boolean isLeapYear(int year) {
0N/A if ((year & 3) != 0) {
0N/A return false;
0N/A }
0N/A
0N/A if (year > gregorianCutoverYear) {
0N/A return (year%100 != 0) || (year%400 == 0); // Gregorian
0N/A }
0N/A if (year < gregorianCutoverYearJulian) {
0N/A return true; // Julian
0N/A }
0N/A boolean gregorian;
0N/A // If the given year is the Gregorian cutover year, we need to
0N/A // determine which calendar system to be applied to February in the year.
0N/A if (gregorianCutoverYear == gregorianCutoverYearJulian) {
0N/A BaseCalendar.Date d = getCalendarDate(gregorianCutoverDate); // Gregorian
0N/A gregorian = d.getMonth() < BaseCalendar.MARCH;
0N/A } else {
0N/A gregorian = year == gregorianCutoverYear;
0N/A }
0N/A return gregorian ? (year%100 != 0) || (year%400 == 0) : true;
0N/A }
0N/A
0N/A /**
0N/A * Compares this <code>GregorianCalendar</code> to the specified
0N/A * <code>Object</code>. The result is <code>true</code> if and
0N/A * only if the argument is a <code>GregorianCalendar</code> object
0N/A * that represents the same time value (millisecond offset from
0N/A * the <a href="Calendar.html#Epoch">Epoch</a>) under the same
0N/A * <code>Calendar</code> parameters and Gregorian change date as
0N/A * this object.
0N/A *
0N/A * @param obj the object to compare with.
0N/A * @return <code>true</code> if this object is equal to <code>obj</code>;
0N/A * <code>false</code> otherwise.
0N/A * @see Calendar#compareTo(Calendar)
0N/A */
0N/A public boolean equals(Object obj) {
0N/A return obj instanceof GregorianCalendar &&
0N/A super.equals(obj) &&
0N/A gregorianCutover == ((GregorianCalendar)obj).gregorianCutover;
0N/A }
0N/A
0N/A /**
0N/A * Generates the hash code for this <code>GregorianCalendar</code> object.
0N/A */
0N/A public int hashCode() {
0N/A return super.hashCode() ^ (int)gregorianCutoverDate;
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified (signed) amount of time to the given calendar field,
0N/A * based on the calendar's rules.
0N/A *
0N/A * <p><em>Add rule 1</em>. The value of <code>field</code>
0N/A * after the call minus the value of <code>field</code> before the
0N/A * call is <code>amount</code>, modulo any overflow that has occurred in
0N/A * <code>field</code>. Overflow occurs when a field value exceeds its
0N/A * range and, as a result, the next larger field is incremented or
0N/A * decremented and the field value is adjusted back into its range.</p>
0N/A *
0N/A * <p><em>Add rule 2</em>. If a smaller field is expected to be
0N/A * invariant, but it is impossible for it to be equal to its
0N/A * prior value because of changes in its minimum or maximum after
0N/A * <code>field</code> is changed, then its value is adjusted to be as close
0N/A * as possible to its expected value. A smaller field represents a
0N/A * smaller unit of time. <code>HOUR</code> is a smaller field than
0N/A * <code>DAY_OF_MONTH</code>. No adjustment is made to smaller fields
0N/A * that are not expected to be invariant. The calendar system
0N/A * determines what fields are expected to be invariant.</p>
0N/A *
0N/A * @param field the calendar field.
0N/A * @param amount the amount of date or time to be added to the field.
0N/A * @exception IllegalArgumentException if <code>field</code> is
0N/A * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
0N/A * or if any calendar fields have out-of-range values in
0N/A * non-lenient mode.
0N/A */
0N/A public void add(int field, int amount) {
0N/A // If amount == 0, do nothing even the given field is out of
0N/A // range. This is tested by JCK.
0N/A if (amount == 0) {
0N/A return; // Do nothing!
0N/A }
0N/A
0N/A if (field < 0 || field >= ZONE_OFFSET) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A // Sync the time and calendar fields.
0N/A complete();
0N/A
0N/A if (field == YEAR) {
0N/A int year = internalGet(YEAR);
0N/A if (internalGetEra() == CE) {
0N/A year += amount;
0N/A if (year > 0) {
0N/A set(YEAR, year);
0N/A } else { // year <= 0
0N/A set(YEAR, 1 - year);
0N/A // if year == 0, you get 1 BCE.
0N/A set(ERA, BCE);
0N/A }
0N/A }
0N/A else { // era == BCE
0N/A year -= amount;
0N/A if (year > 0) {
0N/A set(YEAR, year);
0N/A } else { // year <= 0
0N/A set(YEAR, 1 - year);
0N/A // if year == 0, you get 1 CE
0N/A set(ERA, CE);
0N/A }
0N/A }
0N/A pinDayOfMonth();
0N/A } else if (field == MONTH) {
0N/A int month = internalGet(MONTH) + amount;
0N/A int year = internalGet(YEAR);
0N/A int y_amount;
0N/A
0N/A if (month >= 0) {
0N/A y_amount = month/12;
0N/A } else {
0N/A y_amount = (month+1)/12 - 1;
0N/A }
0N/A if (y_amount != 0) {
0N/A if (internalGetEra() == CE) {
0N/A year += y_amount;
0N/A if (year > 0) {
0N/A set(YEAR, year);
0N/A } else { // year <= 0
0N/A set(YEAR, 1 - year);
0N/A // if year == 0, you get 1 BCE
0N/A set(ERA, BCE);
0N/A }
0N/A }
0N/A else { // era == BCE
0N/A year -= y_amount;
0N/A if (year > 0) {
0N/A set(YEAR, year);
0N/A } else { // year <= 0
0N/A set(YEAR, 1 - year);
0N/A // if year == 0, you get 1 CE
0N/A set(ERA, CE);
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (month >= 0) {
0N/A set(MONTH, (int) (month % 12));
0N/A } else {
0N/A // month < 0
0N/A month %= 12;
0N/A if (month < 0) {
0N/A month += 12;
0N/A }
0N/A set(MONTH, JANUARY + month);
0N/A }
0N/A pinDayOfMonth();
0N/A } else if (field == ERA) {
0N/A int era = internalGet(ERA) + amount;
0N/A if (era < 0) {
0N/A era = 0;
0N/A }
0N/A if (era > 1) {
0N/A era = 1;
0N/A }
0N/A set(ERA, era);
0N/A } else {
0N/A long delta = amount;
0N/A long timeOfDay = 0;
0N/A switch (field) {
0N/A // Handle the time fields here. Convert the given
0N/A // amount to milliseconds and call setTimeInMillis.
0N/A case HOUR:
0N/A case HOUR_OF_DAY:
0N/A delta *= 60 * 60 * 1000; // hours to minutes
0N/A break;
0N/A
0N/A case MINUTE:
0N/A delta *= 60 * 1000; // minutes to seconds
0N/A break;
0N/A
0N/A case SECOND:
0N/A delta *= 1000; // seconds to milliseconds
0N/A break;
0N/A
0N/A case MILLISECOND:
0N/A break;
0N/A
0N/A // Handle week, day and AM_PM fields which involves
0N/A // time zone offset change adjustment. Convert the
0N/A // given amount to the number of days.
0N/A case WEEK_OF_YEAR:
0N/A case WEEK_OF_MONTH:
0N/A case DAY_OF_WEEK_IN_MONTH:
0N/A delta *= 7;
0N/A break;
0N/A
0N/A case DAY_OF_MONTH: // synonym of DATE
0N/A case DAY_OF_YEAR:
0N/A case DAY_OF_WEEK:
0N/A break;
0N/A
0N/A case AM_PM:
0N/A // Convert the amount to the number of days (delta)
0N/A // and +12 or -12 hours (timeOfDay).
0N/A delta = amount / 2;
0N/A timeOfDay = 12 * (amount % 2);
0N/A break;
0N/A }
0N/A
0N/A // The time fields don't require time zone offset change
0N/A // adjustment.
0N/A if (field >= HOUR) {
0N/A setTimeInMillis(time + delta);
0N/A return;
0N/A }
0N/A
0N/A // The rest of the fields (week, day or AM_PM fields)
0N/A // require time zone offset (both GMT and DST) change
0N/A // adjustment.
0N/A
0N/A // Translate the current time to the fixed date and time
0N/A // of the day.
0N/A long fd = getCurrentFixedDate();
0N/A timeOfDay += internalGet(HOUR_OF_DAY);
0N/A timeOfDay *= 60;
0N/A timeOfDay += internalGet(MINUTE);
0N/A timeOfDay *= 60;
0N/A timeOfDay += internalGet(SECOND);
0N/A timeOfDay *= 1000;
0N/A timeOfDay += internalGet(MILLISECOND);
0N/A if (timeOfDay >= ONE_DAY) {
0N/A fd++;
0N/A timeOfDay -= ONE_DAY;
0N/A } else if (timeOfDay < 0) {
0N/A fd--;
0N/A timeOfDay += ONE_DAY;
0N/A }
0N/A
0N/A fd += delta; // fd is the expected fixed date after the calculation
0N/A int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
0N/A setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);
0N/A zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
0N/A // If the time zone offset has changed, then adjust the difference.
0N/A if (zoneOffset != 0) {
0N/A setTimeInMillis(time + zoneOffset);
0N/A long fd2 = getCurrentFixedDate();
0N/A // If the adjustment has changed the date, then take
0N/A // the previous one.
0N/A if (fd2 != fd) {
0N/A setTimeInMillis(time - zoneOffset);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds or subtracts (up/down) a single unit of time on the given time
0N/A * field without changing larger fields.
0N/A * <p>
0N/A * <em>Example</em>: Consider a <code>GregorianCalendar</code>
0N/A * originally set to December 31, 1999. Calling {@link #roll(int,boolean) roll(Calendar.MONTH, true)}
0N/A * sets the calendar to January 31, 1999. The <code>YEAR</code> field is unchanged
0N/A * because it is a larger field than <code>MONTH</code>.</p>
0N/A *
0N/A * @param up indicates if the value of the specified calendar field is to be
0N/A * rolled up or rolled down. Use <code>true</code> if rolling up, <code>false</code> otherwise.
0N/A * @exception IllegalArgumentException if <code>field</code> is
0N/A * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
0N/A * or if any calendar fields have out-of-range values in
0N/A * non-lenient mode.
0N/A * @see #add(int,int)
0N/A * @see #set(int,int)
0N/A */
0N/A public void roll(int field, boolean up) {
0N/A roll(field, up ? +1 : -1);
0N/A }
0N/A
0N/A /**
0N/A * Adds a signed amount to the specified calendar field without changing larger fields.
0N/A * A negative roll amount means to subtract from field without changing
0N/A * larger fields. If the specified amount is 0, this method performs nothing.
0N/A *
0N/A * <p>This method calls {@link #complete()} before adding the
0N/A * amount so that all the calendar fields are normalized. If there
0N/A * is any calendar field having an out-of-range value in non-lenient mode, then an
0N/A * <code>IllegalArgumentException</code> is thrown.
0N/A *
0N/A * <p>
0N/A * <em>Example</em>: Consider a <code>GregorianCalendar</code>
0N/A * originally set to August 31, 1999. Calling <code>roll(Calendar.MONTH,
0N/A * 8)</code> sets the calendar to April 30, <strong>1999</strong>. Using a
0N/A * <code>GregorianCalendar</code>, the <code>DAY_OF_MONTH</code> field cannot
0N/A * be 31 in the month April. <code>DAY_OF_MONTH</code> is set to the closest possible
0N/A * value, 30. The <code>YEAR</code> field maintains the value of 1999 because it
0N/A * is a larger field than <code>MONTH</code>.
0N/A * <p>
0N/A * <em>Example</em>: Consider a <code>GregorianCalendar</code>
0N/A * originally set to Sunday June 6, 1999. Calling
0N/A * <code>roll(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
0N/A * Tuesday June 1, 1999, whereas calling
0N/A * <code>add(Calendar.WEEK_OF_MONTH, -1)</code> sets the calendar to
0N/A * Sunday May 30, 1999. This is because the roll rule imposes an
0N/A * additional constraint: The <code>MONTH</code> must not change when the
0N/A * <code>WEEK_OF_MONTH</code> is rolled. Taken together with add rule 1,
0N/A * the resultant date must be between Tuesday June 1 and Saturday June
0N/A * 5. According to add rule 2, the <code>DAY_OF_WEEK</code>, an invariant
0N/A * when changing the <code>WEEK_OF_MONTH</code>, is set to Tuesday, the
0N/A * closest possible value to Sunday (where Sunday is the first day of the
0N/A * week).</p>
0N/A *
0N/A * @param field the calendar field.
0N/A * @param amount the signed amount to add to <code>field</code>.
0N/A * @exception IllegalArgumentException if <code>field</code> is
0N/A * <code>ZONE_OFFSET</code>, <code>DST_OFFSET</code>, or unknown,
0N/A * or if any calendar fields have out-of-range values in
0N/A * non-lenient mode.
0N/A * @see #roll(int,boolean)
0N/A * @see #add(int,int)
0N/A * @see #set(int,int)
0N/A * @since 1.2
0N/A */
0N/A public void roll(int field, int amount) {
0N/A // If amount == 0, do nothing even the given field is out of
0N/A // range. This is tested by JCK.
0N/A if (amount == 0) {
0N/A return;
0N/A }
0N/A
0N/A if (field < 0 || field >= ZONE_OFFSET) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A // Sync the time and calendar fields.
0N/A complete();
0N/A
0N/A int min = getMinimum(field);
0N/A int max = getMaximum(field);
0N/A
0N/A switch (field) {
0N/A case AM_PM:
0N/A case ERA:
0N/A case YEAR:
0N/A case MINUTE:
0N/A case SECOND:
0N/A case MILLISECOND:
0N/A // These fields are handled simply, since they have fixed minima
0N/A // and maxima. The field DAY_OF_MONTH is almost as simple. Other
0N/A // fields are complicated, since the range within they must roll
0N/A // varies depending on the date.
0N/A break;
0N/A
0N/A case HOUR:
0N/A case HOUR_OF_DAY:
0N/A {
0N/A int unit = max + 1; // 12 or 24 hours
0N/A int h = internalGet(field);
0N/A int nh = (h + amount) % unit;
0N/A if (nh < 0) {
0N/A nh += unit;
0N/A }
0N/A time += ONE_HOUR * (nh - h);
0N/A
0N/A // The day might have changed, which could happen if
0N/A // the daylight saving time transition brings it to
0N/A // the next day, although it's very unlikely. But we
0N/A // have to make sure not to change the larger fields.
0N/A CalendarDate d = calsys.getCalendarDate(time, getZone());
0N/A if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) {
0N/A d.setDate(internalGet(YEAR),
0N/A internalGet(MONTH) + 1,
0N/A internalGet(DAY_OF_MONTH));
0N/A if (field == HOUR) {
0N/A assert (internalGet(AM_PM) == PM);
0N/A d.addHours(+12); // restore PM
0N/A }
0N/A time = calsys.getTime(d);
0N/A }
0N/A int hourOfDay = d.getHours();
0N/A internalSet(field, hourOfDay % unit);
0N/A if (field == HOUR) {
0N/A internalSet(HOUR_OF_DAY, hourOfDay);
0N/A } else {
0N/A internalSet(AM_PM, hourOfDay / 12);
0N/A internalSet(HOUR, hourOfDay % 12);
0N/A }
0N/A
0N/A // Time zone offset and/or daylight saving might have changed.
0N/A int zoneOffset = d.getZoneOffset();
0N/A int saving = d.getDaylightSaving();
0N/A internalSet(ZONE_OFFSET, zoneOffset - saving);
0N/A internalSet(DST_OFFSET, saving);
0N/A return;
0N/A }
0N/A
0N/A case MONTH:
0N/A // Rolling the month involves both pinning the final value to [0, 11]
0N/A // and adjusting the DAY_OF_MONTH if necessary. We only adjust the
0N/A // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
0N/A // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
0N/A {
0N/A if (!isCutoverYear(cdate.getNormalizedYear())) {
0N/A int mon = (internalGet(MONTH) + amount) % 12;
0N/A if (mon < 0) {
0N/A mon += 12;
0N/A }
0N/A set(MONTH, mon);
0N/A
0N/A // Keep the day of month in the range. We don't want to spill over
0N/A // into the next month; e.g., we don't want jan31 + 1 mo -> feb31 ->
0N/A // mar3.
0N/A int monthLen = monthLength(mon);
0N/A if (internalGet(DAY_OF_MONTH) > monthLen) {
0N/A set(DAY_OF_MONTH, monthLen);
0N/A }
0N/A } else {
0N/A // We need to take care of different lengths in
0N/A // year and month due to the cutover.
0N/A int yearLength = getActualMaximum(MONTH) + 1;
0N/A int mon = (internalGet(MONTH) + amount) % yearLength;
0N/A if (mon < 0) {
0N/A mon += yearLength;
0N/A }
0N/A set(MONTH, mon);
0N/A int monthLen = getActualMaximum(DAY_OF_MONTH);
0N/A if (internalGet(DAY_OF_MONTH) > monthLen) {
0N/A set(DAY_OF_MONTH, monthLen);
0N/A }
0N/A }
0N/A return;
0N/A }
0N/A
0N/A case WEEK_OF_YEAR:
0N/A {
0N/A int y = cdate.getNormalizedYear();
0N/A max = getActualMaximum(WEEK_OF_YEAR);
0N/A set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
0N/A int woy = internalGet(WEEK_OF_YEAR);
0N/A int value = woy + amount;
0N/A if (!isCutoverYear(y)) {
0N/A // If the new value is in between min and max
0N/A // (exclusive), then we can use the value.
0N/A if (value > min && value < max) {
0N/A set(WEEK_OF_YEAR, value);
0N/A return;
0N/A }
0N/A long fd = getCurrentFixedDate();
0N/A // Make sure that the min week has the current DAY_OF_WEEK
0N/A long day1 = fd - (7 * (woy - min));
0N/A if (calsys.getYearFromFixedDate(day1) != y) {
0N/A min++;
0N/A }
0N/A
0N/A // Make sure the same thing for the max week
0N/A fd += 7 * (max - internalGet(WEEK_OF_YEAR));
0N/A if (calsys.getYearFromFixedDate(fd) != y) {
0N/A max--;
0N/A }
0N/A break;
0N/A }
0N/A
0N/A // Handle cutover here.
0N/A long fd = getCurrentFixedDate();
0N/A BaseCalendar cal;
0N/A if (gregorianCutoverYear == gregorianCutoverYearJulian) {
0N/A cal = getCutoverCalendarSystem();
0N/A } else if (y == gregorianCutoverYear) {
0N/A cal = gcal;
0N/A } else {
0N/A cal = getJulianCalendarSystem();
0N/A }
0N/A long day1 = fd - (7 * (woy - min));
0N/A // Make sure that the min week has the current DAY_OF_WEEK
0N/A if (cal.getYearFromFixedDate(day1) != y) {
0N/A min++;
0N/A }
0N/A
0N/A // Make sure the same thing for the max week
0N/A fd += 7 * (max - woy);
0N/A cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
0N/A if (cal.getYearFromFixedDate(fd) != y) {
0N/A max--;
0N/A }
0N/A // value: the new WEEK_OF_YEAR which must be converted
0N/A // to month and day of month.
0N/A value = getRolledValue(woy, amount, min, max) - 1;
0N/A BaseCalendar.Date d = getCalendarDate(day1 + value * 7);
0N/A set(MONTH, d.getMonth() - 1);
0N/A set(DAY_OF_MONTH, d.getDayOfMonth());
0N/A return;
0N/A }
0N/A
0N/A case WEEK_OF_MONTH:
0N/A {
0N/A boolean isCutoverYear = isCutoverYear(cdate.getNormalizedYear());
0N/A // dow: relative day of week from first day of week
0N/A int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
0N/A if (dow < 0) {
0N/A dow += 7;
0N/A }
0N/A
0N/A long fd = getCurrentFixedDate();
0N/A long month1; // fixed date of the first day (usually 1) of the month
0N/A int monthLength; // actual month length
0N/A if (isCutoverYear) {
0N/A month1 = getFixedDateMonth1(cdate, fd);
0N/A monthLength = actualMonthLength();
0N/A } else {
0N/A month1 = fd - internalGet(DAY_OF_MONTH) + 1;
0N/A monthLength = calsys.getMonthLength(cdate);
0N/A }
0N/A
0N/A // the first day of week of the month.
0N/A long monthDay1st = calsys.getDayOfWeekDateOnOrBefore(month1 + 6,
0N/A getFirstDayOfWeek());
0N/A // if the week has enough days to form a week, the
0N/A // week starts from the previous month.
0N/A if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
0N/A monthDay1st -= 7;
0N/A }
0N/A max = getActualMaximum(field);
0N/A
0N/A // value: the new WEEK_OF_MONTH value
0N/A int value = getRolledValue(internalGet(field), amount, 1, max) - 1;
0N/A
0N/A // nfd: fixed date of the rolled date
0N/A long nfd = monthDay1st + value * 7 + dow;
0N/A
0N/A // Unlike WEEK_OF_YEAR, we need to change day of week if the
0N/A // nfd is out of the month.
0N/A if (nfd < month1) {
0N/A nfd = month1;
0N/A } else if (nfd >= (month1 + monthLength)) {
0N/A nfd = month1 + monthLength - 1;
0N/A }
0N/A int dayOfMonth;
0N/A if (isCutoverYear) {
0N/A // If we are in the cutover year, convert nfd to
0N/A // its calendar date and use dayOfMonth.
0N/A BaseCalendar.Date d = getCalendarDate(nfd);
0N/A dayOfMonth = d.getDayOfMonth();
0N/A } else {
0N/A dayOfMonth = (int)(nfd - month1) + 1;
0N/A }
0N/A set(DAY_OF_MONTH, dayOfMonth);
0N/A return;
0N/A }
0N/A
0N/A case DAY_OF_MONTH:
0N/A {
0N/A if (!isCutoverYear(cdate.getNormalizedYear())) {
0N/A max = calsys.getMonthLength(cdate);
0N/A break;
0N/A }
0N/A
0N/A // Cutover year handling
0N/A long fd = getCurrentFixedDate();
0N/A long month1 = getFixedDateMonth1(cdate, fd);
0N/A // It may not be a regular month. Convert the date and range to
0N/A // the relative values, perform the roll, and
0N/A // convert the result back to the rolled date.
0N/A int value = getRolledValue((int)(fd - month1), amount, 0, actualMonthLength() - 1);
0N/A BaseCalendar.Date d = getCalendarDate(month1 + value);
0N/A assert d.getMonth()-1 == internalGet(MONTH);
0N/A set(DAY_OF_MONTH, d.getDayOfMonth());
0N/A return;
0N/A }
0N/A
0N/A case DAY_OF_YEAR:
0N/A {
0N/A max = getActualMaximum(field);
0N/A if (!isCutoverYear(cdate.getNormalizedYear())) {
0N/A break;
0N/A }
0N/A
0N/A // Handle cutover here.
0N/A long fd = getCurrentFixedDate();
0N/A long jan1 = fd - internalGet(DAY_OF_YEAR) + 1;
0N/A int value = getRolledValue((int)(fd - jan1) + 1, amount, min, max);
0N/A BaseCalendar.Date d = getCalendarDate(jan1 + value - 1);
0N/A set(MONTH, d.getMonth() - 1);
0N/A set(DAY_OF_MONTH, d.getDayOfMonth());
0N/A return;
0N/A }
0N/A
0N/A case DAY_OF_WEEK:
0N/A {
0N/A if (!isCutoverYear(cdate.getNormalizedYear())) {
0N/A // If the week of year is in the same year, we can
0N/A // just change DAY_OF_WEEK.
0N/A int weekOfYear = internalGet(WEEK_OF_YEAR);
0N/A if (weekOfYear > 1 && weekOfYear < 52) {
0N/A set(WEEK_OF_YEAR, weekOfYear); // update stamp[WEEK_OF_YEAR]
0N/A max = SATURDAY;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A // We need to handle it in a different way around year
0N/A // boundaries and in the cutover year. Note that
0N/A // changing era and year values violates the roll
0N/A // rule: not changing larger calendar fields...
0N/A amount %= 7;
0N/A if (amount == 0) {
0N/A return;
0N/A }
0N/A long fd = getCurrentFixedDate();
0N/A long dowFirst = calsys.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
0N/A fd += amount;
0N/A if (fd < dowFirst) {
0N/A fd += 7;
0N/A } else if (fd >= dowFirst + 7) {
0N/A fd -= 7;
0N/A }
0N/A BaseCalendar.Date d = getCalendarDate(fd);
0N/A set(ERA, (d.getNormalizedYear() <= 0 ? BCE : CE));
0N/A set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
0N/A return;
0N/A }
0N/A
0N/A case DAY_OF_WEEK_IN_MONTH:
0N/A {
0N/A min = 1; // after normalized, min should be 1.
0N/A if (!isCutoverYear(cdate.getNormalizedYear())) {
0N/A int dom = internalGet(DAY_OF_MONTH);
0N/A int monthLength = calsys.getMonthLength(cdate);
0N/A int lastDays = monthLength % 7;
0N/A max = monthLength / 7;
0N/A int x = (dom - 1) % 7;
0N/A if (x < lastDays) {
0N/A max++;
0N/A }
0N/A set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
0N/A break;
0N/A }
0N/A
0N/A // Cutover year handling
0N/A long fd = getCurrentFixedDate();
0N/A long month1 = getFixedDateMonth1(cdate, fd);
0N/A int monthLength = actualMonthLength();
0N/A int lastDays = monthLength % 7;
0N/A max = monthLength / 7;
0N/A int x = (int)(fd - month1) % 7;
0N/A if (x < lastDays) {
0N/A max++;
0N/A }
0N/A int value = getRolledValue(internalGet(field), amount, min, max) - 1;
0N/A fd = month1 + value * 7 + x;
0N/A BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
0N/A BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
0N/A cal.getCalendarDateFromFixedDate(d, fd);
0N/A set(DAY_OF_MONTH, d.getDayOfMonth());
0N/A return;
0N/A }
0N/A }
0N/A
0N/A set(field, getRolledValue(internalGet(field), amount, min, max));
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum value for the given calendar field of this
0N/A * <code>GregorianCalendar</code> instance. The minimum value is
0N/A * defined as the smallest value returned by the {@link
0N/A * Calendar#get(int) get} method for any possible time value,
0N/A * taking into consideration the current values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A *
0N/A * @param field the calendar field.
0N/A * @return the minimum value for the given calendar field.
0N/A * @see #getMaximum(int)
0N/A * @see #getGreatestMinimum(int)
0N/A * @see #getLeastMaximum(int)
0N/A * @see #getActualMinimum(int)
0N/A * @see #getActualMaximum(int)
0N/A */
0N/A public int getMinimum(int field) {
0N/A return MIN_VALUES[field];
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum value for the given calendar field of this
0N/A * <code>GregorianCalendar</code> instance. The maximum value is
0N/A * defined as the largest value returned by the {@link
0N/A * Calendar#get(int) get} method for any possible time value,
0N/A * taking into consideration the current values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A *
0N/A * @param field the calendar field.
0N/A * @return the maximum value for the given calendar field.
0N/A * @see #getMinimum(int)
0N/A * @see #getGreatestMinimum(int)
0N/A * @see #getLeastMaximum(int)
0N/A * @see #getActualMinimum(int)
0N/A * @see #getActualMaximum(int)
0N/A */
0N/A public int getMaximum(int field) {
0N/A switch (field) {
0N/A case MONTH:
0N/A case DAY_OF_MONTH:
0N/A case DAY_OF_YEAR:
0N/A case WEEK_OF_YEAR:
0N/A case WEEK_OF_MONTH:
0N/A case DAY_OF_WEEK_IN_MONTH:
0N/A case YEAR:
0N/A {
0N/A // On or after Gregorian 200-3-1, Julian and Gregorian
0N/A // calendar dates are the same or Gregorian dates are
0N/A // larger (i.e., there is a "gap") after 300-3-1.
0N/A if (gregorianCutoverYear > 200) {
0N/A break;
0N/A }
0N/A // There might be "overlapping" dates.
0N/A GregorianCalendar gc = (GregorianCalendar) clone();
0N/A gc.setLenient(true);
0N/A gc.setTimeInMillis(gregorianCutover);
0N/A int v1 = gc.getActualMaximum(field);
0N/A gc.setTimeInMillis(gregorianCutover-1);
0N/A int v2 = gc.getActualMaximum(field);
0N/A return Math.max(MAX_VALUES[field], Math.max(v1, v2));
0N/A }
0N/A }
0N/A return MAX_VALUES[field];
0N/A }
0N/A
0N/A /**
0N/A * Returns the highest minimum value for the given calendar field
0N/A * of this <code>GregorianCalendar</code> instance. The highest
0N/A * minimum value is defined as the largest value returned by
0N/A * {@link #getActualMinimum(int)} for any possible time value,
0N/A * taking into consideration the current values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A *
0N/A * @param field the calendar field.
0N/A * @return the highest minimum value for the given calendar field.
0N/A * @see #getMinimum(int)
0N/A * @see #getMaximum(int)
0N/A * @see #getLeastMaximum(int)
0N/A * @see #getActualMinimum(int)
0N/A * @see #getActualMaximum(int)
0N/A */
0N/A public int getGreatestMinimum(int field) {
0N/A if (field == DAY_OF_MONTH) {
0N/A BaseCalendar.Date d = getGregorianCutoverDate();
0N/A long mon1 = getFixedDateMonth1(d, gregorianCutoverDate);
0N/A d = getCalendarDate(mon1);
0N/A return Math.max(MIN_VALUES[field], d.getDayOfMonth());
0N/A }
0N/A return MIN_VALUES[field];
0N/A }
0N/A
0N/A /**
0N/A * Returns the lowest maximum value for the given calendar field
0N/A * of this <code>GregorianCalendar</code> instance. The lowest
0N/A * maximum value is defined as the smallest value returned by
0N/A * {@link #getActualMaximum(int)} for any possible time value,
0N/A * taking into consideration the current values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A *
0N/A * @param field the calendar field
0N/A * @return the lowest maximum value for the given calendar field.
0N/A * @see #getMinimum(int)
0N/A * @see #getMaximum(int)
0N/A * @see #getGreatestMinimum(int)
0N/A * @see #getActualMinimum(int)
0N/A * @see #getActualMaximum(int)
0N/A */
0N/A public int getLeastMaximum(int field) {
0N/A switch (field) {
0N/A case MONTH:
0N/A case DAY_OF_MONTH:
0N/A case DAY_OF_YEAR:
0N/A case WEEK_OF_YEAR:
0N/A case WEEK_OF_MONTH:
0N/A case DAY_OF_WEEK_IN_MONTH:
0N/A case YEAR:
0N/A {
0N/A GregorianCalendar gc = (GregorianCalendar) clone();
0N/A gc.setLenient(true);
0N/A gc.setTimeInMillis(gregorianCutover);
0N/A int v1 = gc.getActualMaximum(field);
0N/A gc.setTimeInMillis(gregorianCutover-1);
0N/A int v2 = gc.getActualMaximum(field);
0N/A return Math.min(LEAST_MAX_VALUES[field], Math.min(v1, v2));
0N/A }
0N/A }
0N/A return LEAST_MAX_VALUES[field];
0N/A }
0N/A
0N/A /**
0N/A * Returns the minimum value that this calendar field could have,
0N/A * taking into consideration the given time value and the current
0N/A * values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A *
0N/A * <p>For example, if the Gregorian change date is January 10,
0N/A * 1970 and the date of this <code>GregorianCalendar</code> is
0N/A * January 20, 1970, the actual minimum value of the
0N/A * <code>DAY_OF_MONTH</code> field is 10 because the previous date
0N/A * of January 10, 1970 is December 27, 1996 (in the Julian
0N/A * calendar). Therefore, December 28, 1969 to January 9, 1970
0N/A * don't exist.
0N/A *
0N/A * @param field the calendar field
0N/A * @return the minimum of the given field for the time value of
0N/A * this <code>GregorianCalendar</code>
0N/A * @see #getMinimum(int)
0N/A * @see #getMaximum(int)
0N/A * @see #getGreatestMinimum(int)
0N/A * @see #getLeastMaximum(int)
0N/A * @see #getActualMaximum(int)
0N/A * @since 1.2
0N/A */
0N/A public int getActualMinimum(int field) {
0N/A if (field == DAY_OF_MONTH) {
0N/A GregorianCalendar gc = getNormalizedCalendar();
0N/A int year = gc.cdate.getNormalizedYear();
0N/A if (year == gregorianCutoverYear || year == gregorianCutoverYearJulian) {
0N/A long month1 = getFixedDateMonth1(gc.cdate, gc.calsys.getFixedDate(gc.cdate));
0N/A BaseCalendar.Date d = getCalendarDate(month1);
0N/A return d.getDayOfMonth();
0N/A }
0N/A }
0N/A return getMinimum(field);
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum value that this calendar field could have,
0N/A * taking into consideration the given time value and the current
0N/A * values of the
0N/A * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
0N/A * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
0N/A * {@link #getGregorianChange() getGregorianChange} and
0N/A * {@link Calendar#getTimeZone() getTimeZone} methods.
0N/A * For example, if the date of this instance is February 1, 2004,
0N/A * the actual maximum value of the <code>DAY_OF_MONTH</code> field
0N/A * is 29 because 2004 is a leap year, and if the date of this
0N/A * instance is February 1, 2005, it's 28.
0N/A *
2702N/A * <p>This method calculates the maximum value of {@link
2702N/A * Calendar#WEEK_OF_YEAR WEEK_OF_YEAR} based on the {@link
2702N/A * Calendar#YEAR YEAR} (calendar year) value, not the <a
2702N/A * href="#week_year">week year</a>. Call {@link
2702N/A * #getWeeksInWeekYear()} to get the maximum value of {@code
2702N/A * WEEK_OF_YEAR} in the week year of this {@code GregorianCalendar}.
2702N/A *
0N/A * @param field the calendar field
0N/A * @return the maximum of the given field for the time value of
0N/A * this <code>GregorianCalendar</code>
0N/A * @see #getMinimum(int)
0N/A * @see #getMaximum(int)
0N/A * @see #getGreatestMinimum(int)
0N/A * @see #getLeastMaximum(int)
0N/A * @see #getActualMinimum(int)
0N/A * @since 1.2
0N/A */
0N/A public int getActualMaximum(int field) {
0N/A final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
0N/A HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
0N/A ZONE_OFFSET_MASK|DST_OFFSET_MASK;
0N/A if ((fieldsForFixedMax & (1<<field)) != 0) {
0N/A return getMaximum(field);
0N/A }
0N/A
0N/A GregorianCalendar gc = getNormalizedCalendar();
0N/A BaseCalendar.Date date = gc.cdate;
0N/A BaseCalendar cal = gc.calsys;
0N/A int normalizedYear = date.getNormalizedYear();
0N/A
0N/A int value = -1;
0N/A switch (field) {
0N/A case MONTH:
0N/A {
0N/A if (!gc.isCutoverYear(normalizedYear)) {
0N/A value = DECEMBER;
0N/A break;
0N/A }
0N/A
0N/A // January 1 of the next year may or may not exist.
0N/A long nextJan1;
0N/A do {
0N/A nextJan1 = gcal.getFixedDate(++normalizedYear, BaseCalendar.JANUARY, 1, null);
0N/A } while (nextJan1 < gregorianCutoverDate);
0N/A BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
0N/A cal.getCalendarDateFromFixedDate(d, nextJan1 - 1);
0N/A value = d.getMonth() - 1;
0N/A }
0N/A break;
0N/A
0N/A case DAY_OF_MONTH:
0N/A {
0N/A value = cal.getMonthLength(date);
0N/A if (!gc.isCutoverYear(normalizedYear) || date.getDayOfMonth() == value) {
0N/A break;
0N/A }
0N/A
0N/A // Handle cutover year.
0N/A long fd = gc.getCurrentFixedDate();
0N/A if (fd >= gregorianCutoverDate) {
0N/A break;
0N/A }
0N/A int monthLength = gc.actualMonthLength();
0N/A long monthEnd = gc.getFixedDateMonth1(gc.cdate, fd) + monthLength - 1;
0N/A // Convert the fixed date to its calendar date.
0N/A BaseCalendar.Date d = gc.getCalendarDate(monthEnd);
0N/A value = d.getDayOfMonth();
0N/A }
0N/A break;
0N/A
0N/A case DAY_OF_YEAR:
0N/A {
0N/A if (!gc.isCutoverYear(normalizedYear)) {
0N/A value = cal.getYearLength(date);
0N/A break;
0N/A }
0N/A
0N/A // Handle cutover year.
0N/A long jan1;
0N/A if (gregorianCutoverYear == gregorianCutoverYearJulian) {
0N/A BaseCalendar cocal = gc.getCutoverCalendarSystem();
0N/A jan1 = cocal.getFixedDate(normalizedYear, 1, 1, null);
0N/A } else if (normalizedYear == gregorianCutoverYearJulian) {
0N/A jan1 = cal.getFixedDate(normalizedYear, 1, 1, null);
0N/A } else {
0N/A jan1 = gregorianCutoverDate;
0N/A }
0N/A // January 1 of the next year may or may not exist.
0N/A long nextJan1 = gcal.getFixedDate(++normalizedYear, 1, 1, null);
0N/A if (nextJan1 < gregorianCutoverDate) {
0N/A nextJan1 = gregorianCutoverDate;
0N/A }
0N/A assert jan1 <= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
0N/A date.getDayOfMonth(), date);
0N/A assert nextJan1 >= cal.getFixedDate(date.getNormalizedYear(), date.getMonth(),
0N/A date.getDayOfMonth(), date);
0N/A value = (int)(nextJan1 - jan1);
0N/A }
0N/A break;
0N/A
0N/A case WEEK_OF_YEAR:
0N/A {
0N/A if (!gc.isCutoverYear(normalizedYear)) {
0N/A // Get the day of week of January 1 of the year
0N/A CalendarDate d = cal.newCalendarDate(TimeZone.NO_TIMEZONE);
0N/A d.setDate(date.getYear(), BaseCalendar.JANUARY, 1);
0N/A int dayOfWeek = cal.getDayOfWeek(d);
0N/A // Normalize the day of week with the firstDayOfWeek value
0N/A dayOfWeek -= getFirstDayOfWeek();
0N/A if (dayOfWeek < 0) {
0N/A dayOfWeek += 7;
0N/A }
0N/A value = 52;
0N/A int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
0N/A if ((magic == 6) ||
0N/A (date.isLeapYear() && (magic == 5 || magic == 12))) {
0N/A value++;
0N/A }
0N/A break;
0N/A }
0N/A
0N/A if (gc == this) {
0N/A gc = (GregorianCalendar) gc.clone();
0N/A }
2702N/A int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
2702N/A gc.set(DAY_OF_YEAR, maxDayOfYear);
0N/A value = gc.get(WEEK_OF_YEAR);
2702N/A if (internalGet(YEAR) != gc.getWeekYear()) {
2702N/A gc.set(DAY_OF_YEAR, maxDayOfYear - 7);
2702N/A value = gc.get(WEEK_OF_YEAR);
2702N/A }
0N/A }
0N/A break;
0N/A
0N/A case WEEK_OF_MONTH:
0N/A {
0N/A if (!gc.isCutoverYear(normalizedYear)) {
0N/A CalendarDate d = cal.newCalendarDate(null);
0N/A d.setDate(date.getYear(), date.getMonth(), 1);
0N/A int dayOfWeek = cal.getDayOfWeek(d);
0N/A int monthLength = cal.getMonthLength(d);
0N/A dayOfWeek -= getFirstDayOfWeek();
0N/A if (dayOfWeek < 0) {
0N/A dayOfWeek += 7;
0N/A }
0N/A int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
0N/A value = 3;
0N/A if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
0N/A value++;
0N/A }
0N/A monthLength -= nDaysFirstWeek + 7 * 3;
0N/A if (monthLength > 0) {
0N/A value++;
0N/A if (monthLength > 7) {
0N/A value++;
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A
0N/A // Cutover year handling
0N/A if (gc == this) {
0N/A gc = (GregorianCalendar) gc.clone();
0N/A }
0N/A int y = gc.internalGet(YEAR);
0N/A int m = gc.internalGet(MONTH);
0N/A do {
0N/A value = gc.get(WEEK_OF_MONTH);
0N/A gc.add(WEEK_OF_MONTH, +1);
0N/A } while (gc.get(YEAR) == y && gc.get(MONTH) == m);
0N/A }
0N/A break;
0N/A
0N/A case DAY_OF_WEEK_IN_MONTH:
0N/A {
0N/A // may be in the Gregorian cutover month
0N/A int ndays, dow1;
0N/A int dow = date.getDayOfWeek();
0N/A if (!gc.isCutoverYear(normalizedYear)) {
0N/A BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
0N/A ndays = cal.getMonthLength(d);
0N/A d.setDayOfMonth(1);
0N/A cal.normalize(d);
0N/A dow1 = d.getDayOfWeek();
0N/A } else {
0N/A // Let a cloned GregorianCalendar take care of the cutover cases.
0N/A if (gc == this) {
0N/A gc = (GregorianCalendar) clone();
0N/A }
0N/A ndays = gc.actualMonthLength();
0N/A gc.set(DAY_OF_MONTH, gc.getActualMinimum(DAY_OF_MONTH));
0N/A dow1 = gc.get(DAY_OF_WEEK);
0N/A }
0N/A int x = dow - dow1;
0N/A if (x < 0) {
0N/A x += 7;
0N/A }
0N/A ndays -= x;
0N/A value = (ndays + 6) / 7;
0N/A }
0N/A break;
0N/A
0N/A case YEAR:
0N/A /* The year computation is no different, in principle, from the
0N/A * others, however, the range of possible maxima is large. In
0N/A * addition, the way we know we've exceeded the range is different.
0N/A * For these reasons, we use the special case code below to handle
0N/A * this field.
0N/A *
0N/A * The actual maxima for YEAR depend on the type of calendar:
0N/A *
0N/A * Gregorian = May 17, 292275056 BCE - Aug 17, 292278994 CE
0N/A * Julian = Dec 2, 292269055 BCE - Jan 3, 292272993 CE
0N/A * Hybrid = Dec 2, 292269055 BCE - Aug 17, 292278994 CE
0N/A *
0N/A * We know we've exceeded the maximum when either the month, date,
0N/A * time, or era changes in response to setting the year. We don't
0N/A * check for month, date, and time here because the year and era are
0N/A * sufficient to detect an invalid year setting. NOTE: If code is
0N/A * added to check the month and date in the future for some reason,
0N/A * Feb 29 must be allowed to shift to Mar 1 when setting the year.
0N/A */
0N/A {
0N/A if (gc == this) {
0N/A gc = (GregorianCalendar) clone();
0N/A }
0N/A
0N/A // Calculate the millisecond offset from the beginning
0N/A // of the year of this calendar and adjust the max
0N/A // year value if we are beyond the limit in the max
0N/A // year.
0N/A long current = gc.getYearOffsetInMillis();
0N/A
0N/A if (gc.internalGetEra() == CE) {
0N/A gc.setTimeInMillis(Long.MAX_VALUE);
0N/A value = gc.get(YEAR);
0N/A long maxEnd = gc.getYearOffsetInMillis();
0N/A if (current > maxEnd) {
0N/A value--;
0N/A }
0N/A } else {
0N/A CalendarSystem mincal = gc.getTimeInMillis() >= gregorianCutover ?
0N/A gcal : getJulianCalendarSystem();
0N/A CalendarDate d = mincal.getCalendarDate(Long.MIN_VALUE, getZone());
0N/A long maxEnd = (cal.getDayOfYear(d) - 1) * 24 + d.getHours();
0N/A maxEnd *= 60;
0N/A maxEnd += d.getMinutes();
0N/A maxEnd *= 60;
0N/A maxEnd += d.getSeconds();
0N/A maxEnd *= 1000;
0N/A maxEnd += d.getMillis();
0N/A value = d.getYear();
0N/A if (value <= 0) {
0N/A assert mincal == gcal;
0N/A value = 1 - value;
0N/A }
0N/A if (current < maxEnd) {
0N/A value--;
0N/A }
0N/A }
0N/A }
0N/A break;
0N/A
0N/A default:
0N/A throw new ArrayIndexOutOfBoundsException(field);
0N/A }
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the millisecond offset from the beginning of this
0N/A * year. This Calendar object must have been normalized.
0N/A */
0N/A private final long getYearOffsetInMillis() {
0N/A long t = (internalGet(DAY_OF_YEAR) - 1) * 24;
0N/A t += internalGet(HOUR_OF_DAY);
0N/A t *= 60;
0N/A t += internalGet(MINUTE);
0N/A t *= 60;
0N/A t += internalGet(SECOND);
0N/A t *= 1000;
0N/A return t + internalGet(MILLISECOND) -
0N/A (internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET));
0N/A }
0N/A
0N/A public Object clone()
0N/A {
0N/A GregorianCalendar other = (GregorianCalendar) super.clone();
0N/A
0N/A other.gdate = (BaseCalendar.Date) gdate.clone();
0N/A if (cdate != null) {
0N/A if (cdate != gdate) {
0N/A other.cdate = (BaseCalendar.Date) cdate.clone();
0N/A } else {
0N/A other.cdate = other.gdate;
0N/A }
0N/A }
0N/A other.originalFields = null;
0N/A other.zoneOffsets = null;
0N/A return other;
0N/A }
0N/A
0N/A public TimeZone getTimeZone() {
0N/A TimeZone zone = super.getTimeZone();
0N/A // To share the zone by CalendarDates
0N/A gdate.setZone(zone);
0N/A if (cdate != null && cdate != gdate) {
0N/A cdate.setZone(zone);
0N/A }
0N/A return zone;
0N/A }
0N/A
0N/A public void setTimeZone(TimeZone zone) {
0N/A super.setTimeZone(zone);
0N/A // To share the zone by CalendarDates
0N/A gdate.setZone(zone);
0N/A if (cdate != null && cdate != gdate) {
0N/A cdate.setZone(zone);
0N/A }
0N/A }
0N/A
2702N/A /**
2702N/A * Returns {@code true} indicating this {@code GregorianCalendar}
2702N/A * supports week dates.
2702N/A *
2702N/A * @return {@code true} (always)
2702N/A * @see #getWeekYear()
2702N/A * @see #setWeekDate(int,int,int)
2702N/A * @see #getWeeksInWeekYear()
2702N/A * @since 1.7
2702N/A */
2702N/A @Override
2702N/A public final boolean isWeekDateSupported() {
2702N/A return true;
2702N/A }
0N/A
0N/A /**
2702N/A * Returns the <a href="#week_year">week year</a> represented by this
2702N/A * {@code GregorianCalendar}. The dates in the weeks between 1 and the
2702N/A * maximum week number of the week year have the same week year value
2702N/A * that may be one year before or after the {@link Calendar#YEAR YEAR}
2702N/A * (calendar year) value.
0N/A *
2702N/A * <p>This method calls {@link Calendar#complete()} before
2702N/A * calculating the week year.
0N/A *
2702N/A * @return the week year represented by this {@code GregorianCalendar}.
2702N/A * If the {@link Calendar#ERA ERA} value is {@link #BC}, the year is
2702N/A * represented by 0 or a negative number: BC 1 is 0, BC 2
2702N/A * is -1, BC 3 is -2, and so on.
2702N/A * @throws IllegalArgumentException
2702N/A * if any of the calendar fields is invalid in non-lenient mode.
2702N/A * @see #isWeekDateSupported()
2702N/A * @see #getWeeksInWeekYear()
2702N/A * @see Calendar#getFirstDayOfWeek()
2702N/A * @see Calendar#getMinimalDaysInFirstWeek()
2702N/A * @since 1.7
0N/A */
2702N/A @Override
2702N/A public int getWeekYear() {
2702N/A int year = get(YEAR); // implicitly calls complete()
2702N/A if (internalGetEra() == BCE) {
2702N/A year = 1 - year;
2702N/A }
2702N/A
2702N/A // Fast path for the Gregorian calendar years that are never
2702N/A // affected by the Julian-Gregorian transition
2702N/A if (year > gregorianCutoverYear + 1) {
2702N/A int weekOfYear = internalGet(WEEK_OF_YEAR);
2702N/A if (internalGet(MONTH) == JANUARY) {
2702N/A if (weekOfYear >= 52) {
2702N/A --year;
2702N/A }
2702N/A } else {
2702N/A if (weekOfYear == 1) {
2702N/A ++year;
2702N/A }
2702N/A }
2702N/A return year;
2702N/A }
2702N/A
2702N/A // General (slow) path
2702N/A int dayOfYear = internalGet(DAY_OF_YEAR);
2702N/A int maxDayOfYear = getActualMaximum(DAY_OF_YEAR);
2702N/A int minimalDays = getMinimalDaysInFirstWeek();
2702N/A
2702N/A // Quickly check the possibility of year adjustments before
2702N/A // cloning this GregorianCalendar.
2702N/A if (dayOfYear > minimalDays && dayOfYear < (maxDayOfYear - 6)) {
2702N/A return year;
2702N/A }
2702N/A
2702N/A // Create a clone to work on the calculation
2702N/A GregorianCalendar cal = (GregorianCalendar) clone();
2702N/A cal.setLenient(true);
2702N/A // Use GMT so that intermediate date calculations won't
2702N/A // affect the time of day fields.
2702N/A cal.setTimeZone(TimeZone.getTimeZone("GMT"));
2702N/A // Go to the first day of the year, which is usually January 1.
2702N/A cal.set(DAY_OF_YEAR, 1);
2702N/A cal.complete();
2702N/A
2702N/A // Get the first day of the first day-of-week in the year.
2702N/A int delta = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2702N/A if (delta != 0) {
2702N/A if (delta < 0) {
2702N/A delta += 7;
2702N/A }
2702N/A cal.add(DAY_OF_YEAR, delta);
2702N/A }
2702N/A int minDayOfYear = cal.get(DAY_OF_YEAR);
2702N/A if (dayOfYear < minDayOfYear) {
2702N/A if (minDayOfYear <= minimalDays) {
0N/A --year;
0N/A }
0N/A } else {
2702N/A cal.set(YEAR, year + 1);
2702N/A cal.set(DAY_OF_YEAR, 1);
2702N/A cal.complete();
2702N/A int del = getFirstDayOfWeek() - cal.get(DAY_OF_WEEK);
2702N/A if (del != 0) {
2702N/A if (del < 0) {
2702N/A del += 7;
2702N/A }
2702N/A cal.add(DAY_OF_YEAR, del);
2702N/A }
2702N/A minDayOfYear = cal.get(DAY_OF_YEAR) - 1;
2702N/A if (minDayOfYear == 0) {
2702N/A minDayOfYear = 7;
2702N/A }
2702N/A if (minDayOfYear >= minimalDays) {
2702N/A int days = maxDayOfYear - dayOfYear + 1;
2702N/A if (days <= (7 - minDayOfYear)) {
2702N/A ++year;
2702N/A }
0N/A }
0N/A }
0N/A return year;
0N/A }
2702N/A
2702N/A /**
2702N/A * Sets this {@code GregorianCalendar} to the date given by the
2702N/A * date specifiers - <a href="#week_year">{@code weekYear}</a>,
2702N/A * {@code weekOfYear}, and {@code dayOfWeek}. {@code weekOfYear}
2702N/A * follows the <a href="#week_and_year">{@code WEEK_OF_YEAR}
2702N/A * numbering</a>. The {@code dayOfWeek} value must be one of the
2702N/A * {@link Calendar#DAY_OF_WEEK DAY_OF_WEEK} values: {@link
2702N/A * Calendar#SUNDAY SUNDAY} to {@link Calendar#SATURDAY SATURDAY}.
2702N/A *
2702N/A * <p>Note that the numeric day-of-week representation differs from
2702N/A * the ISO 8601 standard, and that the {@code weekOfYear}
2702N/A * numbering is compatible with the standard when {@code
2702N/A * getFirstDayOfWeek()} is {@code MONDAY} and {@code
2702N/A * getMinimalDaysInFirstWeek()} is 4.
2702N/A *
2702N/A * <p>Unlike the {@code set} method, all of the calendar fields
2702N/A * and the instant of time value are calculated upon return.
2702N/A *
2702N/A * <p>If {@code weekOfYear} is out of the valid week-of-year
2702N/A * range in {@code weekYear}, the {@code weekYear}
2702N/A * and {@code weekOfYear} values are adjusted in lenient
2702N/A * mode, or an {@code IllegalArgumentException} is thrown in
2702N/A * non-lenient mode.
2702N/A *
2702N/A * @param weekYear the week year
2702N/A * @param weekOfYear the week number based on {@code weekYear}
2702N/A * @param dayOfWeek the day of week value: one of the constants
2702N/A * for the {@link #DAY_OF_WEEK DAY_OF_WEEK} field:
2702N/A * {@link Calendar#SUNDAY SUNDAY}, ...,
2702N/A * {@link Calendar#SATURDAY SATURDAY}.
2702N/A * @exception IllegalArgumentException
2702N/A * if any of the given date specifiers is invalid,
2702N/A * or if any of the calendar fields are inconsistent
2702N/A * with the given date specifiers in non-lenient mode
2702N/A * @see GregorianCalendar#isWeekDateSupported()
2702N/A * @see Calendar#getFirstDayOfWeek()
2702N/A * @see Calendar#getMinimalDaysInFirstWeek()
2702N/A * @since 1.7
2702N/A */
2702N/A @Override
2702N/A public void setWeekDate(int weekYear, int weekOfYear, int dayOfWeek) {
2702N/A if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
2702N/A throw new IllegalArgumentException("invalid dayOfWeek: " + dayOfWeek);
2702N/A }
0N/A
2702N/A // To avoid changing the time of day fields by date
2702N/A // calculations, use a clone with the GMT time zone.
2702N/A GregorianCalendar gc = (GregorianCalendar) clone();
2702N/A gc.setLenient(true);
2702N/A int era = gc.get(ERA);
2702N/A gc.clear();
2702N/A gc.setTimeZone(TimeZone.getTimeZone("GMT"));
2702N/A gc.set(ERA, era);
2702N/A gc.set(YEAR, weekYear);
2702N/A gc.set(WEEK_OF_YEAR, 1);
2702N/A gc.set(DAY_OF_WEEK, getFirstDayOfWeek());
2702N/A int days = dayOfWeek - getFirstDayOfWeek();
2702N/A if (days < 0) {
2702N/A days += 7;
2702N/A }
2702N/A days += 7 * (weekOfYear - 1);
2702N/A if (days != 0) {
2702N/A gc.add(DAY_OF_YEAR, days);
2702N/A } else {
2702N/A gc.complete();
2702N/A }
2702N/A
2715N/A if (!isLenient() &&
2715N/A (gc.getWeekYear() != weekYear
2715N/A || gc.internalGet(WEEK_OF_YEAR) != weekOfYear
2715N/A || gc.internalGet(DAY_OF_WEEK) != dayOfWeek)) {
2715N/A throw new IllegalArgumentException();
2715N/A }
2715N/A
2702N/A set(ERA, gc.internalGet(ERA));
2702N/A set(YEAR, gc.internalGet(YEAR));
2702N/A set(MONTH, gc.internalGet(MONTH));
2702N/A set(DAY_OF_MONTH, gc.internalGet(DAY_OF_MONTH));
2702N/A
2702N/A // to avoid throwing an IllegalArgumentException in
2715N/A // non-lenient, set WEEK_OF_YEAR internally
2702N/A internalSet(WEEK_OF_YEAR, weekOfYear);
2702N/A complete();
2702N/A }
2702N/A
2702N/A /**
2702N/A * Returns the number of weeks in the <a href="#week_year">week year</a>
2702N/A * represented by this {@code GregorianCalendar}.
2702N/A *
2702N/A * <p>For example, if this {@code GregorianCalendar}'s date is
2702N/A * December 31, 2008 with <a href="#iso8601_compatible_setting">the ISO
2702N/A * 8601 compatible setting</a>, this method will return 53 for the
2702N/A * period: December 29, 2008 to January 3, 2010 while {@link
2702N/A * #getActualMaximum(int) getActualMaximum(WEEK_OF_YEAR)} will return
2702N/A * 52 for the period: December 31, 2007 to December 28, 2008.
2702N/A *
2702N/A * @return the number of weeks in the week year.
2702N/A * @see Calendar#WEEK_OF_YEAR
2702N/A * @see #getWeekYear()
2702N/A * @see #getActualMaximum(int)
2702N/A * @since 1.7
2702N/A */
2702N/A public int getWeeksInWeekYear() {
2702N/A GregorianCalendar gc = getNormalizedCalendar();
2702N/A int weekYear = gc.getWeekYear();
2702N/A if (weekYear == gc.internalGet(YEAR)) {
2702N/A return gc.getActualMaximum(WEEK_OF_YEAR);
2702N/A }
2702N/A
2702N/A // Use the 2nd week for calculating the max of WEEK_OF_YEAR
2702N/A if (gc == this) {
2702N/A gc = (GregorianCalendar) gc.clone();
2702N/A }
2702N/A gc.setWeekDate(weekYear, 2, internalGet(DAY_OF_WEEK));
2702N/A return gc.getActualMaximum(WEEK_OF_YEAR);
2702N/A }
0N/A
0N/A/////////////////////////////
0N/A// Time => Fields computation
0N/A/////////////////////////////
0N/A
0N/A /**
0N/A * The fixed date corresponding to gdate. If the value is
0N/A * Long.MIN_VALUE, the fixed date value is unknown. Currently,
0N/A * Julian calendar dates are not cached.
0N/A */
0N/A transient private long cachedFixedDate = Long.MIN_VALUE;
0N/A
0N/A /**
0N/A * Converts the time value (millisecond offset from the <a
0N/A * href="Calendar.html#Epoch">Epoch</a>) to calendar field values.
0N/A * The time is <em>not</em>
0N/A * recomputed first; to recompute the time, then the fields, call the
0N/A * <code>complete</code> method.
0N/A *
0N/A * @see Calendar#complete
0N/A */
0N/A protected void computeFields() {
0N/A int mask = 0;
0N/A if (isPartiallyNormalized()) {
0N/A // Determine which calendar fields need to be computed.
0N/A mask = getSetStateFields();
0N/A int fieldMask = ~mask & ALL_FIELDS;
0N/A // We have to call computTime in case calsys == null in
0N/A // order to set calsys and cdate. (6263644)
0N/A if (fieldMask != 0 || calsys == null) {
0N/A mask |= computeFields(fieldMask,
0N/A mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));
0N/A assert mask == ALL_FIELDS;
0N/A }
0N/A } else {
0N/A mask = ALL_FIELDS;
0N/A computeFields(mask, 0);
0N/A }
0N/A // After computing all the fields, set the field state to `COMPUTED'.
0N/A setFieldsComputed(mask);
0N/A }
0N/A
0N/A /**
0N/A * This computeFields implements the conversion from UTC
0N/A * (millisecond offset from the Epoch) to calendar
0N/A * field values. fieldMask specifies which fields to change the
0N/A * setting state to COMPUTED, although all fields are set to
0N/A * the correct values. This is required to fix 4685354.
0N/A *
0N/A * @param fieldMask a bit mask to specify which fields to change
0N/A * the setting state.
0N/A * @param tzMask a bit mask to specify which time zone offset
0N/A * fields to be used for time calculations
0N/A * @return a new field mask that indicates what field values have
0N/A * actually been set.
0N/A */
0N/A private int computeFields(int fieldMask, int tzMask) {
0N/A int zoneOffset = 0;
0N/A TimeZone tz = getZone();
0N/A if (zoneOffsets == null) {
0N/A zoneOffsets = new int[2];
0N/A }
0N/A if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
0N/A if (tz instanceof ZoneInfo) {
0N/A zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);
0N/A } else {
0N/A zoneOffset = tz.getOffset(time);
0N/A zoneOffsets[0] = tz.getRawOffset();
0N/A zoneOffsets[1] = zoneOffset - zoneOffsets[0];
0N/A }
0N/A }
0N/A if (tzMask != 0) {
0N/A if (isFieldSet(tzMask, ZONE_OFFSET)) {
0N/A zoneOffsets[0] = internalGet(ZONE_OFFSET);
0N/A }
0N/A if (isFieldSet(tzMask, DST_OFFSET)) {
0N/A zoneOffsets[1] = internalGet(DST_OFFSET);
0N/A }
0N/A zoneOffset = zoneOffsets[0] + zoneOffsets[1];
0N/A }
0N/A
0N/A // By computing time and zoneOffset separately, we can take
0N/A // the wider range of time+zoneOffset than the previous
0N/A // implementation.
0N/A long fixedDate = zoneOffset / ONE_DAY;
0N/A int timeOfDay = zoneOffset % (int)ONE_DAY;
0N/A fixedDate += time / ONE_DAY;
0N/A timeOfDay += (int) (time % ONE_DAY);
0N/A if (timeOfDay >= ONE_DAY) {
0N/A timeOfDay -= ONE_DAY;
0N/A ++fixedDate;
0N/A } else {
0N/A while (timeOfDay < 0) {
0N/A timeOfDay += ONE_DAY;
0N/A --fixedDate;
0N/A }
0N/A }
0N/A fixedDate += EPOCH_OFFSET;
0N/A
0N/A int era = CE;
0N/A int year;
0N/A if (fixedDate >= gregorianCutoverDate) {
0N/A // Handle Gregorian dates.
0N/A assert cachedFixedDate == Long.MIN_VALUE || gdate.isNormalized()
0N/A : "cache control: not normalized";
0N/A assert cachedFixedDate == Long.MIN_VALUE ||
0N/A gcal.getFixedDate(gdate.getNormalizedYear(),
0N/A gdate.getMonth(),
0N/A gdate.getDayOfMonth(), gdate)
0N/A == cachedFixedDate
0N/A : "cache control: inconsictency" +
0N/A ", cachedFixedDate=" + cachedFixedDate +
0N/A ", computed=" +
0N/A gcal.getFixedDate(gdate.getNormalizedYear(),
0N/A gdate.getMonth(),
0N/A gdate.getDayOfMonth(),
0N/A gdate) +
0N/A ", date=" + gdate;
0N/A
0N/A // See if we can use gdate to avoid date calculation.
0N/A if (fixedDate != cachedFixedDate) {
0N/A gcal.getCalendarDateFromFixedDate(gdate, fixedDate);
0N/A cachedFixedDate = fixedDate;
0N/A }
0N/A
0N/A year = gdate.getYear();
0N/A if (year <= 0) {
0N/A year = 1 - year;
0N/A era = BCE;
0N/A }
0N/A calsys = gcal;
0N/A cdate = gdate;
0N/A assert cdate.getDayOfWeek() > 0 : "dow="+cdate.getDayOfWeek()+", date="+cdate;
0N/A } else {
0N/A // Handle Julian calendar dates.
0N/A calsys = getJulianCalendarSystem();
0N/A cdate = (BaseCalendar.Date) jcal.newCalendarDate(getZone());
0N/A jcal.getCalendarDateFromFixedDate(cdate, fixedDate);
0N/A Era e = cdate.getEra();
0N/A if (e == jeras[0]) {
0N/A era = BCE;
0N/A }
0N/A year = cdate.getYear();
0N/A }
0N/A
0N/A // Always set the ERA and YEAR values.
0N/A internalSet(ERA, era);
0N/A internalSet(YEAR, year);
0N/A int mask = fieldMask | (ERA_MASK|YEAR_MASK);
0N/A
0N/A int month = cdate.getMonth() - 1; // 0-based
0N/A int dayOfMonth = cdate.getDayOfMonth();
0N/A
0N/A // Set the basic date fields.
0N/A if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))
0N/A != 0) {
0N/A internalSet(MONTH, month);
0N/A internalSet(DAY_OF_MONTH, dayOfMonth);
0N/A internalSet(DAY_OF_WEEK, cdate.getDayOfWeek());
0N/A mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;
0N/A }
0N/A
0N/A if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
0N/A |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {
0N/A if (timeOfDay != 0) {
0N/A int hours = timeOfDay / ONE_HOUR;
0N/A internalSet(HOUR_OF_DAY, hours);
0N/A internalSet(AM_PM, hours / 12); // Assume AM == 0
0N/A internalSet(HOUR, hours % 12);
0N/A int r = timeOfDay % ONE_HOUR;
0N/A internalSet(MINUTE, r / ONE_MINUTE);
0N/A r %= ONE_MINUTE;
0N/A internalSet(SECOND, r / ONE_SECOND);
0N/A internalSet(MILLISECOND, r % ONE_SECOND);
0N/A } else {
0N/A internalSet(HOUR_OF_DAY, 0);
0N/A internalSet(AM_PM, AM);
0N/A internalSet(HOUR, 0);
0N/A internalSet(MINUTE, 0);
0N/A internalSet(SECOND, 0);
0N/A internalSet(MILLISECOND, 0);
0N/A }
0N/A mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
0N/A |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);
0N/A }
0N/A
0N/A if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {
0N/A internalSet(ZONE_OFFSET, zoneOffsets[0]);
0N/A internalSet(DST_OFFSET, zoneOffsets[1]);
0N/A mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
0N/A }
0N/A
0N/A if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
0N/A int normalizedYear = cdate.getNormalizedYear();
0N/A long fixedDateJan1 = calsys.getFixedDate(normalizedYear, 1, 1, cdate);
0N/A int dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
0N/A long fixedDateMonth1 = fixedDate - dayOfMonth + 1;
0N/A int cutoverGap = 0;
0N/A int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
0N/A int relativeDayOfMonth = dayOfMonth - 1;
0N/A
0N/A // If we are in the cutover year, we need some special handling.
0N/A if (normalizedYear == cutoverYear) {
0N/A // Need to take care of the "missing" days.
2702N/A if (gregorianCutoverYearJulian <= gregorianCutoverYear) {
0N/A // We need to find out where we are. The cutover
0N/A // gap could even be more than one year. (One
0N/A // year difference in ~48667 years.)
0N/A fixedDateJan1 = getFixedDateJan1(cdate, fixedDate);
0N/A if (fixedDate >= gregorianCutoverDate) {
0N/A fixedDateMonth1 = getFixedDateMonth1(cdate, fixedDate);
0N/A }
0N/A }
0N/A int realDayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
0N/A cutoverGap = dayOfYear - realDayOfYear;
0N/A dayOfYear = realDayOfYear;
0N/A relativeDayOfMonth = (int)(fixedDate - fixedDateMonth1);
0N/A }
0N/A internalSet(DAY_OF_YEAR, dayOfYear);
0N/A internalSet(DAY_OF_WEEK_IN_MONTH, relativeDayOfMonth / 7 + 1);
0N/A
0N/A int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
0N/A
0N/A // The spec is to calculate WEEK_OF_YEAR in the
0N/A // ISO8601-style. This creates problems, though.
0N/A if (weekOfYear == 0) {
0N/A // If the date belongs to the last week of the
0N/A // previous year, use the week number of "12/31" of
0N/A // the "previous" year. Again, if the previous year is
0N/A // the Gregorian cutover year, we need to take care of
0N/A // it. Usually the previous day of January 1 is
0N/A // December 31, which is not always true in
0N/A // GregorianCalendar.
0N/A long fixedDec31 = fixedDateJan1 - 1;
2702N/A long prevJan1 = fixedDateJan1 - 365;
0N/A if (normalizedYear > (cutoverYear + 1)) {
0N/A if (CalendarUtils.isGregorianLeapYear(normalizedYear - 1)) {
0N/A --prevJan1;
0N/A }
2702N/A } else if (normalizedYear <= gregorianCutoverYearJulian) {
2702N/A if (CalendarUtils.isJulianLeapYear(normalizedYear - 1)) {
2702N/A --prevJan1;
2702N/A }
0N/A } else {
0N/A BaseCalendar calForJan1 = calsys;
2702N/A //int prevYear = normalizedYear - 1;
2702N/A int prevYear = getCalendarDate(fixedDec31).getNormalizedYear();
2702N/A if (prevYear == gregorianCutoverYear) {
0N/A calForJan1 = getCutoverCalendarSystem();
2702N/A if (calForJan1 == jcal) {
2702N/A prevJan1 = calForJan1.getFixedDate(prevYear,
2702N/A BaseCalendar.JANUARY,
2702N/A 1,
2702N/A null);
2702N/A } else {
2702N/A prevJan1 = gregorianCutoverDate;
2702N/A calForJan1 = gcal;
2702N/A }
2702N/A } else if (prevYear <= gregorianCutoverYearJulian) {
2702N/A calForJan1 = getJulianCalendarSystem();
2702N/A prevJan1 = calForJan1.getFixedDate(prevYear,
2702N/A BaseCalendar.JANUARY,
2702N/A 1,
2702N/A null);
0N/A }
0N/A }
0N/A weekOfYear = getWeekNumber(prevJan1, fixedDec31);
0N/A } else {
0N/A if (normalizedYear > gregorianCutoverYear ||
0N/A normalizedYear < (gregorianCutoverYearJulian - 1)) {
0N/A // Regular years
0N/A if (weekOfYear >= 52) {
0N/A long nextJan1 = fixedDateJan1 + 365;
0N/A if (cdate.isLeapYear()) {
0N/A nextJan1++;
0N/A }
0N/A long nextJan1st = calsys.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
0N/A getFirstDayOfWeek());
0N/A int ndays = (int)(nextJan1st - nextJan1);
0N/A if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
0N/A // The first days forms a week in which the date is included.
0N/A weekOfYear = 1;
0N/A }
0N/A }
0N/A } else {
0N/A BaseCalendar calForJan1 = calsys;
0N/A int nextYear = normalizedYear + 1;
0N/A if (nextYear == (gregorianCutoverYearJulian + 1) &&
0N/A nextYear < gregorianCutoverYear) {
0N/A // In case the gap is more than one year.
0N/A nextYear = gregorianCutoverYear;
0N/A }
0N/A if (nextYear == gregorianCutoverYear) {
0N/A calForJan1 = getCutoverCalendarSystem();
0N/A }
2702N/A
2702N/A long nextJan1;
2702N/A if (nextYear > gregorianCutoverYear
2702N/A || gregorianCutoverYearJulian == gregorianCutoverYear
2702N/A || nextYear == gregorianCutoverYearJulian) {
2702N/A nextJan1 = calForJan1.getFixedDate(nextYear,
2702N/A BaseCalendar.JANUARY,
2702N/A 1,
2702N/A null);
2702N/A } else {
0N/A nextJan1 = gregorianCutoverDate;
0N/A calForJan1 = gcal;
0N/A }
2702N/A
0N/A long nextJan1st = calForJan1.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
0N/A getFirstDayOfWeek());
0N/A int ndays = (int)(nextJan1st - nextJan1);
0N/A if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
0N/A // The first days forms a week in which the date is included.
0N/A weekOfYear = 1;
0N/A }
0N/A }
0N/A }
0N/A internalSet(WEEK_OF_YEAR, weekOfYear);
0N/A internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
0N/A mask |= (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK|WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK);
0N/A }
0N/A return mask;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of weeks in a period between fixedDay1 and
0N/A * fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule
0N/A * is applied to calculate the number of weeks.
0N/A *
0N/A * @param fixedDay1 the fixed date of the first day of the period
0N/A * @param fixedDate the fixed date of the last day of the period
0N/A * @return the number of weeks of the given period
0N/A */
0N/A private final int getWeekNumber(long fixedDay1, long fixedDate) {
0N/A // We can always use `gcal' since Julian and Gregorian are the
0N/A // same thing for this calculation.
0N/A long fixedDay1st = gcal.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,
0N/A getFirstDayOfWeek());
0N/A int ndays = (int)(fixedDay1st - fixedDay1);
0N/A assert ndays <= 7;
0N/A if (ndays >= getMinimalDaysInFirstWeek()) {
0N/A fixedDay1st -= 7;
0N/A }
0N/A int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);
0N/A if (normalizedDayOfPeriod >= 0) {
0N/A return normalizedDayOfPeriod / 7 + 1;
0N/A }
0N/A return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;
0N/A }
0N/A
0N/A /**
0N/A * Converts calendar field values to the time value (millisecond
0N/A * offset from the <a href="Calendar.html#Epoch">Epoch</a>).
0N/A *
0N/A * @exception IllegalArgumentException if any calendar fields are invalid.
0N/A */
0N/A protected void computeTime() {
0N/A // In non-lenient mode, perform brief checking of calendar
0N/A // fields which have been set externally. Through this
0N/A // checking, the field values are stored in originalFields[]
0N/A // to see if any of them are normalized later.
0N/A if (!isLenient()) {
0N/A if (originalFields == null) {
0N/A originalFields = new int[FIELD_COUNT];
0N/A }
0N/A for (int field = 0; field < FIELD_COUNT; field++) {
0N/A int value = internalGet(field);
0N/A if (isExternallySet(field)) {
0N/A // Quick validation for any out of range values
0N/A if (value < getMinimum(field) || value > getMaximum(field)) {
0N/A throw new IllegalArgumentException(getFieldName(field));
0N/A }
0N/A }
0N/A originalFields[field] = value;
0N/A }
0N/A }
0N/A
0N/A // Let the super class determine which calendar fields to be
0N/A // used to calculate the time.
0N/A int fieldMask = selectFields();
0N/A
0N/A // The year defaults to the epoch start. We don't check
0N/A // fieldMask for YEAR because YEAR is a mandatory field to
0N/A // determine the date.
0N/A int year = isSet(YEAR) ? internalGet(YEAR) : EPOCH_YEAR;
0N/A
0N/A int era = internalGetEra();
0N/A if (era == BCE) {
0N/A year = 1 - year;
0N/A } else if (era != CE) {
0N/A // Even in lenient mode we disallow ERA values other than CE & BCE.
0N/A // (The same normalization rule as add()/roll() could be
0N/A // applied here in lenient mode. But this checking is kept
0N/A // unchanged for compatibility as of 1.5.)
0N/A throw new IllegalArgumentException("Invalid era");
0N/A }
0N/A
0N/A // If year is 0 or negative, we need to set the ERA value later.
0N/A if (year <= 0 && !isSet(ERA)) {
0N/A fieldMask |= ERA_MASK;
0N/A setFieldsComputed(ERA_MASK);
0N/A }
0N/A
0N/A // Calculate the time of day. We rely on the convention that
0N/A // an UNSET field has 0.
0N/A long timeOfDay = 0;
0N/A if (isFieldSet(fieldMask, HOUR_OF_DAY)) {
0N/A timeOfDay += (long) internalGet(HOUR_OF_DAY);
0N/A } else {
0N/A timeOfDay += internalGet(HOUR);
0N/A // The default value of AM_PM is 0 which designates AM.
0N/A if (isFieldSet(fieldMask, AM_PM)) {
0N/A timeOfDay += 12 * internalGet(AM_PM);
0N/A }
0N/A }
0N/A timeOfDay *= 60;
0N/A timeOfDay += internalGet(MINUTE);
0N/A timeOfDay *= 60;
0N/A timeOfDay += internalGet(SECOND);
0N/A timeOfDay *= 1000;
0N/A timeOfDay += internalGet(MILLISECOND);
0N/A
0N/A // Convert the time of day to the number of days and the
0N/A // millisecond offset from midnight.
0N/A long fixedDate = timeOfDay / ONE_DAY;
0N/A timeOfDay %= ONE_DAY;
0N/A while (timeOfDay < 0) {
0N/A timeOfDay += ONE_DAY;
0N/A --fixedDate;
0N/A }
0N/A
0N/A // Calculate the fixed date since January 1, 1 (Gregorian).
0N/A calculateFixedDate: {
0N/A long gfd, jfd;
0N/A if (year > gregorianCutoverYear && year > gregorianCutoverYearJulian) {
0N/A gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
0N/A if (gfd >= gregorianCutoverDate) {
0N/A fixedDate = gfd;
0N/A break calculateFixedDate;
0N/A }
0N/A jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
0N/A } else if (year < gregorianCutoverYear && year < gregorianCutoverYearJulian) {
0N/A jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
0N/A if (jfd < gregorianCutoverDate) {
0N/A fixedDate = jfd;
0N/A break calculateFixedDate;
0N/A }
0N/A gfd = jfd;
0N/A } else {
2702N/A jfd = fixedDate + getFixedDate(getJulianCalendarSystem(), year, fieldMask);
0N/A gfd = fixedDate + getFixedDate(gcal, year, fieldMask);
0N/A }
2702N/A
0N/A // Now we have to determine which calendar date it is.
2702N/A
2702N/A // If the date is relative from the beginning of the year
2702N/A // in the Julian calendar, then use jfd;
2702N/A if (isFieldSet(fieldMask, DAY_OF_YEAR) || isFieldSet(fieldMask, WEEK_OF_YEAR)) {
2702N/A if (gregorianCutoverYear == gregorianCutoverYearJulian) {
2702N/A fixedDate = jfd;
2702N/A break calculateFixedDate;
2702N/A } else if (year == gregorianCutoverYear) {
2702N/A fixedDate = gfd;
2702N/A break calculateFixedDate;
2702N/A }
2702N/A }
2702N/A
0N/A if (gfd >= gregorianCutoverDate) {
0N/A if (jfd >= gregorianCutoverDate) {
0N/A fixedDate = gfd;
0N/A } else {
0N/A // The date is in an "overlapping" period. No way
0N/A // to disambiguate it. Determine it using the
0N/A // previous date calculation.
0N/A if (calsys == gcal || calsys == null) {
0N/A fixedDate = gfd;
0N/A } else {
0N/A fixedDate = jfd;
0N/A }
0N/A }
0N/A } else {
0N/A if (jfd < gregorianCutoverDate) {
0N/A fixedDate = jfd;
0N/A } else {
0N/A // The date is in a "missing" period.
0N/A if (!isLenient()) {
0N/A throw new IllegalArgumentException("the specified date doesn't exist");
0N/A }
0N/A // Take the Julian date for compatibility, which
0N/A // will produce a Gregorian date.
0N/A fixedDate = jfd;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // millis represents local wall-clock time in milliseconds.
0N/A long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;
0N/A
0N/A // Compute the time zone offset and DST offset. There are two potential
0N/A // ambiguities here. We'll assume a 2:00 am (wall time) switchover time
0N/A // for discussion purposes here.
0N/A // 1. The transition into DST. Here, a designated time of 2:00 am - 2:59 am
0N/A // can be in standard or in DST depending. However, 2:00 am is an invalid
0N/A // representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).
0N/A // We assume standard time.
0N/A // 2. The transition out of DST. Here, a designated time of 1:00 am - 1:59 am
0N/A // can be in standard or DST. Both are valid representations (the rep
0N/A // jumps from 1:59:59 DST to 1:00:00 Std).
0N/A // Again, we assume standard time.
0N/A // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET
0N/A // or DST_OFFSET fields; then we use those fields.
0N/A TimeZone zone = getZone();
0N/A if (zoneOffsets == null) {
0N/A zoneOffsets = new int[2];
0N/A }
0N/A int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
0N/A if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
0N/A if (zone instanceof ZoneInfo) {
0N/A ((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets);
0N/A } else {
0N/A int gmtOffset = isFieldSet(fieldMask, ZONE_OFFSET) ?
0N/A internalGet(ZONE_OFFSET) : zone.getRawOffset();
0N/A zone.getOffsets(millis - gmtOffset, zoneOffsets);
0N/A }
0N/A }
0N/A if (tzMask != 0) {
0N/A if (isFieldSet(tzMask, ZONE_OFFSET)) {
0N/A zoneOffsets[0] = internalGet(ZONE_OFFSET);
0N/A }
0N/A if (isFieldSet(tzMask, DST_OFFSET)) {
0N/A zoneOffsets[1] = internalGet(DST_OFFSET);
0N/A }
0N/A }
0N/A
0N/A // Adjust the time zone offset values to get the UTC time.
0N/A millis -= zoneOffsets[0] + zoneOffsets[1];
0N/A
0N/A // Set this calendar's time in milliseconds
0N/A time = millis;
0N/A
0N/A int mask = computeFields(fieldMask | getSetStateFields(), tzMask);
0N/A
0N/A if (!isLenient()) {
0N/A for (int field = 0; field < FIELD_COUNT; field++) {
0N/A if (!isExternallySet(field)) {
0N/A continue;
0N/A }
0N/A if (originalFields[field] != internalGet(field)) {
2702N/A String s = originalFields[field] + " -> " + internalGet(field);
0N/A // Restore the original field values
0N/A System.arraycopy(originalFields, 0, fields, 0, fields.length);
2702N/A throw new IllegalArgumentException(getFieldName(field) + ": " + s);
0N/A }
0N/A }
0N/A }
0N/A setFieldsNormalized(mask);
0N/A }
0N/A
0N/A /**
0N/A * Computes the fixed date under either the Gregorian or the
0N/A * Julian calendar, using the given year and the specified calendar fields.
0N/A *
0N/A * @param cal the CalendarSystem to be used for the date calculation
0N/A * @param year the normalized year number, with 0 indicating the
0N/A * year 1 BCE, -1 indicating 2 BCE, etc.
0N/A * @param fieldMask the calendar fields to be used for the date calculation
0N/A * @return the fixed date
0N/A * @see Calendar#selectFields
0N/A */
0N/A private long getFixedDate(BaseCalendar cal, int year, int fieldMask) {
0N/A int month = JANUARY;
0N/A if (isFieldSet(fieldMask, MONTH)) {
0N/A // No need to check if MONTH has been set (no isSet(MONTH)
0N/A // call) since its unset value happens to be JANUARY (0).
0N/A month = internalGet(MONTH);
0N/A
0N/A // If the month is out of range, adjust it into range
0N/A if (month > DECEMBER) {
0N/A year += month / 12;
0N/A month %= 12;
0N/A } else if (month < JANUARY) {
0N/A int[] rem = new int[1];
0N/A year += CalendarUtils.floorDivide(month, 12, rem);
0N/A month = rem[0];
0N/A }
0N/A }
0N/A
0N/A // Get the fixed date since Jan 1, 1 (Gregorian). We are on
0N/A // the first day of either `month' or January in 'year'.
0N/A long fixedDate = cal.getFixedDate(year, month + 1, 1,
0N/A cal == gcal ? gdate : null);
0N/A if (isFieldSet(fieldMask, MONTH)) {
0N/A // Month-based calculations
0N/A if (isFieldSet(fieldMask, DAY_OF_MONTH)) {
0N/A // We are on the first day of the month. Just add the
0N/A // offset if DAY_OF_MONTH is set. If the isSet call
0N/A // returns false, that means DAY_OF_MONTH has been
0N/A // selected just because of the selected
0N/A // combination. We don't need to add any since the
0N/A // default value is the 1st.
0N/A if (isSet(DAY_OF_MONTH)) {
0N/A // To avoid underflow with DAY_OF_MONTH-1, add
0N/A // DAY_OF_MONTH, then subtract 1.
0N/A fixedDate += internalGet(DAY_OF_MONTH);
0N/A fixedDate--;
0N/A }
0N/A } else {
0N/A if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {
0N/A long firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(fixedDate + 6,
0N/A getFirstDayOfWeek());
0N/A // If we have enough days in the first week, then
0N/A // move to the previous week.
0N/A if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
0N/A firstDayOfWeek -= 7;
0N/A }
0N/A if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
0N/A firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
0N/A internalGet(DAY_OF_WEEK));
0N/A }
0N/A // In lenient mode, we treat days of the previous
0N/A // months as a part of the specified
0N/A // WEEK_OF_MONTH. See 4633646.
0N/A fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);
0N/A } else {
0N/A int dayOfWeek;
0N/A if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
0N/A dayOfWeek = internalGet(DAY_OF_WEEK);
0N/A } else {
0N/A dayOfWeek = getFirstDayOfWeek();
0N/A }
0N/A // We are basing this on the day-of-week-in-month. The only
0N/A // trickiness occurs if the day-of-week-in-month is
0N/A // negative.
0N/A int dowim;
0N/A if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {
0N/A dowim = internalGet(DAY_OF_WEEK_IN_MONTH);
0N/A } else {
0N/A dowim = 1;
0N/A }
0N/A if (dowim >= 0) {
0N/A fixedDate = cal.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,
0N/A dayOfWeek);
0N/A } else {
0N/A // Go to the first day of the next week of
0N/A // the specified week boundary.
0N/A int lastDate = monthLength(month, year) + (7 * (dowim + 1));
0N/A // Then, get the day of week date on or before the last date.
0N/A fixedDate = cal.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,
0N/A dayOfWeek);
0N/A }
0N/A }
0N/A }
0N/A } else {
0N/A if (year == gregorianCutoverYear && cal == gcal
0N/A && fixedDate < gregorianCutoverDate
0N/A && gregorianCutoverYear != gregorianCutoverYearJulian) {
0N/A // January 1 of the year doesn't exist. Use
0N/A // gregorianCutoverDate as the first day of the
0N/A // year.
0N/A fixedDate = gregorianCutoverDate;
0N/A }
0N/A // We are on the first day of the year.
0N/A if (isFieldSet(fieldMask, DAY_OF_YEAR)) {
0N/A // Add the offset, then subtract 1. (Make sure to avoid underflow.)
0N/A fixedDate += internalGet(DAY_OF_YEAR);
0N/A fixedDate--;
0N/A } else {
0N/A long firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(fixedDate + 6,
0N/A getFirstDayOfWeek());
0N/A // If we have enough days in the first week, then move
0N/A // to the previous week.
0N/A if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
0N/A firstDayOfWeek -= 7;
0N/A }
0N/A if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
0N/A int dayOfWeek = internalGet(DAY_OF_WEEK);
0N/A if (dayOfWeek != getFirstDayOfWeek()) {
0N/A firstDayOfWeek = cal.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
0N/A dayOfWeek);
0N/A }
0N/A }
0N/A fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);
0N/A }
0N/A }
0N/A
0N/A return fixedDate;
0N/A }
0N/A
0N/A /**
0N/A * Returns this object if it's normalized (all fields and time are
0N/A * in sync). Otherwise, a cloned object is returned after calling
0N/A * complete() in lenient mode.
0N/A */
0N/A private final GregorianCalendar getNormalizedCalendar() {
0N/A GregorianCalendar gc;
0N/A if (isFullyNormalized()) {
0N/A gc = this;
0N/A } else {
0N/A // Create a clone and normalize the calendar fields
0N/A gc = (GregorianCalendar) this.clone();
0N/A gc.setLenient(true);
0N/A gc.complete();
0N/A }
0N/A return gc;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Julian calendar system instance (singleton). 'jcal'
0N/A * and 'jeras' are set upon the return.
0N/A */
0N/A synchronized private static final BaseCalendar getJulianCalendarSystem() {
0N/A if (jcal == null) {
0N/A jcal = (JulianCalendar) CalendarSystem.forName("julian");
0N/A jeras = jcal.getEras();
0N/A }
0N/A return jcal;
0N/A }
0N/A
0N/A /**
0N/A * Returns the calendar system for dates before the cutover date
0N/A * in the cutover year. If the cutover date is January 1, the
0N/A * method returns Gregorian. Otherwise, Julian.
0N/A */
0N/A private BaseCalendar getCutoverCalendarSystem() {
2702N/A if (gregorianCutoverYearJulian < gregorianCutoverYear) {
0N/A return gcal;
0N/A }
0N/A return getJulianCalendarSystem();
0N/A }
0N/A
0N/A /**
0N/A * Determines if the specified year (normalized) is the Gregorian
0N/A * cutover year. This object must have been normalized.
0N/A */
0N/A private final boolean isCutoverYear(int normalizedYear) {
0N/A int cutoverYear = (calsys == gcal) ? gregorianCutoverYear : gregorianCutoverYearJulian;
0N/A return normalizedYear == cutoverYear;
0N/A }
0N/A
0N/A /**
0N/A * Returns the fixed date of the first day of the year (usually
0N/A * January 1) before the specified date.
0N/A *
0N/A * @param date the date for which the first day of the year is
0N/A * calculated. The date has to be in the cut-over year (Gregorian
0N/A * or Julian).
0N/A * @param fixedDate the fixed date representation of the date
0N/A */
0N/A private final long getFixedDateJan1(BaseCalendar.Date date, long fixedDate) {
0N/A assert date.getNormalizedYear() == gregorianCutoverYear ||
0N/A date.getNormalizedYear() == gregorianCutoverYearJulian;
0N/A if (gregorianCutoverYear != gregorianCutoverYearJulian) {
0N/A if (fixedDate >= gregorianCutoverDate) {
0N/A // Dates before the cutover date don't exist
0N/A // in the same (Gregorian) year. So, no
0N/A // January 1 exists in the year. Use the
0N/A // cutover date as the first day of the year.
0N/A return gregorianCutoverDate;
0N/A }
0N/A }
0N/A // January 1 of the normalized year should exist.
0N/A BaseCalendar jcal = getJulianCalendarSystem();
0N/A return jcal.getFixedDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1, null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the fixed date of the first date of the month (usually
0N/A * the 1st of the month) before the specified date.
0N/A *
0N/A * @param date the date for which the first day of the month is
0N/A * calculated. The date has to be in the cut-over year (Gregorian
0N/A * or Julian).
0N/A * @param fixedDate the fixed date representation of the date
0N/A */
0N/A private final long getFixedDateMonth1(BaseCalendar.Date date, long fixedDate) {
0N/A assert date.getNormalizedYear() == gregorianCutoverYear ||
0N/A date.getNormalizedYear() == gregorianCutoverYearJulian;
0N/A BaseCalendar.Date gCutover = getGregorianCutoverDate();
0N/A if (gCutover.getMonth() == BaseCalendar.JANUARY
0N/A && gCutover.getDayOfMonth() == 1) {
0N/A // The cutover happened on January 1.
0N/A return fixedDate - date.getDayOfMonth() + 1;
0N/A }
0N/A
0N/A long fixedDateMonth1;
0N/A // The cutover happened sometime during the year.
0N/A if (date.getMonth() == gCutover.getMonth()) {
0N/A // The cutover happened in the month.
0N/A BaseCalendar.Date jLastDate = getLastJulianDate();
0N/A if (gregorianCutoverYear == gregorianCutoverYearJulian
0N/A && gCutover.getMonth() == jLastDate.getMonth()) {
0N/A // The "gap" fits in the same month.
0N/A fixedDateMonth1 = jcal.getFixedDate(date.getNormalizedYear(),
0N/A date.getMonth(),
0N/A 1,
0N/A null);
0N/A } else {
0N/A // Use the cutover date as the first day of the month.
0N/A fixedDateMonth1 = gregorianCutoverDate;
0N/A }
0N/A } else {
0N/A // The cutover happened before the month.
0N/A fixedDateMonth1 = fixedDate - date.getDayOfMonth() + 1;
0N/A }
0N/A
0N/A return fixedDateMonth1;
0N/A }
0N/A
0N/A /**
0N/A * Returns a CalendarDate produced from the specified fixed date.
0N/A *
0N/A * @param fd the fixed date
0N/A */
0N/A private final BaseCalendar.Date getCalendarDate(long fd) {
0N/A BaseCalendar cal = (fd >= gregorianCutoverDate) ? gcal : getJulianCalendarSystem();
0N/A BaseCalendar.Date d = (BaseCalendar.Date) cal.newCalendarDate(TimeZone.NO_TIMEZONE);
0N/A cal.getCalendarDateFromFixedDate(d, fd);
0N/A return d;
0N/A }
0N/A
0N/A /**
0N/A * Returns the Gregorian cutover date as a BaseCalendar.Date. The
0N/A * date is a Gregorian date.
0N/A */
0N/A private final BaseCalendar.Date getGregorianCutoverDate() {
0N/A return getCalendarDate(gregorianCutoverDate);
0N/A }
0N/A
0N/A /**
0N/A * Returns the day before the Gregorian cutover date as a
0N/A * BaseCalendar.Date. The date is a Julian date.
0N/A */
0N/A private final BaseCalendar.Date getLastJulianDate() {
0N/A return getCalendarDate(gregorianCutoverDate - 1);
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of the specified month in the specified
0N/A * year. The year number must be normalized.
0N/A *
0N/A * @see #isLeapYear(int)
0N/A */
0N/A private final int monthLength(int month, int year) {
0N/A return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];
0N/A }
0N/A
0N/A /**
0N/A * Returns the length of the specified month in the year provided
0N/A * by internalGet(YEAR).
0N/A *
0N/A * @see #isLeapYear(int)
0N/A */
0N/A private final int monthLength(int month) {
0N/A int year = internalGet(YEAR);
0N/A if (internalGetEra() == BCE) {
0N/A year = 1 - year;
0N/A }
0N/A return monthLength(month, year);
0N/A }
0N/A
0N/A private final int actualMonthLength() {
0N/A int year = cdate.getNormalizedYear();
0N/A if (year != gregorianCutoverYear && year != gregorianCutoverYearJulian) {
0N/A return calsys.getMonthLength(cdate);
0N/A }
0N/A BaseCalendar.Date date = (BaseCalendar.Date) cdate.clone();
0N/A long fd = calsys.getFixedDate(date);
0N/A long month1 = getFixedDateMonth1(date, fd);
0N/A long next1 = month1 + calsys.getMonthLength(date);
0N/A if (next1 < gregorianCutoverDate) {
0N/A return (int)(next1 - month1);
0N/A }
0N/A if (cdate != gdate) {
0N/A date = (BaseCalendar.Date) gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
0N/A }
0N/A gcal.getCalendarDateFromFixedDate(date, next1);
0N/A next1 = getFixedDateMonth1(date, next1);
0N/A return (int)(next1 - month1);
0N/A }
0N/A
0N/A /**
0N/A * Returns the length (in days) of the specified year. The year
0N/A * must be normalized.
0N/A */
0N/A private final int yearLength(int year) {
0N/A return isLeapYear(year) ? 366 : 365;
0N/A }
0N/A
0N/A /**
0N/A * Returns the length (in days) of the year provided by
0N/A * internalGet(YEAR).
0N/A */
0N/A private final int yearLength() {
0N/A int year = internalGet(YEAR);
0N/A if (internalGetEra() == BCE) {
0N/A year = 1 - year;
0N/A }
0N/A return yearLength(year);
0N/A }
0N/A
0N/A /**
0N/A * After adjustments such as add(MONTH), add(YEAR), we don't want the
0N/A * month to jump around. E.g., we don't want Jan 31 + 1 month to go to Mar
0N/A * 3, we want it to go to Feb 28. Adjustments which might run into this
0N/A * problem call this method to retain the proper month.
0N/A */
0N/A private final void pinDayOfMonth() {
0N/A int year = internalGet(YEAR);
0N/A int monthLen;
0N/A if (year > gregorianCutoverYear || year < gregorianCutoverYearJulian) {
0N/A monthLen = monthLength(internalGet(MONTH));
0N/A } else {
0N/A GregorianCalendar gc = getNormalizedCalendar();
0N/A monthLen = gc.getActualMaximum(DAY_OF_MONTH);
0N/A }
0N/A int dom = internalGet(DAY_OF_MONTH);
0N/A if (dom > monthLen) {
0N/A set(DAY_OF_MONTH, monthLen);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the fixed date value of this object. The time value and
0N/A * calendar fields must be in synch.
0N/A */
0N/A private final long getCurrentFixedDate() {
0N/A return (calsys == gcal) ? cachedFixedDate : calsys.getFixedDate(cdate);
0N/A }
0N/A
0N/A /**
0N/A * Returns the new value after 'roll'ing the specified value and amount.
0N/A */
0N/A private static final int getRolledValue(int value, int amount, int min, int max) {
0N/A assert value >= min && value <= max;
0N/A int range = max - min + 1;
0N/A amount %= range;
0N/A int n = value + amount;
0N/A if (n > max) {
0N/A n -= range;
0N/A } else if (n < min) {
0N/A n += range;
0N/A }
0N/A assert n >= min && n <= max;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ERA. We need a special method for this because the
0N/A * default ERA is CE, but a zero (unset) ERA is BCE.
0N/A */
0N/A private final int internalGetEra() {
0N/A return isSet(ERA) ? internalGet(ERA) : CE;
0N/A }
0N/A
0N/A /**
0N/A * Updates internal state.
0N/A */
0N/A private void readObject(ObjectInputStream stream)
0N/A throws IOException, ClassNotFoundException {
0N/A stream.defaultReadObject();
0N/A if (gdate == null) {
0N/A gdate = (BaseCalendar.Date) gcal.newCalendarDate(getZone());
0N/A cachedFixedDate = Long.MIN_VALUE;
0N/A }
0N/A setGregorianChange(gregorianCutover);
0N/A }
0N/A}