src/java.base/share/classes/java/util/JapaneseImperialCalendar.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58242 94bb65cb37d3
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
/*
53963
7f01a85f2710 8219890: Calendar.getDisplayName() returns empty string for new Japanese Era on some locales
naoto
parents: 50478
diff changeset
     2
 * Copyright (c) 2005, 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
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInputStream;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
    30
import sun.util.locale.provider.CalendarDataUtility;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.util.calendar.BaseCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.util.calendar.CalendarDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.util.calendar.CalendarSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.util.calendar.CalendarUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.util.calendar.Era;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.util.calendar.Gregorian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.util.calendar.LocalGregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.util.calendar.ZoneInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    41
 * {@code JapaneseImperialCalendar} implements a Japanese
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * calendar system in which the imperial era-based year numbering is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * supported from the Meiji era. The following are the eras supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * by this calendar system.
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    45
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * ERA value   Era name    Since (in Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * ------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     0       N/A         N/A
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    49
 *     1       Meiji       1868-01-01T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    50
 *     2       Taisho      1912-07-30T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    51
 *     3       Showa       1926-12-25T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    52
 *     4       Heisei      1989-01-08T00:00:00 local time
54359
3d8934bf505a 8205432: Replace the placeholder Japanese era name
naoto
parents: 53963
diff changeset
    53
 *     5       Reiwa       2019-05-01T00:00:00 local time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * ------------------------------------------------------
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    55
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
    57
 * <p>{@code ERA} value 0 specifies the years before Meiji and
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
    58
 * the Gregorian year values are used. Unlike
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
    59
 * {@link GregorianCalendar}, the Julian to Gregorian transition is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * supported because it doesn't make any sense to the Japanese
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * calendar systems used before Meiji. To represent the years before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Gregorian year 1, 0 and negative values are used. The Japanese
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Imperial rescripts and government decrees don't specify how to deal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * with time differences for applying the era transitions. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * calendar implementation assumes local time for all transitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    67
 * <p>A new era can be specified using property
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    68
 * jdk.calendar.japanese.supplemental.era. The new era is added to the
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    69
 * predefined eras. The syntax of the property is as follows.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
    70
 * <pre>
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    71
 *   {@code name=<name>,abbr=<abbr>,since=<time['u']>}
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    72
 * </pre>
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    73
 * where
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    74
 * <dl>
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
    75
 * <dt>{@code <name>:}<dd>the full name of the new era (non-ASCII characters allowed,
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
    76
 * either in platform's native encoding or in Unicode escape notation, {@code \\uXXXX})
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
    77
 * <dt>{@code <abbr>:}<dd>the abbreviation of the new era (non-ASCII characters allowed,
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
    78
 * either in platform's native encoding or in Unicode escape notation, {@code \\uXXXX})
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    79
 * <dt>{@code <time['u']>:}<dd>the start time of the new era represented by
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    80
 * milliseconds from 1970-01-01T00:00:00 local time or UTC if {@code 'u'} is
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    81
 * appended to the milliseconds value. (ASCII digits only)
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    82
 * </dl>
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    83
 *
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    84
 * <p>If the given era is invalid, such as the since value before the
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    85
 * beginning of the last predefined era, the given era will be
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    86
 * ignored.
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    87
 *
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    88
 * <p>The following is an example of the property usage.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
    89
 * <pre>
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    90
 *   java -Djdk.calendar.japanese.supplemental.era="name=NewEra,abbr=N,since=253374307200000"
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    91
 * </pre>
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    92
 * The property specifies an era change to NewEra at 9999-02-11T00:00:00 local time.
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
    93
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @author Masayoshi Okutsu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
class JapaneseImperialCalendar extends Calendar {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Implementation Notes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * This implementation uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * sun.util.calendar.LocalGregorianCalendar to perform most of the
27286
8299f6b0c5fc 8048124: Read hijra-config-umalqura.properties as a resource
rriggs
parents: 25991
diff changeset
   103
     * calendar calculations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The ERA constant designating the era before Meiji.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int BEFORE_MEIJI = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The ERA constant designating the Meiji era.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static final int MEIJI = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The ERA constant designating the Taisho era.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final int TAISHO = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * The ERA constant designating the Showa era.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static final int SHOWA = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The ERA constant designating the Heisei era.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static final int HEISEI = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   131
    /**
54359
3d8934bf505a 8205432: Replace the placeholder Japanese era name
naoto
parents: 53963
diff changeset
   132
     * The ERA constant designating the Reiwa era.
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   133
     */
