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