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