54359
3d8934bf505a 8205432: Replace the placeholder Japanese era name
naoto
parents: 53963
diff changeset
   134
    private static final int REIWA = 5;
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static final int EPOCH_OFFSET   = 719163; // Fixed date of January 1, 1970 (Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // Useful millisecond constants.  Although ONE_DAY and ONE_WEEK can fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // into ints, they must be longs in order to prevent arithmetic overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // when performing (bug 4173516).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private static final int  ONE_SECOND = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static final int  ONE_MINUTE = 60*ONE_SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static final int  ONE_HOUR   = 60*ONE_MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static final long ONE_DAY    = 24*ONE_HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // Reference to the sun.util.calendar.LocalGregorianCalendar instance (singleton).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private static final LocalGregorianCalendar jcal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        = (LocalGregorianCalendar) CalendarSystem.forName("japanese");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // Gregorian calendar instance. This is required because era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // transition dates are given in Gregorian dates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final Gregorian gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // The Era instance representing "before Meiji".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final Era BEFORE_MEIJI_ERA = new Era("BeforeMeiji", "BM", Long.MIN_VALUE, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // Imperial eras. The sun.util.calendar.LocalGregorianCalendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // doesn't have an Era representing before Meiji, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // inconvenient for a Calendar. So, era[0] is a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // BEFORE_MEIJI_ERA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static final Era[] eras;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // Fixed date of the first date of each era.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final long[] sinceFixedDates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   166
    // The current era
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   167
    private static final int currentEra;
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   168
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *                                 Greatest       Least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Field name             Minimum   Minimum     Maximum     Maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * ----------             -------   -------     -------     -------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * ERA                          0         0           1           1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * YEAR                -292275055         1           ?           ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * MONTH                        0         0          11          11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * WEEK_OF_YEAR                 1         1          52*         53
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * WEEK_OF_MONTH                0         0           4*          6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * DAY_OF_MONTH                 1         1          28*         31
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * DAY_OF_YEAR                  1         1         365*        366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * DAY_OF_WEEK                  1         1           7           7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * DAY_OF_WEEK_IN_MONTH        -1        -1           4*          6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * AM_PM                        0         0           1           1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * HOUR                         0         0          11          11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * HOUR_OF_DAY                  0         0          23          23
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * MINUTE                       0         0          59          59
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * SECOND                       0         0          59          59
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * MILLISECOND                  0         0         999         999
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * ZONE_OFFSET             -13:00    -13:00       14:00       14:00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * DST_OFFSET                0:00      0:00        0:20        2:00
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * *: depends on eras
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    static final int MIN_VALUES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        0,              // ERA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        -292275055,     // YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        JANUARY,        // MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        1,              // WEEK_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        0,              // WEEK_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        1,              // DAY_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        1,              // DAY_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        SUNDAY,         // DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        1,              // DAY_OF_WEEK_IN_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        AM,             // AM_PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        0,              // HOUR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        0,              // HOUR_OF_DAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        0,              // MINUTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        0,              // SECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        0,              // MILLISECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        -13*ONE_HOUR,   // ZONE_OFFSET (UNIX compatibility)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        0               // DST_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static final int LEAST_MAX_VALUES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        0,              // ERA (initialized later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        0,              // YEAR (initialized later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        JANUARY,        // MONTH (Showa 64 ended in January.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        0,              // WEEK_OF_YEAR (Showa 1 has only 6 days which could be 0 weeks.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        4,              // WEEK_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        28,             // DAY_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        0,              // DAY_OF_YEAR (initialized later)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SATURDAY,       // DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        4,              // DAY_OF_WEEK_IN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        PM,             // AM_PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        11,             // HOUR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        23,             // HOUR_OF_DAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        59,             // MINUTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        59,             // SECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        999,            // MILLISECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        14*ONE_HOUR,    // ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        20*ONE_MINUTE   // DST_OFFSET (historical least maximum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static final int MAX_VALUES[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        0,              // ERA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        292278994,      // YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        DECEMBER,       // MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        53,             // WEEK_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        6,              // WEEK_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        31,             // DAY_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        366,            // DAY_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        SATURDAY,       // DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        6,              // DAY_OF_WEEK_IN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        PM,             // AM_PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        11,             // HOUR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        23,             // HOUR_OF_DAY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        59,             // MINUTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        59,             // SECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        999,            // MILLISECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        14*ONE_HOUR,    // ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        2*ONE_HOUR      // DST_OFFSET (double summer time)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    // Proclaim serialization compatibility with JDK 1.6
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   253
    @SuppressWarnings("FieldNameHidesFieldInSuperclass")
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54359
diff changeset
   254
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private static final long serialVersionUID = -3364572813905467929L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Era[] es = jcal.getEras();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        int length = es.length + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        eras = new Era[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        sinceFixedDates = new long[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // eras[BEFORE_MEIJI] and sinceFixedDate[BEFORE_MEIJI] are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        // same as Gregorian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        int index = BEFORE_MEIJI;
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   266
        int current = index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        sinceFixedDates[index] = gcal.getFixedDate(BEFORE_MEIJI_ERA.getSinceDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        eras[index++] = BEFORE_MEIJI_ERA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        for (Era e : es) {
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   270
            if(e.getSince(TimeZone.NO_TIMEZONE) < System.currentTimeMillis()) {
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   271
                current = index;
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   272
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            CalendarDate d = e.getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            sinceFixedDates[index] = gcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            eras[index++] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
   277
        currentEra = current;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        LEAST_MAX_VALUES[ERA] = MAX_VALUES[ERA] = eras.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // Calculate the least maximum year and least day of Year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // values. The following code assumes that there's at most one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // era transition in a Gregorian year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int year = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int dayOfYear = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        CalendarDate date = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (int i = 1; i < eras.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            long fd = sinceFixedDates[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            CalendarDate transitionDate = eras[i].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            date.setDate(transitionDate.getYear(), BaseCalendar.JANUARY, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            long fdd = gcal.getFixedDate(date);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   292
            if (fd != fdd) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   293
                dayOfYear = Math.min((int)(fd - fdd) + 1, dayOfYear);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   294
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            date.setDate(transitionDate.getYear(), BaseCalendar.DECEMBER, 31);
17474
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   296
            fdd = gcal.getFixedDate(date);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   297
            if (fd != fdd) {
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   298
                dayOfYear = Math.min((int)(fdd - fd) + 1, dayOfYear);
8c100beabcc0 8013730: JSR 310 DateTime API Updates III
sherman
parents: 15280
diff changeset
   299
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            LocalGregorianCalendar.Date lgd = getCalendarDate(fd - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int y = lgd.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // Unless the first year starts from January 1, the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // max value could be one year short. For example, if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            // Showa 63 January 8, 63 is the actual max value since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            // Showa 64 January 8 doesn't exist.
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   306
            if (!(lgd.getMonth() == BaseCalendar.JANUARY && lgd.getDayOfMonth() == 1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                y--;
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   308
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            year = Math.min(y, year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        LEAST_MAX_VALUES[YEAR] = year; // Max year could be smaller than this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        LEAST_MAX_VALUES[DAY_OF_YEAR] = dayOfYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * jdate always has a sun.util.calendar.LocalGregorianCalendar.Date instance to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * avoid overhead of creating it for each calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private transient LocalGregorianCalendar.Date jdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Temporary int[2] to get time zone offsets. zoneOffsets[0] gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the GMT offset value and zoneOffsets[1] gets the daylight saving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    private transient int[] zoneOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Temporary storage for saving original fields[] values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * non-lenient mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private transient int[] originalFields;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   335
     * Constructs a {@code JapaneseImperialCalendar} based on the current time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * in the given time zone with the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param zone the given time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param aLocale the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   341
    JapaneseImperialCalendar(TimeZone zone, Locale aLocale) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        super(zone, aLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        jdate = jcal.newCalendarDate(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        setTimeInMillis(System.currentTimeMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
15280
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   348
     * Constructs an "empty" {@code JapaneseImperialCalendar}.
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   349
     *
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   350
     * @param zone    the given time zone
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   351
     * @param aLocale the given locale
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   352
     * @param flag    the flag requesting an empty instance
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   353
     */
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   354
    JapaneseImperialCalendar(TimeZone zone, Locale aLocale, boolean flag) {
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   355
        super(zone, aLocale);
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   356
        jdate = jcal.newCalendarDate(zone);
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   357
    }
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   358
d9e473e85f3c 4745761: (cal) RFE: Support builder for constructing Calendar
okutsu
parents: 14765
diff changeset
   359
    /**
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   360
     * Returns {@code "japanese"} as the calendar type of this {@code
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   361
     * JapaneseImperialCalendar}.
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   362
     *
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   363
     * @return {@code "japanese"}
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   364
     */
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   365
    @Override
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   366
    public String getCalendarType() {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   367
        return "japanese";
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   368
    }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   369
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
   370
    /**
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   371
     * Compares this {@code JapaneseImperialCalendar} to the specified
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   372
     * {@code Object}. The result is {@code true} if and
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   373
     * only if the argument is a {@code JapaneseImperialCalendar} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * that represents the same time value (millisecond offset from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * the <a href="Calendar.html#Epoch">Epoch</a>) under the same
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   376
     * {@code Calendar} parameters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @param obj the object to compare with.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   379
     * @return {@code true} if this object is equal to {@code obj};
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   380
     * {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @see Calendar#compareTo(Calendar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   383
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return obj instanceof JapaneseImperialCalendar &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Generates the hash code for this
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   391
     * {@code JapaneseImperialCalendar} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   393
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return super.hashCode() ^ jdate.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Adds the specified (signed) amount of time to the given calendar field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * based on the calendar's rules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   402
     * <p><em>Add rule 1</em>. The value of {@code field}
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   403
     * after the call minus the value of {@code field} before the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   404
     * call is {@code amount}, modulo any overflow that has occurred in
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   405
     * {@code field}. Overflow occurs when a field value exceeds its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * range and, as a result, the next larger field is incremented or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * decremented and the field value is adjusted back into its range.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p><em>Add rule 2</em>. If a smaller field is expected to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * invariant, but it is impossible for it to be equal to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * prior value because of changes in its minimum or maximum after
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   412
     * {@code field} is changed, then its value is adjusted to be as close
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * as possible to its expected value. A smaller field represents a
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   414
     * smaller unit of time. {@code HOUR} is a smaller field than
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   415
     * {@code DAY_OF_MONTH}. No adjustment is made to smaller fields
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * that are not expected to be invariant. The calendar system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * determines what fields are expected to be invariant.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param field the calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param amount the amount of date or time to be added to the field.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   421
     * @throws    IllegalArgumentException if {@code field} is
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   422
     * {@code ZONE_OFFSET}, {@code DST_OFFSET}, or unknown,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * or if any calendar fields have out-of-range values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * non-lenient mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   426
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void add(int field, int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // If amount == 0, do nothing even the given field is out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // range. This is tested by JCK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (amount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return;   // Do nothing!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (field < 0 || field >= ZONE_OFFSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        // Sync the time and calendar fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        complete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        if (field == YEAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            d.addYear(amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            pinDayOfMonth(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            set(ERA, getEraIndex(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            set(YEAR, d.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            set(MONTH, d.getMonth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        } else if (field == MONTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            d.addMonth(amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            pinDayOfMonth(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            set(ERA, getEraIndex(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            set(YEAR, d.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            set(MONTH, d.getMonth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        } else if (field == ERA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            int era = internalGet(ERA) + amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (era < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                era = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            } else if (era > eras.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                era = eras.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            set(ERA, era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            long delta = amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            long timeOfDay = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // Handle the time fields here. Convert the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // amount to milliseconds and call setTimeInMillis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            case HOUR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            case HOUR_OF_DAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                delta *= 60 * 60 * 1000;        // hours to milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            case MINUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                delta *= 60 * 1000;             // minutes to milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            case SECOND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                delta *= 1000;                  // seconds to milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            case MILLISECOND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            // Handle week, day and AM_PM fields which involves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            // time zone offset change adjustment. Convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // given amount to the number of days.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            case WEEK_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            case WEEK_OF_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            case DAY_OF_WEEK_IN_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                delta *= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            case DAY_OF_MONTH: // synonym of DATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            case DAY_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            case DAY_OF_WEEK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            case AM_PM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                // Convert the amount to the number of days (delta)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                // and +12 or -12 hours (timeOfDay).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                delta = amount / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                timeOfDay = 12 * (amount % 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            // The time fields don't require time zone offset change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // adjustment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (field >= HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                setTimeInMillis(time + delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            // The rest of the fields (week, day or AM_PM fields)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            // require time zone offset (both GMT and DST) change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            // adjustment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            // Translate the current time to the fixed date and time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            // of the day.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            long fd = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            timeOfDay += internalGet(HOUR_OF_DAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            timeOfDay *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            timeOfDay += internalGet(MINUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            timeOfDay *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            timeOfDay += internalGet(SECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            timeOfDay *= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            timeOfDay += internalGet(MILLISECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (timeOfDay >= ONE_DAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                fd++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                timeOfDay -= ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            } else if (timeOfDay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                fd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                timeOfDay += ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            fd += delta; // fd is the expected fixed date after the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            int zoneOffset = internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            setTimeInMillis((fd - EPOCH_OFFSET) * ONE_DAY + timeOfDay - zoneOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            zoneOffset -= internalGet(ZONE_OFFSET) + internalGet(DST_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            // If the time zone offset has changed, then adjust the difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (zoneOffset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                setTimeInMillis(time + zoneOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                long fd2 = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                // If the adjustment has changed the date, then take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                // the previous one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                if (fd2 != fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    setTimeInMillis(time - zoneOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   555
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public void roll(int field, boolean up) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        roll(field, up ? +1 : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Adds a signed amount to the specified calendar field without changing larger fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * A negative roll amount means to subtract from field without changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * larger fields. If the specified amount is 0, this method performs nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p>This method calls {@link #complete()} before adding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * amount so that all the calendar fields are normalized. If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * is any calendar field having an out-of-range value in non-lenient mode, then an
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   568
     * {@code IllegalArgumentException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param field the calendar field.
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   571
     * @param amount the signed amount to add to {@code field}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   572
     * @throws    IllegalArgumentException if {@code field} is
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
   573
     * {@code ZONE_OFFSET}, {@code DST_OFFSET}, or unknown,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * or if any calendar fields have out-of-range values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * non-lenient mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @see #roll(int,boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @see #add(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see #set(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 17474
diff changeset
   580
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    public void roll(int field, int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        // If amount == 0, do nothing even the given field is out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // range. This is tested by JCK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (amount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (field < 0 || field >= ZONE_OFFSET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // Sync the time and calendar fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        complete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        int min = getMinimum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int max = getMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        case ERA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        case AM_PM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        case MINUTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        case SECOND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        case MILLISECOND:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // These fields are handled simply, since they have fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // minima and maxima. Other fields are complicated, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            // the range within they must roll varies depending on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // date, a time zone and the era transitions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        case HOUR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        case HOUR_OF_DAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                int unit = max + 1; // 12 or 24 hours
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                int h = internalGet(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                int nh = (h + amount) % unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                if (nh < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    nh += unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                time += ONE_HOUR * (nh - h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                // The day might have changed, which could happen if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                // the daylight saving time transition brings it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                // the next day, although it's very unlikely. But we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                // have to make sure not to change the larger fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                CalendarDate d = jcal.getCalendarDate(time, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                if (internalGet(DAY_OF_MONTH) != d.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    d.setEra(jdate.getEra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    d.setDate(internalGet(YEAR),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                              internalGet(MONTH) + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                              internalGet(DAY_OF_MONTH));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    if (field == HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        assert (internalGet(AM_PM) == PM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        d.addHours(+12); // restore PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    time = jcal.getTime(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                int hourOfDay = d.getHours();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                internalSet(field, hourOfDay % unit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                if (field == HOUR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    internalSet(HOUR_OF_DAY, hourOfDay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    internalSet(AM_PM, hourOfDay / 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    internalSet(HOUR, hourOfDay % 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                // Time zone offset and/or daylight saving might have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                int zoneOffset = d.getZoneOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                int saving = d.getDaylightSaving();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                internalSet(ZONE_OFFSET, zoneOffset - saving);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                internalSet(DST_OFFSET, saving);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        case YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            min = getActualMinimum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            max = getActualMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        case MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            // Rolling the month involves both pinning the final value to [0, 11]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            // and adjusting the DAY_OF_MONTH if necessary.  We only adjust the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            // DAY_OF_MONTH if, after updating the MONTH field, it is illegal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            // E.g., <jan31>.roll(MONTH, 1) -> <feb28> or <feb29>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                if (!isTransitionYear(jdate.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    int year = jdate.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    if (year == getMaximum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        CalendarDate jd = jcal.getCalendarDate(time, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        CalendarDate d = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        max = d.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        int n = getRolledValue(internalGet(field), amount, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        if (n == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                            // To avoid overflow, use an equivalent year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                            jd.addYear(-400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                            jd.setMonth(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                            if (jd.getDayOfMonth() > d.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                                jd.setDayOfMonth(d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                            if (jd.getDayOfMonth() == d.getDayOfMonth()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                && jd.getTimeOfDay() > d.getTimeOfDay()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                jd.setMonth(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                jd.setDayOfMonth(d.getDayOfMonth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                                // Month may have changed by the normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                                n = jd.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                            set(DAY_OF_MONTH, jd.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        set(MONTH, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    } else if (year == getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        CalendarDate jd = jcal.getCalendarDate(time, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        min = d.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                        int n = getRolledValue(internalGet(field), amount, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        if (n == min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            // To avoid underflow, use an equivalent year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                            jd.addYear(+400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                            jd.setMonth(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            if (jd.getDayOfMonth() < d.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                jd.setDayOfMonth(d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                            if (jd.getDayOfMonth() == d.getDayOfMonth()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                && jd.getTimeOfDay() < d.getTimeOfDay()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                jd.setMonth(n + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                jd.setDayOfMonth(d.getDayOfMonth() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                // Month may have changed by the normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                n = jd.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            set(DAY_OF_MONTH, jd.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        set(MONTH, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        int mon = (internalGet(MONTH) + amount) % 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        if (mon < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                            mon += 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                        set(MONTH, mon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        // Keep the day of month in the range.  We
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        // don't want to spill over into the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        // month; e.g., we don't want jan31 + 1 mo ->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        // feb31 -> mar3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        int monthLen = monthLength(mon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        if (internalGet(DAY_OF_MONTH) > monthLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                            set(DAY_OF_MONTH, monthLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    int eraIndex = getEraIndex(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    CalendarDate transition = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    if (jdate.getYear() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                        transition = eras[eraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                        min = transition.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        if (eraIndex < eras.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            transition = eras[eraIndex + 1].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            if (transition.getYear() == jdate.getNormalizedYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                max = transition.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                if (transition.getDayOfMonth() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                                    max--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    if (min == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        // The year has only one month. No need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                        // process further. (Showa Gan-nen (year 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        // and the last year have only one month.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    int n = getRolledValue(internalGet(field), amount, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    set(MONTH, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    if (n == min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                        if (!(transition.getMonth() == BaseCalendar.JANUARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                              && transition.getDayOfMonth() == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                            if (jdate.getDayOfMonth() < transition.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                set(DAY_OF_MONTH, transition.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    } else if (n == max && (transition.getMonth() - 1 == n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        int dom = transition.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        if (jdate.getDayOfMonth() >= dom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                            set(DAY_OF_MONTH, dom - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        case WEEK_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                int y = jdate.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                max = getActualMaximum(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK)); // update stamp[field]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                int woy = internalGet(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                int value = woy + amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                if (!isTransitionYear(jdate.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    int year = jdate.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    if (year == getMaximum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        max = getActualMaximum(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    } else if (year == getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                        min = getActualMinimum(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        max = getActualMaximum(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                        if (value > min && value < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                            set(WEEK_OF_YEAR, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    // If the new value is in between min and max
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    // (exclusive), then we can use the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    if (value > min && value < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        set(WEEK_OF_YEAR, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    long fd = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    // Make sure that the min week has the current DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    long day1 = fd - (7 * (woy - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    if (year != getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        if (gcal.getYearFromFixedDate(day1) != y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                            min++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                        CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        if (day1 < jcal.getFixedDate(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                            min++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    // Make sure the same thing for the max week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    fd += 7 * (max - internalGet(WEEK_OF_YEAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    if (gcal.getYearFromFixedDate(fd) != y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        max--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                // Handle transition here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                long fd = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                long day1 = fd - (7 * (woy - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                // Make sure that the min week has the current DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                LocalGregorianCalendar.Date d = getCalendarDate(day1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    min++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                // Make sure the same thing for the max week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                fd += 7 * (max - woy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                jcal.getCalendarDateFromFixedDate(d, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                if (!(d.getEra() == jdate.getEra() && d.getYear() == jdate.getYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    max--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                // value: the new WEEK_OF_YEAR which must be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                // to month and day of month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                value = getRolledValue(woy, amount, min, max) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                d = getCalendarDate(day1 + value * 7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                set(MONTH, d.getMonth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        case WEEK_OF_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                boolean isTransitionYear = isTransitionYear(jdate.getNormalizedYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                // dow: relative day of week from the first day of week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                int dow = internalGet(DAY_OF_WEEK) - getFirstDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                if (dow < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    dow += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                long fd = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                long month1;     // fixed date of the first day (usually 1) of the month
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                int monthLength; // actual month length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                if (isTransitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    month1 = getFixedDateMonth1(jdate, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    monthLength = actualMonthLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    month1 = fd - internalGet(DAY_OF_MONTH) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    monthLength = jcal.getMonthLength(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                // the first day of week of the month.
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   867
                long monthDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(month1 + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   868
                                                                                     getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                // if the week has enough days to form a week, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                // week starts from the previous month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                if ((int)(monthDay1st - month1) >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    monthDay1st -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                max = getActualMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                // value: the new WEEK_OF_MONTH value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                int value = getRolledValue(internalGet(field), amount, 1, max) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                // nfd: fixed date of the rolled date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                long nfd = monthDay1st + value * 7 + dow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                // Unlike WEEK_OF_YEAR, we need to change day of week if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                // nfd is out of the month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                if (nfd < month1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    nfd = month1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                } else if (nfd >= (month1 + monthLength)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    nfd = month1 + monthLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                set(DAY_OF_MONTH, (int)(nfd - month1) + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        case DAY_OF_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                if (!isTransitionYear(jdate.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    max = jcal.getMonthLength(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                // TODO: Need to change the spec to be usable DAY_OF_MONTH rolling...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                // Transition handling. We can't change year and era
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                // values here due to the Calendar roll spec!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                long month1 = getFixedDateMonth1(jdate, cachedFixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                // It may not be a regular month. Convert the date and range to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                // the relative values, perform the roll, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                // convert the result back to the rolled date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                int value = getRolledValue((int)(cachedFixedDate - month1), amount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                           0, actualMonthLength() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                LocalGregorianCalendar.Date d = getCalendarDate(month1 + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                assert getEraIndex(d) == internalGetEra()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    && d.getYear() == internalGet(YEAR) && d.getMonth()-1 == internalGet(MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        case DAY_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                max = getActualMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                if (!isTransitionYear(jdate.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                // Handle transition. We can't change year and era values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                // here due to the Calendar roll spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                int value = getRolledValue(internalGet(DAY_OF_YEAR), amount, min, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                long jan0 = cachedFixedDate - internalGet(DAY_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                LocalGregorianCalendar.Date d = getCalendarDate(jan0 + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                assert getEraIndex(d) == internalGetEra() && d.getYear() == internalGet(YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                set(MONTH, d.getMonth() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        case DAY_OF_WEEK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                int normalizedYear = jdate.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                if (!isTransitionYear(normalizedYear) && !isTransitionYear(normalizedYear - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    // If the week of year is in the same year, we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    // just change DAY_OF_WEEK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    int weekOfYear = internalGet(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    if (weekOfYear > 1 && weekOfYear < 52) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        set(WEEK_OF_YEAR, internalGet(WEEK_OF_YEAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                        max = SATURDAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                // We need to handle it in a different way around year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                // boundaries and in the transition year. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                // changing era and year values violates the roll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                // rule: not changing larger calendar fields...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                amount %= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                if (amount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                long fd = cachedFixedDate;
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
   959
                long dowFirst = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fd, getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                fd += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                if (fd < dowFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                    fd += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                } else if (fd >= dowFirst + 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                    fd -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                LocalGregorianCalendar.Date d = getCalendarDate(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                set(ERA, getEraIndex(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                set(d.getYear(), d.getMonth() - 1, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        case DAY_OF_WEEK_IN_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                min = 1; // after having normalized, min should be 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                if (!isTransitionYear(jdate.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    int dom = internalGet(DAY_OF_MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    int monthLength = jcal.getMonthLength(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    int lastDays = monthLength % 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    max = monthLength / 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    int x = (dom - 1) % 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    if (x < lastDays) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        max++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    set(DAY_OF_WEEK, internalGet(DAY_OF_WEEK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                // Transition year handling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                long fd = cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                long month1 = getFixedDateMonth1(jdate, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                int monthLength = actualMonthLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                int lastDays = monthLength % 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                max = monthLength / 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                int x = (int)(fd - month1) % 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                if (x < lastDays) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    max++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                int value = getRolledValue(internalGet(field), amount, min, max) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                fd = month1 + value * 7 + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                LocalGregorianCalendar.Date d = getCalendarDate(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                set(DAY_OF_MONTH, d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        set(field, getRolledValue(internalGet(field), amount, min, max));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1009
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public String getDisplayName(int field, int style, Locale locale) {
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1011
        if (!checkDisplayNameParams(field, style, SHORT, NARROW_FORMAT, locale,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                                    ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1016
        int fieldValue = get(field);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1017
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // "GanNen" is supported only in the LONG style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (field == YEAR
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1020
            && (getBaseStyle(style) != LONG || fieldValue != 1 || get(ERA) == 0)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1024
        String name = CalendarDataUtility.retrieveFieldValueName(getCalendarType(), field,
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1025
                                                                 fieldValue, style, locale);
53963
7f01a85f2710 8219890: Calendar.getDisplayName() returns empty string for new Japanese Era on some locales
naoto
parents: 50478
diff changeset
  1026
        // If the ERA value is null or empty, then
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1027
        // try to get its name or abbreviation from the Era instance.
53963
7f01a85f2710 8219890: Calendar.getDisplayName() returns empty string for new Japanese Era on some locales
naoto
parents: 50478
diff changeset
  1028
        if ((name == null || name.isEmpty()) &&
7f01a85f2710 8219890: Calendar.getDisplayName() returns empty string for new Japanese Era on some locales
naoto
parents: 50478
diff changeset
  1029
                field == ERA &&
7f01a85f2710 8219890: Calendar.getDisplayName() returns empty string for new Japanese Era on some locales
naoto
parents: 50478
diff changeset
  1030
                fieldValue < eras.length) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1031
            Era era = eras[fieldValue];
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1032
            name = (style == SHORT) ? era.getAbbreviation() : era.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1037
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    public Map<String,Integer> getDisplayNames(int field, int style, Locale locale) {
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1039
        if (!checkDisplayNameParams(field, style, ALL_STYLES, NARROW_FORMAT, locale,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                    ERA_MASK|YEAR_MASK|MONTH_MASK|DAY_OF_WEEK_MASK|AM_PM_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1043
        Map<String, Integer> names;
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1044
        names = CalendarDataUtility.retrieveFieldValueNames(getCalendarType(), field, style, locale);
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1045
        // If strings[] has fewer than eras[], get more names from eras[].
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1046
        if (names != null) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1047
            if (field == ERA) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1048
                int size = names.size();
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1049
                if (style == ALL_STYLES) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1050
                    Set<Integer> values = new HashSet<>();
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1051
                    // count unique era values
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1052
                    for (String key : names.keySet()) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1053
                        values.add(names.get(key));
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1054
                    }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1055
                    size = values.size();
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1056
                }
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1057
                if (size < eras.length) {
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1058
                    int baseStyle = getBaseStyle(style);
34878
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1059
                    for (int i = 0; i < eras.length; i++) {
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1060
                        if (!names.values().contains(i)) {
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1061
                            Era era = eras[i];
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1062
                            if (baseStyle == ALL_STYLES || baseStyle == SHORT
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1063
                                    || baseStyle == NARROW_FORMAT) {
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1064
                                names.put(era.getAbbreviation(), i);
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1065
                            }
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1066
                            if (baseStyle == ALL_STYLES || baseStyle == LONG) {
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1067
                                names.put(era.getName(), i);
7d7e55ff253a 8039301: [ja] Host Locale Provider uses non-translated Calendar field names
naoto
parents: 32649
diff changeset
  1068
                            }
14765
0987999ed367 8000983: Support narrow display names for calendar fields
okutsu
parents: 14014
diff changeset
  1069
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 11130
diff changeset
  1074
        return names;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * Returns the minimum value for the given calendar field of this
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1079
     * {@code Calendar} instance. The minimum value is
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1080
     * defined as the smallest value returned by the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1081
     * {@link Calendar#get(int) get} method for any possible time value,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * taking into consideration the current values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * and {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @param field the calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @return the minimum value for the given calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @see #getMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see #getGreatestMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @see #getLeastMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @see #getActualMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @see #getActualMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    public int getMinimum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        return MIN_VALUES[field];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Returns the maximum value for the given calendar field of this
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1101
     * {@code GregorianCalendar} instance. The maximum value is
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1102
     * defined as the largest value returned by the
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1103
     * {@link Calendar#get(int) get} method for any possible time value,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * taking into consideration the current values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * and {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @param field the calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @return the maximum value for the given calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @see #getMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @see #getGreatestMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @see #getLeastMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @see #getActualMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * @see #getActualMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    public int getMaximum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        case YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                // The value should depend on the time zone of this calendar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                                                     getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                return Math.max(LEAST_MAX_VALUES[YEAR], d.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        return MAX_VALUES[field];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * Returns the highest minimum value for the given calendar field
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1132
     * of this {@code GregorianCalendar} instance. The highest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * minimum value is defined as the largest value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * {@link #getActualMinimum(int)} for any possible time value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * taking into consideration the current values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * and {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * @param field the calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @return the highest minimum value for the given calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @see #getMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * @see #getMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * @see #getLeastMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * @see #getActualMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * @see #getActualMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    public int getGreatestMinimum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        return field == YEAR ? 1 : MIN_VALUES[field];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * Returns the lowest maximum value for the given calendar field
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1154
     * of this {@code GregorianCalendar} instance. The lowest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * maximum value is defined as the smallest value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * {@link #getActualMaximum(int)} for any possible time value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * taking into consideration the current values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * and {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @param field the calendar field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @return the lowest maximum value for the given calendar field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @see #getMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @see #getMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @see #getGreatestMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @see #getActualMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @see #getActualMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    public int getLeastMaximum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        case YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                return Math.min(LEAST_MAX_VALUES[YEAR], getMaximum(YEAR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return LEAST_MAX_VALUES[field];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * Returns the minimum value that this calendar field could have,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * taking into consideration the given time value and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * and {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @param field the calendar field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @return the minimum of the given field for the time value of
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1190
     * this {@code JapaneseImperialCalendar}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @see #getMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @see #getMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @see #getGreatestMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @see #getLeastMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * @see #getActualMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public int getActualMinimum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        if (!isFieldSet(YEAR_MASK|MONTH_MASK|WEEK_OF_YEAR_MASK, field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return getMinimum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        JapaneseImperialCalendar jc = getNormalizedCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        // Get a local date which includes time of day and time zone,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        // which are missing in jc.jdate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        LocalGregorianCalendar.Date jd = jcal.getCalendarDate(jc.getTimeInMillis(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                                                              getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        int eraIndex = getEraIndex(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        case YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                if (eraIndex > BEFORE_MEIJI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    value = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    long since = eras[eraIndex].getSince(getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    CalendarDate d = jcal.getCalendarDate(since, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    // Use the same year in jd to take care of leap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    // years. i.e., both jd and d must agree on leap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    // or common years.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    jd.setYear(d.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                    jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    assert jd.isLeapYear() == d.isLeapYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                    if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                        value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    value = getMinimum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    // Use an equvalent year of d.getYear() if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    // possible. Otherwise, ignore the leap year and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    // common year difference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    int y = d.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    if (y > 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                        y -= 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    jd.setYear(y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                    if (getYearOffsetInMillis(jd) < getYearOffsetInMillis(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                        value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        case MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                // In Before Meiji and Meiji, January is the first month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                if (eraIndex > MEIJI && jd.getYear() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    long since = eras[eraIndex].getSince(getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                    CalendarDate d = jcal.getCalendarDate(since, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                    value = d.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    if (jd.getDayOfMonth() < d.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                        value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        case WEEK_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                value = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                // shift 400 years to avoid underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                d.addYear(+400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                jd.setEra(d.getEra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                jd.setYear(d.getYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                long jan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                long fd = jcal.getFixedDate(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                int woy = getWeekNumber(jan1, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                long day1 = fd - (7 * (woy - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                if ((day1 < jan1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    (day1 == jan1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                     jd.getTimeOfDay() < d.getTimeOfDay())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Returns the maximum value that this calendar field could have,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * taking into consideration the given time value and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * {@link Calendar#getFirstDayOfWeek() getFirstDayOfWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * {@link Calendar#getMinimalDaysInFirstWeek() getMinimalDaysInFirstWeek},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * {@link Calendar#getTimeZone() getTimeZone} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * For example, if the date of this instance is Heisei 16February 1,
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1293
     * the actual maximum value of the {@code DAY_OF_MONTH} field
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * is 29 because Heisei 16 is a leap year, and if the date of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * instance is Heisei 17 February 1, it's 28.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @param field the calendar field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * @return the maximum of the given field for the time value of
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1299
     * this {@code JapaneseImperialCalendar}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @see #getMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @see #getMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @see #getGreatestMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * @see #getLeastMaximum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @see #getActualMinimum(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    public int getActualMaximum(int field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        final int fieldsForFixedMax = ERA_MASK|DAY_OF_WEEK_MASK|HOUR_MASK|AM_PM_MASK|
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            HOUR_OF_DAY_MASK|MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK|
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            ZONE_OFFSET_MASK|DST_OFFSET_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if ((fieldsForFixedMax & (1<<field)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            return getMaximum(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        JapaneseImperialCalendar jc = getNormalizedCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        LocalGregorianCalendar.Date date = jc.jdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        int normalizedYear = date.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        int value = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        switch (field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        case MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                value = DECEMBER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                if (isTransitionYear(date.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    // TODO: there may be multiple transitions in a year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    int eraIndex = getEraIndex(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    if (date.getYear() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        eraIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                        assert eraIndex < eras.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    long transition = sinceFixedDates[eraIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                    long fd = jc.cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    if (fd < transition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                        LocalGregorianCalendar.Date ldate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                            = (LocalGregorianCalendar.Date) date.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                        jcal.getCalendarDateFromFixedDate(ldate, transition - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                        value = ldate.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                                                                         getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                        value = d.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        case DAY_OF_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            value = jcal.getMonthLength(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        case DAY_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                if (isTransitionYear(date.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                    // Handle transition year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    // TODO: there may be multiple transitions in a year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                    int eraIndex = getEraIndex(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                    if (date.getYear() != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                        eraIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                        assert eraIndex < eras.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    long transition = sinceFixedDates[eraIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    long fd = jc.cachedFixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    if (fd < transition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        value = (int)(transition - gcal.getFixedDate(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        d.addYear(+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                        value = (int)(gcal.getFixedDate(d) - transition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                                                                         getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                    if (date.getEra() == d.getEra() && date.getYear() == d.getYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                        long fd = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                        long jan1 = getFixedDateJan1(d, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                        value = (int)(fd - jan1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    } else if (date.getYear() == getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                        CalendarDate d1 = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                        long fd1 = jcal.getFixedDate(d1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                        d1.addYear(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                        d1.setMonth(BaseCalendar.JANUARY).setDayOfMonth(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                        jcal.normalize(d1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                        long fd2 = jcal.getFixedDate(d1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                        value = (int)(fd2 - fd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                        value = jcal.getYearLength(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        case WEEK_OF_YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                if (!isTransitionYear(date.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                                                                          getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    if (date.getEra() == jd.getEra() && date.getYear() == jd.getYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                        long fd = jcal.getFixedDate(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                        long jan1 = getFixedDateJan1(jd, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                        value = getWeekNumber(jan1, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    } else if (date.getEra() == null && date.getYear() == getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                        CalendarDate d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                        // shift 400 years to avoid underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                        d.addYear(+400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                        jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                        jd.setEra(d.getEra());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                        jd.setDate(d.getYear() + 1, BaseCalendar.JANUARY, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                        jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                        long jan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                        long nextJan1 = jcal.getFixedDate(jd);
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1413
                        long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1414
                                                                                            getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                        int ndays = (int)(nextJan1st - nextJan1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                        if (ndays >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                            nextJan1st -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                        value = getWeekNumber(jan1, nextJan1st);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                        // Get the day of week of January 1 of the year
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                        CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                        d.setDate(date.getNormalizedYear(), BaseCalendar.JANUARY, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                        int dayOfWeek = gcal.getDayOfWeek(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                        // Normalize the day of week with the firstDayOfWeek value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                        dayOfWeek -= getFirstDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                        if (dayOfWeek < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                            dayOfWeek += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                        value = 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                        int magic = dayOfWeek + getMinimalDaysInFirstWeek() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                        if ((magic == 6) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                            (date.isLeapYear() && (magic == 5 || magic == 12))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                            value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                if (jc == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                    jc = (JapaneseImperialCalendar) jc.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                int max = getActualMaximum(DAY_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                jc.set(DAY_OF_YEAR, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                value = jc.get(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                if (value == 1 && max > 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    jc.add(WEEK_OF_YEAR, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                    value = jc.get(WEEK_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        case WEEK_OF_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
                LocalGregorianCalendar.Date jd = jcal.getCalendarDate(Long.MAX_VALUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                                                                      getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                if (!(date.getEra() == jd.getEra() && date.getYear() == jd.getYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                    CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                    d.setDate(date.getNormalizedYear(), date.getMonth(), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    int dayOfWeek = gcal.getDayOfWeek(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    int monthLength = gcal.getMonthLength(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    dayOfWeek -= getFirstDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    if (dayOfWeek < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                        dayOfWeek += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                    int nDaysFirstWeek = 7 - dayOfWeek; // # of days in the first week
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    value = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                    if (nDaysFirstWeek >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                        value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                    monthLength -= nDaysFirstWeek + 7 * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                    if (monthLength > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
                        value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                        if (monthLength > 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                            value++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                    long fd = jcal.getFixedDate(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    long month1 = fd - jd.getDayOfMonth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                    value = getWeekNumber(month1, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        case DAY_OF_WEEK_IN_MONTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                int ndays, dow1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                int dow = date.getDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                BaseCalendar.Date d = (BaseCalendar.Date) date.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                ndays = jcal.getMonthLength(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                d.setDayOfMonth(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                dow1 = d.getDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                int x = dow - dow1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                    x += 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                ndays -= x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                value = (ndays + 6) / 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        case YEAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                CalendarDate jd = jcal.getCalendarDate(jc.getTimeInMillis(), getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                CalendarDate d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                int eraIndex = getEraIndex(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                if (eraIndex == eras.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    d = jcal.getCalendarDate(Long.MAX_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                    value = d.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    // Use an equivalent year for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    // getYearOffsetInMillis call to avoid overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    if (value > 400) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                        jd.setYear(value - 400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    d = jcal.getCalendarDate(eras[eraIndex + 1].getSince(getZone()) - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                                             getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                    value = d.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                    // Use the same year as d.getYear() to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    // consistent with leap and common years.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    jd.setYear(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                jcal.normalize(jd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                if (getYearOffsetInMillis(jd) > getYearOffsetInMillis(d)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                    value--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            throw new ArrayIndexOutOfBoundsException(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * Returns the millisecond offset from the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * year. In the year for Long.MIN_VALUE, it's a pseudo value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * beyond the limit. The given CalendarDate object must have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * normalized before calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1544
    private long getYearOffsetInMillis(CalendarDate date) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        long t = (jcal.getDayOfYear(date) - 1) * ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        return t + date.getTimeOfDay() - date.getZoneOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        JapaneseImperialCalendar other = (JapaneseImperialCalendar) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        other.jdate = (LocalGregorianCalendar.Date) jdate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        other.originalFields = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        other.zoneOffsets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    public TimeZone getTimeZone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        TimeZone zone = super.getTimeZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        // To share the zone by the CalendarDate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        jdate.setZone(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        return zone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
    public void setTimeZone(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        super.setTimeZone(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        // To share the zone by the CalendarDate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        jdate.setZone(zone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * The fixed date corresponding to jdate. If the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * Long.MIN_VALUE, the fixed date value is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30042
diff changeset
  1575
    private transient long cachedFixedDate = Long.MIN_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * Converts the time value (millisecond offset from the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * href="Calendar.html#Epoch">Epoch</a>) to calendar field values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * The time is <em>not</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * recomputed first; to recompute the time, then the fields, call the
30042
4728ebcf8fd0 8076224: some tidy warnings from core libs
avstepan
parents: 27286
diff changeset
  1582
     * {@code complete} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * @see Calendar#complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    protected void computeFields() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        int mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (isPartiallyNormalized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            // Determine which calendar fields need to be computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            mask = getSetStateFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            int fieldMask = ~mask & ALL_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            if (fieldMask != 0 || cachedFixedDate == Long.MIN_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
                mask |= computeFields(fieldMask,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                                      mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                assert mask == ALL_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            // Specify all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            mask = ALL_FIELDS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            computeFields(mask, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        // After computing all the fields, set the field state to `COMPUTED'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        setFieldsComputed(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * This computeFields implements the conversion from UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * (millisecond offset from the Epoch) to calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * field values. fieldMask specifies which fields to change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * setting state to COMPUTED, although all fields are set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * the correct values. This is required to fix 4685354.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * @param fieldMask a bit mask to specify which fields to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * the setting state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * @param tzMask a bit mask to specify which time zone offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * fields to be used for time calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @return a new field mask that indicates what field values have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * actually been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    private int computeFields(int fieldMask, int tzMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        int zoneOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        TimeZone tz = getZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        if (zoneOffsets == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
            zoneOffsets = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            if (tz instanceof ZoneInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                zoneOffset = ((ZoneInfo)tz).getOffsets(time, zoneOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                zoneOffset = tz.getOffset(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                zoneOffsets[0] = tz.getRawOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                zoneOffsets[1] = zoneOffset - zoneOffsets[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        if (tzMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            if (isFieldSet(tzMask, ZONE_OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                zoneOffsets[0] = internalGet(ZONE_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            if (isFieldSet(tzMask, DST_OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                zoneOffsets[1] = internalGet(DST_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            zoneOffset = zoneOffsets[0] + zoneOffsets[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        // By computing time and zoneOffset separately, we can take
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        // the wider range of time+zoneOffset than the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        // implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        long fixedDate = zoneOffset / ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        int timeOfDay = zoneOffset % (int)ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        fixedDate += time / ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        timeOfDay += (int) (time % ONE_DAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        if (timeOfDay >= ONE_DAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            timeOfDay -= ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            ++fixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            while (timeOfDay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                timeOfDay += ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                --fixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        fixedDate += EPOCH_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        // See if we can use jdate to avoid date calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        if (fixedDate != cachedFixedDate || fixedDate < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            jcal.getCalendarDateFromFixedDate(jdate, fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            cachedFixedDate = fixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        int era = getEraIndex(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        int year = jdate.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        // Always set the ERA and YEAR values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        internalSet(ERA, era);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        internalSet(YEAR, year);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        int mask = fieldMask | (ERA_MASK|YEAR_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        int month =  jdate.getMonth() - 1; // 0-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        int dayOfMonth = jdate.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        // Set the basic date fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        if ((fieldMask & (MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
            internalSet(MONTH, month);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            internalSet(DAY_OF_MONTH, dayOfMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            internalSet(DAY_OF_WEEK, jdate.getDayOfWeek());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            mask |= MONTH_MASK|DAY_OF_MONTH_MASK|DAY_OF_WEEK_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        if ((fieldMask & (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                          |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            if (timeOfDay != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                int hours = timeOfDay / ONE_HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                internalSet(HOUR_OF_DAY, hours);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                internalSet(AM_PM, hours / 12); // Assume AM == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                internalSet(HOUR, hours % 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                int r = timeOfDay % ONE_HOUR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                internalSet(MINUTE, r / ONE_MINUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                r %= ONE_MINUTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                internalSet(SECOND, r / ONE_SECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                internalSet(MILLISECOND, r % ONE_SECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                internalSet(HOUR_OF_DAY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                internalSet(AM_PM, AM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                internalSet(HOUR, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                internalSet(MINUTE, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                internalSet(SECOND, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                internalSet(MILLISECOND, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
            mask |= (HOUR_OF_DAY_MASK|AM_PM_MASK|HOUR_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                     |MINUTE_MASK|SECOND_MASK|MILLISECOND_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        if ((fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            internalSet(ZONE_OFFSET, zoneOffsets[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            internalSet(DST_OFFSET, zoneOffsets[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            mask |= (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        if ((fieldMask & (DAY_OF_YEAR_MASK|WEEK_OF_YEAR_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                          |WEEK_OF_MONTH_MASK|DAY_OF_WEEK_IN_MONTH_MASK)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            int normalizedYear = jdate.getNormalizedYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            // If it's a year of an era transition, we need to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            // irregular year boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            boolean transitionYear = isTransitionYear(jdate.getNormalizedYear());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            int dayOfYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            long fixedDateJan1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            if (transitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                fixedDateJan1 = getFixedDateJan1(jdate, fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            } else if (normalizedYear == MIN_VALUES[YEAR]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                fixedDateJan1 = jcal.getFixedDate(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                dayOfYear = (int)(fixedDate - fixedDateJan1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                dayOfYear = (int) jcal.getDayOfYear(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                fixedDateJan1 = fixedDate - dayOfYear + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            long fixedDateMonth1 = transitionYear ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                getFixedDateMonth1(jdate, fixedDate) : fixedDate - dayOfMonth + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            internalSet(DAY_OF_YEAR, dayOfYear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            internalSet(DAY_OF_WEEK_IN_MONTH, (dayOfMonth - 1) / 7 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            int weekOfYear = getWeekNumber(fixedDateJan1, fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            // The spec is to calculate WEEK_OF_YEAR in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            // ISO8601-style. This creates problems, though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            if (weekOfYear == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                // If the date belongs to the last week of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                // previous year, use the week number of "12/31" of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                // the "previous" year. Again, if the previous year is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                // a transition year, we need to take care of it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                // Usually the previous day of the first day of a year
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                // is December 31, which is not always true in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                // Japanese imperial calendar system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                long fixedDec31 = fixedDateJan1 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                long prevJan1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                LocalGregorianCalendar.Date d = getCalendarDate(fixedDec31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                if (!(transitionYear || isTransitionYear(d.getNormalizedYear()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    prevJan1 = fixedDateJan1 - 365;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    if (d.isLeapYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                        --prevJan1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                } else if (transitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                    if (jdate.getYear() == 1) {
54359
3d8934bf505a 8205432: Replace the placeholder Japanese era name
naoto
parents: 53963
diff changeset
  1765
                        // As of Reiwa (since Meiji) there's no case
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                        // that there are multiple transitions in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                        // year.  Historically there was such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                        // case. There might be such case again in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                        // future.
54359
3d8934bf505a 8205432: Replace the placeholder Japanese era name
naoto
parents: 53963
diff changeset
  1770
                        if (era > REIWA) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                            CalendarDate pd = eras[era - 1].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                            if (normalizedYear == pd.getYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                                d.setMonth(pd.getMonth()).setDayOfMonth(pd.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                        } else {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1776
                            d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                        jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                        prevJan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                        prevJan1 = fixedDateJan1 - 365;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                        if (d.isLeapYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                            --prevJan1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                    CalendarDate cd = eras[getEraIndex(jdate)].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    d.setMonth(cd.getMonth()).setDayOfMonth(cd.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                    jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                    prevJan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                weekOfYear = getWeekNumber(prevJan1, fixedDec31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                if (!transitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                    // Regular years
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                    if (weekOfYear >= 52) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                        long nextJan1 = fixedDateJan1 + 365;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                        if (jdate.isLeapYear()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                            nextJan1++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                        }
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1801
                        long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1802
                                                                                            getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                        int ndays = (int)(nextJan1st - nextJan1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                        if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                            // The first days forms a week in which the date is included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                            weekOfYear = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                    LocalGregorianCalendar.Date d = (LocalGregorianCalendar.Date) jdate.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                    long nextJan1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                    if (jdate.getYear() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                        d.addYear(+1);
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1814
                        d.setMonth(LocalGregorianCalendar.JANUARY).setDayOfMonth(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                        nextJan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
                        int nextEraIndex = getEraIndex(d) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                        CalendarDate cd = eras[nextEraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                        d.setEra(eras[nextEraIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                        d.setDate(1, cd.getMonth(), cd.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                        jcal.normalize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                        nextJan1 = jcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                    }
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1824
                    long nextJan1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(nextJan1 + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1825
                                                                                        getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                    int ndays = (int)(nextJan1st - nextJan1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                    if (ndays >= getMinimalDaysInFirstWeek() && fixedDate >= (nextJan1st - 7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                        // The first days forms a week in which the date is included.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                        weekOfYear = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            internalSet(WEEK_OF_YEAR, weekOfYear);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            internalSet(WEEK_OF_MONTH, getWeekNumber(fixedDateMonth1, fixedDate));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            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
  1836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * Returns the number of weeks in a period between fixedDay1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * fixedDate. The getFirstDayOfWeek-getMinimalDaysInFirstWeek rule
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * is applied to calculate the number of weeks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * @param fixedDay1 the fixed date of the first day of the period
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * @param fixedDate the fixed date of the last day of the period
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * @return the number of weeks of the given period
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1849
    private int getWeekNumber(long fixedDay1, long fixedDate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        // We can always use `jcal' since Julian and Gregorian are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        // same thing for this calculation.
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1852
        long fixedDay1st = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDay1 + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  1853
                                                                             getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        int ndays = (int)(fixedDay1st - fixedDay1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        assert ndays <= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        if (ndays >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            fixedDay1st -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        int normalizedDayOfPeriod = (int)(fixedDate - fixedDay1st);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        if (normalizedDayOfPeriod >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            return normalizedDayOfPeriod / 7 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        return CalendarUtils.floorDivide(normalizedDayOfPeriod, 7) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * Converts calendar field values to the time value (millisecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * offset from the <a href="Calendar.html#Epoch">Epoch</a>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
  1870
     * @throws    IllegalArgumentException if any calendar fields are invalid.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    protected void computeTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        // In non-lenient mode, perform brief checking of calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        // fields which have been set externally. Through this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        // checking, the field values are stored in originalFields[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        // to see if any of them are normalized later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        if (!isLenient()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            if (originalFields == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                originalFields = new int[FIELD_COUNT];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            for (int field = 0; field < FIELD_COUNT; field++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                int value = internalGet(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                if (isExternallySet(field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                    // Quick validation for any out of range values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                    if (value < getMinimum(field) || value > getMaximum(field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                        throw new IllegalArgumentException(getFieldName(field));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                originalFields[field] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        // Let the super class determine which calendar fields to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        // used to calculate the time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        int fieldMask = selectFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        int year;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        int era;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        if (isSet(ERA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            era = internalGet(ERA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            year = isSet(YEAR) ? internalGet(YEAR) : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            if (isSet(YEAR)) {
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
  1905
                era = currentEra;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                year = internalGet(YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                // Equivalent to 1970 (Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                era = SHOWA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                year = 45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        // Calculate the time of day. We rely on the convention that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        // an UNSET field has 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        long timeOfDay = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        if (isFieldSet(fieldMask, HOUR_OF_DAY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            timeOfDay += (long) internalGet(HOUR_OF_DAY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            timeOfDay += internalGet(HOUR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            // The default value of AM_PM is 0 which designates AM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            if (isFieldSet(fieldMask, AM_PM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                timeOfDay += 12 * internalGet(AM_PM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        timeOfDay *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        timeOfDay += internalGet(MINUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        timeOfDay *= 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        timeOfDay += internalGet(SECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        timeOfDay *= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        timeOfDay += internalGet(MILLISECOND);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        // Convert the time of day to the number of days and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        // millisecond offset from midnight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        long fixedDate = timeOfDay / ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        timeOfDay %= ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        while (timeOfDay < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            timeOfDay += ONE_DAY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            --fixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        // Calculate the fixed date since January 1, 1 (Gregorian).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        fixedDate += getFixedDate(era, year, fieldMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        // millis represents local wall-clock time in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        long millis = (fixedDate - EPOCH_OFFSET) * ONE_DAY + timeOfDay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        // Compute the time zone offset and DST offset.  There are two potential
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        // ambiguities here.  We'll assume a 2:00 am (wall time) switchover time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        // for discussion purposes here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        // 1. The transition into DST.  Here, a designated time of 2:00 am - 2:59 am
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        //    can be in standard or in DST depending.  However, 2:00 am is an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        //    representation (the representation jumps from 1:59:59 am Std to 3:00:00 am DST).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        //    We assume standard time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        // 2. The transition out of DST.  Here, a designated time of 1:00 am - 1:59 am
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        //    can be in standard or DST.  Both are valid representations (the rep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        //    jumps from 1:59:59 DST to 1:00:00 Std).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        //    Again, we assume standard time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        // We use the TimeZone object, unless the user has explicitly set the ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        // or DST_OFFSET fields; then we use those fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        TimeZone zone = getZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        if (zoneOffsets == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            zoneOffsets = new int[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        int tzMask = fieldMask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        if (tzMask != (ZONE_OFFSET_MASK|DST_OFFSET_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            if (zone instanceof ZoneInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                ((ZoneInfo)zone).getOffsetsByWall(millis, zoneOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                zone.getOffsets(millis - zone.getRawOffset(), zoneOffsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        if (tzMask != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            if (isFieldSet(tzMask, ZONE_OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                zoneOffsets[0] = internalGet(ZONE_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            if (isFieldSet(tzMask, DST_OFFSET)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                zoneOffsets[1] = internalGet(DST_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        // Adjust the time zone offset values to get the UTC time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        millis -= zoneOffsets[0] + zoneOffsets[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        // Set this calendar's time in milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        time = millis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        int mask = computeFields(fieldMask | getSetStateFields(), tzMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        if (!isLenient()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            for (int field = 0; field < FIELD_COUNT; field++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                if (!isExternallySet(field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                if (originalFields[field] != internalGet(field)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    int wrongValue = internalGet(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    // Restore the original field values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                    System.arraycopy(originalFields, 0, fields, 0, fields.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                    throw new IllegalArgumentException(getFieldName(field) + "=" + wrongValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                                                       + ", expected " + originalFields[field]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        setFieldsNormalized(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * Computes the fixed date under either the Gregorian or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * Julian calendar, using the given year and the specified calendar fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13583
diff changeset
  2011
     * @param era era index
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * @param year the normalized year number, with 0 indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * year 1 BCE, -1 indicating 2 BCE, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * @param fieldMask the calendar fields to be used for the date calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * @return the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * @see Calendar#selectFields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    private long getFixedDate(int era, int year, int fieldMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        int month = JANUARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        int firstDayOfMonth = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        if (isFieldSet(fieldMask, MONTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            // No need to check if MONTH has been set (no isSet(MONTH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            // call) since its unset value happens to be JANUARY (0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            month = internalGet(MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            // If the month is out of range, adjust it into range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            if (month > DECEMBER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                year += month / 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                month %= 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            } else if (month < JANUARY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                int[] rem = new int[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                year += CalendarUtils.floorDivide(month, 12, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                month = rem[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            if (year == 1 && era != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                CalendarDate d = eras[era].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                month = d.getMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                firstDayOfMonth = d.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        // Adjust the base date if year is the minimum value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        if (year == MIN_VALUES[YEAR]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            CalendarDate dx = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            int m = dx.getMonth() - 1;
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2047
            if (month < m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                month = m;
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2049
            }
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2050
            if (month == m) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                firstDayOfMonth = dx.getDayOfMonth();
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2052
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        LocalGregorianCalendar.Date date = jcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        date.setEra(era > 0 ? eras[era] : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        date.setDate(year, month + 1, firstDayOfMonth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        jcal.normalize(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        // Get the fixed date since Jan 1, 1 (Gregorian). We are on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        // the first day of either `month' or January in 'year'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        long fixedDate = jcal.getFixedDate(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        if (isFieldSet(fieldMask, MONTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            // Month-based calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            if (isFieldSet(fieldMask, DAY_OF_MONTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                // We are on the "first day" of the month (which may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                // not be 1). Just add the offset if DAY_OF_MONTH is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                // set. If the isSet call returns false, that means
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                // DAY_OF_MONTH has been selected just because of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                // selected combination. We don't need to add any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                // since the default value is the "first day".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                if (isSet(DAY_OF_MONTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    // To avoid underflow with DAY_OF_MONTH-firstDayOfMonth, add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                    // DAY_OF_MONTH, then subtract firstDayOfMonth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                    fixedDate += internalGet(DAY_OF_MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                    fixedDate -= firstDayOfMonth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                if (isFieldSet(fieldMask, WEEK_OF_MONTH)) {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2081
                    long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2082
                                                                                            getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    // If we have enough days in the first week, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                    // move to the previous week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                    if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                        firstDayOfWeek -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                    if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2089
                        firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2090
                                                                                           internalGet(DAY_OF_WEEK));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                    // In lenient mode, we treat days of the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                    // months as a part of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                    // WEEK_OF_MONTH. See 4633646.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                    fixedDate = firstDayOfWeek + 7 * (internalGet(WEEK_OF_MONTH) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                    int dayOfWeek;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                    if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        dayOfWeek = internalGet(DAY_OF_WEEK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        dayOfWeek = getFirstDayOfWeek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                    // We are basing this on the day-of-week-in-month.  The only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                    // trickiness occurs if the day-of-week-in-month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                    // negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                    int dowim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                    if (isFieldSet(fieldMask, DAY_OF_WEEK_IN_MONTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        dowim = internalGet(DAY_OF_WEEK_IN_MONTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                        dowim = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    if (dowim >= 0) {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2113
                        fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + (7 * dowim) - 1,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2114
                                                                                      dayOfWeek);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                        // Go to the first day of the next week of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                        // the specified week boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                        int lastDate = monthLength(month, year) + (7 * (dowim + 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                        // 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: 5506
diff changeset
  2120
                        fixedDate = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + lastDate - 1,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2121
                                                                                      dayOfWeek);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            // We are on the first day of the year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
            if (isFieldSet(fieldMask, DAY_OF_YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                if (isTransitionYear(date.getNormalizedYear())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    fixedDate = getFixedDateJan1(date, fixedDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                // Add the offset, then subtract 1. (Make sure to avoid underflow.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                fixedDate += internalGet(DAY_OF_YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                fixedDate--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            } else {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2135
                long firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(fixedDate + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2136
                                                                                        getFirstDayOfWeek());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                // If we have enough days in the first week, then move
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                // to the previous week.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                if ((firstDayOfWeek - fixedDate) >= getMinimalDaysInFirstWeek()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                    firstDayOfWeek -= 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                if (isFieldSet(fieldMask, DAY_OF_WEEK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    int dayOfWeek = internalGet(DAY_OF_WEEK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                    if (dayOfWeek != getFirstDayOfWeek()) {
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2145
                        firstDayOfWeek = LocalGregorianCalendar.getDayOfWeekDateOnOrBefore(firstDayOfWeek + 6,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2146
                                                                                           dayOfWeek);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                fixedDate = firstDayOfWeek + 7 * ((long)internalGet(WEEK_OF_YEAR) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        return fixedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * Returns the fixed date of the first day of the year (usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * January 1) before the specified date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * @param date the date for which the first day of the year is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * calculated. The date has to be in the cut-over year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * @param fixedDate the fixed date representation of the date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2163
    private long getFixedDateJan1(LocalGregorianCalendar.Date date, long fixedDate) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        Era era = date.getEra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        if (date.getEra() != null && date.getYear() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            for (int eraIndex = getEraIndex(date); eraIndex > 0; eraIndex--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                CalendarDate d = eras[eraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                long fd = gcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                // There might be multiple era transitions in a year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                if (fd > fixedDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        CalendarDate d = gcal.newCalendarDate(TimeZone.NO_TIMEZONE);
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2177
        d.setDate(date.getNormalizedYear(), Gregorian.JANUARY, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        return gcal.getFixedDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
     * Returns the fixed date of the first date of the month (usually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     * the 1st of the month) before the specified date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * @param date the date for which the first day of the month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * calculated. The date must be in the era transition year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * @param fixedDate the fixed date representation of the date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2189
    private long getFixedDateMonth1(LocalGregorianCalendar.Date date,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                                          long fixedDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        int eraIndex = getTransitionEraIndex(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        if (eraIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            long transition = sinceFixedDates[eraIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            // If the given date is on or after the transition date, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            // return the transition date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (transition <= fixedDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                return transition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        // Otherwise, we can use the 1st day of the month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        return fixedDate - date.getDayOfMonth() + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * Returns a LocalGregorianCalendar.Date produced from the specified fixed date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * @param fd the fixed date
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2210
    private static LocalGregorianCalendar.Date getCalendarDate(long fd) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        LocalGregorianCalendar.Date d = jcal.newCalendarDate(TimeZone.NO_TIMEZONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        jcal.getCalendarDateFromFixedDate(d, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * Returns the length of the specified month in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * Gregorian year. The year number must be normalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13583
diff changeset
  2220
     * @see GregorianCalendar#isLeapYear(int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2222
    private int monthLength(int month, int gregorianYear) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
        return CalendarUtils.isGregorianLeapYear(gregorianYear) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
            GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
     * Returns the length of the specified month in the year provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * by internalGet(YEAR).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13583
diff changeset
  2231
     * @see GregorianCalendar#isLeapYear(int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2233
    private int monthLength(int month) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        assert jdate.isNormalized();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        return jdate.isLeapYear() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            GregorianCalendar.LEAP_MONTH_LENGTH[month] : GregorianCalendar.MONTH_LENGTH[month];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2239
    private int actualMonthLength() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        int length = jcal.getMonthLength(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        int eraIndex = getTransitionEraIndex(jdate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
        if (eraIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            long transitionFixedDate = sinceFixedDates[eraIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            CalendarDate d = eras[eraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
            if (transitionFixedDate <= cachedFixedDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                length -= d.getDayOfMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                length = d.getDayOfMonth() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * Returns the index to the new era if the given date is in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * transition month.  For example, if the give date is Heisei 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * (1989) January 20, then the era index for Heisei is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * returned. Likewise, if the given date is Showa 64 (1989)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * January 3, then the era index for Heisei is returned. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * given date is not in any transition month, then -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2262
    private static int getTransitionEraIndex(LocalGregorianCalendar.Date date) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        int eraIndex = getEraIndex(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        CalendarDate transitionDate = eras[eraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        if (transitionDate.getYear() == date.getNormalizedYear() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            transitionDate.getMonth() == date.getMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            return eraIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        if (eraIndex < eras.length - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            transitionDate = eras[++eraIndex].getSinceDate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            if (transitionDate.getYear() == date.getNormalizedYear() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                transitionDate.getMonth() == date.getMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                return eraIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2279
    private boolean isTransitionYear(int normalizedYear) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
        for (int i = eras.length - 1; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
            int transitionYear = eras[i].getSinceDate().getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
            if (normalizedYear == transitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
            if (normalizedYear > transitionYear) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2292
    private static int getEraIndex(LocalGregorianCalendar.Date date) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        Era era = date.getEra();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        for (int i = eras.length - 1; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            if (eras[i] == era) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * Returns this object if it's normalized (all fields and time are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
     * in sync). Otherwise, a cloned object is returned after calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * complete() in lenient mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2307
    private JapaneseImperialCalendar getNormalizedCalendar() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        JapaneseImperialCalendar jc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        if (isFullyNormalized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            jc = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
            // Create a clone and normalize the calendar fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            jc = (JapaneseImperialCalendar) this.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            jc.setLenient(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            jc.complete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        return jc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     * After adjustments such as add(MONTH), add(YEAR), we don't want the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * 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
  2323
     * 3, we want it to go to Feb 28.  Adjustments which might run into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * problem call this method to retain the proper month.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2326
    private void pinDayOfMonth(LocalGregorianCalendar.Date date) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        int year = date.getYear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        int dom = date.getDayOfMonth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        if (year != getMinimum(YEAR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
            date.setDayOfMonth(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
            jcal.normalize(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            int monthLength = jcal.getMonthLength(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            if (dom > monthLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                date.setDayOfMonth(monthLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                date.setDayOfMonth(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            jcal.normalize(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            LocalGregorianCalendar.Date d = jcal.getCalendarDate(Long.MIN_VALUE, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            LocalGregorianCalendar.Date realDate = jcal.getCalendarDate(time, getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            long tod = realDate.getTimeOfDay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            // Use an equivalent year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            realDate.addYear(+400);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            realDate.setMonth(date.getMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            realDate.setDayOfMonth(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            jcal.normalize(realDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            int monthLength = jcal.getMonthLength(realDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            if (dom > monthLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                realDate.setDayOfMonth(monthLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                if (dom < d.getDayOfMonth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    realDate.setDayOfMonth(d.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    realDate.setDayOfMonth(dom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
            if (realDate.getDayOfMonth() == d.getDayOfMonth() && tod < d.getTimeOfDay()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                realDate.setDayOfMonth(Math.min(dom + 1, monthLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            // restore the year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            date.setDate(year, realDate.getMonth(), realDate.getDayOfMonth());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            // Don't normalize date here so as not to cause underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * Returns the new value after 'roll'ing the specified value and amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2370
    private static int getRolledValue(int value, int amount, int min, int max) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        assert value >= min && value <= max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        int range = max - min + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        amount %= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        int n = value + amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        if (n > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            n -= range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
        } else if (n < min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
            n += range;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        assert n >= min && n <= max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * Returns the ERA.  We need a special method for this because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * default ERA is the current era, but a zero (unset) ERA means before Meiji.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 5506
diff changeset
  2388
    private int internalGetEra() {
50478
2e3f73b616c2 8202088: Japanese new era implementation
naoto
parents: 47216
diff changeset
  2389
        return isSet(ERA) ? internalGet(ERA) : currentEra;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * Updates internal state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 54359
diff changeset
  2395
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        if (jdate == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
            jdate = jcal.newCalendarDate(getZone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            cachedFixedDate = Long.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
}