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