jdk/src/share/classes/sun/util/calendar/Era.java
author okutsu
Thu, 07 Aug 2014 13:04:26 +0900
changeset 25966 71ee1f15845e
parent 5506 202f599c92aa
permissions -rw-r--r--
8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era Reviewed-by: naoto, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2014, 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 sun.util.calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The class <code>Era</code> represents a calendar era that defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * period of time in which the same year numbering is used. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * example, Gregorian year 2004 is <I>Heisei</I> 16 in the Japanese
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * calendar system. An era starts at any point of time (Gregorian) that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * represented by <code>CalendarDate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p><code>Era</code>s that are applicable to a particular calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * system can be obtained by calling {@link CalendarSystem#getEras}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * one of which can be used to specify a date in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>CalendarDate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>The following era names are defined in this release.
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    44
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   Calendar system         Era name         Since (in Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   -----------------------------------------------------------------------
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    47
 *   Japanese calendar       Meiji            1868-01-01T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    48
 *                           Taisho           1912-07-30T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    49
 *                           Showa            1926-12-25T00:00:00 local time
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    50
 *                           Heisei           1989-01-08T00:00:00 local time
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   -----------------------------------------------------------------------
25966
71ee1f15845e 8048123: Replace calendars.properties with another mechanism to specify a new Japanese calendar era
okutsu
parents: 5506
diff changeset
    52
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Masayoshi Okutsu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public final class Era {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final String abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final long since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final CalendarDate sinceDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final boolean localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Constructs an <code>Era</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param since the time (millisecond offset from January 1, 1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * (Gregorian) UTC or local time) when the era starts, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param localTime <code>true</code> if <code>since</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * specifies a local time; <code>false</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>since</code> specifies UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public Era(String name, String abbr, long since, boolean localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.abbr = abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.since = since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.localTime = localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Gregorian gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        gcal.getCalendarDate(since, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        sinceDate = new ImmutableGregorianDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public String getDisplayName(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public String getAbbreviation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public String getDiaplayAbbreviation(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public long getSince(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (zone == null || !localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int offset = zone.getOffset(since);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return since - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public CalendarDate getSinceDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return sinceDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public boolean isLocalTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!(o instanceof Era)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Era that = (Era) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return name.equals(that.name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            && abbr.equals(that.abbr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            && since == that.since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            && localTime == that.localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (hash == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                ^ (localTime ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sb.append(getName()).append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        sb.append(getAbbreviation()).append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        sb.append(" since ").append(getSinceDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            sb.setLength(sb.length() - 1); // remove 'Z'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            sb.append(" local time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}