1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.util.calendar;
  27 
  28 import java.util.Locale;
  29 import java.util.TimeZone;
  30 
  31 /**
  32  * The class <code>Era</code> represents a calendar era that defines a
  33  * period of time in which the same year numbering is used. For
  34  * example, Gregorian year 2004 is <I>Heisei</I> 16 in the Japanese
  35  * calendar system. An era starts at any point of time (Gregorian) that is
  36  * represented by <code>CalendarDate</code>.
  37  *
  38  * <p><code>Era</code>s that are applicable to a particular calendar
  39  * system can be obtained by calling {@link CalendarSystem#getEras}
  40  * one of which can be used to specify a date in
  41  * <code>CalendarDate</code>.
  42  *
  43  * <p>The following era names are defined in this release.
  44  * <pre>{@code
  45  *   Calendar system         Era name         Since (in Gregorian)
  46  *   -----------------------------------------------------------------------
  47  *   Japanese calendar       Meiji            1868-01-01T00:00:00 local time
  48  *                           Taisho           1912-07-30T00:00:00 local time
  49  *                           Showa            1926-12-25T00:00:00 local time
  50  *                           Heisei           1989-01-08T00:00:00 local time
  51  *                           NewEra           2019-05-01T00:00:00 local time
  52  *   -----------------------------------------------------------------------
  53  * }</pre>
  54  *
  55  * @author Masayoshi Okutsu
  56  * @since 1.5
  57  */
  58 
  59 public final class Era {
  60     private final String name;
  61     private final String abbr;
  62     private final long since;
  63     private final CalendarDate sinceDate;
  64     private final boolean localTime;
  65 
  66     /**
  67      * Constructs an <code>Era</code> instance.
  68      *
  69      * @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)
  70      * @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")
  71      * @param since the time (millisecond offset from January 1, 1970
  72      * (Gregorian) UTC or local time) when the era starts, inclusive.
  73      * @param localTime <code>true</code> if <code>since</code>
  74      * specifies a local time; <code>false</code> if
  75      * <code>since</code> specifies UTC
  76      */
  77     public Era(String name, String abbr, long since, boolean localTime) {
  78         this.name = name;
  79         this.abbr = abbr;
  80         this.since = since;
  81         this.localTime = localTime;
  82         Gregorian gcal = CalendarSystem.getGregorianCalendar();
  83         BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);
  84         gcal.getCalendarDate(since, d);
  85         sinceDate = new ImmutableGregorianDate(d);
  86     }
  87 
  88     public String getName() {
  89         return name;
  90     }
  91 
  92     public String getDisplayName(Locale locale) {
  93         return name;
  94     }
  95 
  96     public String getAbbreviation() {
  97         return abbr;
  98     }
  99 
 100     public String getDiaplayAbbreviation(Locale locale) {
 101         return abbr;
 102     }
 103 
 104     public long getSince(TimeZone zone) {
 105         if (zone == null || !localTime) {
 106             return since;
 107         }
 108         int offset = zone.getOffset(since);
 109         return since - offset;
 110     }
 111 
 112     public CalendarDate getSinceDate() {
 113         return sinceDate;
 114     }
 115 
 116     public boolean isLocalTime() {
 117         return localTime;
 118     }
 119 
 120     public boolean equals(Object o) {
 121         if (!(o instanceof Era)) {
 122             return false;
 123         }
 124         Era that = (Era) o;
 125         return name.equals(that.name)
 126             && abbr.equals(that.abbr)
 127             && since == that.since
 128             && localTime == that.localTime;
 129     }
 130 
 131     private int hash = 0;
 132 
 133     public int hashCode() {
 134         if (hash == 0) {
 135             hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)
 136                 ^ (localTime ? 1 : 0);
 137         }
 138         return hash;
 139     }
 140 
 141     public String toString() {
 142         StringBuilder sb = new StringBuilder();
 143         sb.append('[');
 144         sb.append(getName()).append(" (");
 145         sb.append(getAbbreviation()).append(')');
 146         sb.append(" since ").append(getSinceDate());
 147         if (localTime) {
 148             sb.setLength(sb.length() - 1); // remove 'Z'
 149             sb.append(" local time");
 150         }
 151         sb.append(']');
 152         return sb.toString();
 153     }
 154 }