Path: blob/master/src/java.base/share/classes/sun/util/calendar/Era.java
41159 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util.calendar;2627import java.util.Locale;28import java.util.TimeZone;2930/**31* The class <code>Era</code> represents a calendar era that defines a32* period of time in which the same year numbering is used. For33* example, Gregorian year 2004 is <I>Heisei</I> 16 in the Japanese34* calendar system. An era starts at any point of time (Gregorian) that is35* represented by <code>CalendarDate</code>.36*37* <p><code>Era</code>s that are applicable to a particular calendar38* system can be obtained by calling {@link CalendarSystem#getEras}39* one of which can be used to specify a date in40* <code>CalendarDate</code>.41*42* <p>The following era names are defined in this release.43* <pre>{@code44* Calendar system Era name Since (in Gregorian)45* -----------------------------------------------------------------------46* Japanese calendar Meiji 1868-01-01T00:00:00 local time47* Taisho 1912-07-30T00:00:00 local time48* Showa 1926-12-25T00:00:00 local time49* Heisei 1989-01-08T00:00:00 local time50* Reiwa 2019-05-01T00:00:00 local time51* -----------------------------------------------------------------------52* }</pre>53*54* @author Masayoshi Okutsu55* @since 1.556*/5758public final class Era {59private final String name;60private final String abbr;61private final long since;62private final CalendarDate sinceDate;63private final boolean localTime;6465/**66* Constructs an <code>Era</code> instance.67*68* @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)69* @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")70* @param since the time (millisecond offset from January 1, 197071* (Gregorian) UTC or local time) when the era starts, inclusive.72* @param localTime <code>true</code> if <code>since</code>73* specifies a local time; <code>false</code> if74* <code>since</code> specifies UTC75*/76public Era(String name, String abbr, long since, boolean localTime) {77this.name = name;78this.abbr = abbr;79this.since = since;80this.localTime = localTime;81Gregorian gcal = CalendarSystem.getGregorianCalendar();82BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);83gcal.getCalendarDate(since, d);84sinceDate = new ImmutableGregorianDate(d);85}8687public String getName() {88return name;89}9091public String getDisplayName(Locale locale) {92return name;93}9495public String getAbbreviation() {96return abbr;97}9899public String getDiaplayAbbreviation(Locale locale) {100return abbr;101}102103public long getSince(TimeZone zone) {104if (zone == null || !localTime) {105return since;106}107int offset = zone.getOffset(since);108return since - offset;109}110111public CalendarDate getSinceDate() {112return sinceDate;113}114115public boolean isLocalTime() {116return localTime;117}118119public boolean equals(Object o) {120if (!(o instanceof Era)) {121return false;122}123Era that = (Era) o;124return name.equals(that.name)125&& abbr.equals(that.abbr)126&& since == that.since127&& localTime == that.localTime;128}129130private int hash = 0;131132public int hashCode() {133if (hash == 0) {134hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)135^ (localTime ? 1 : 0);136}137return hash;138}139140public String toString() {141StringBuilder sb = new StringBuilder();142sb.append('[');143sb.append(getName()).append(" (");144sb.append(getAbbreviation()).append(')');145sb.append(" since ").append(getSinceDate());146if (localTime) {147sb.setLength(sb.length() - 1); // remove 'Z'148sb.append(" local time");149}150sb.append(']');151return sb.toString();152}153}154155156