Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (c) 2003, 2005, 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  * <!-- TODO: use HTML table -->
     45  * <pre><tt>
     46  *   Calendar system         Era name         Since (in Gregorian)
     47  *   -----------------------------------------------------------------------
     48  *   Japanese calendar       Meiji            1868-01-01 midnight local time
     49  *                           Taisho           1912-07-30 midnight local time
     50  *                           Showa            1926-12-26 midnight local time
     51  *                           Heisei           1989-01-08 midnight local time
     52  *   Julian calendar         BeforeCommonEra  -292275055-05-16T16:47:04.192Z
     53  *                           CommonEra        0000-12-30 midnight local time
     54  *   Taiwanese calendar      MinGuo           1911-01-01 midnight local time
     55  *   Thai Buddhist calendar  BuddhistEra      -543-01-01 midnight local time
     56  *   -----------------------------------------------------------------------
     57  * </tt></pre>
     58  *
     59  * @author Masayoshi Okutsu
     60  * @since 1.5
     61  */
     62 
     63 public final class Era {
     64     private final String name;
     65     private final String abbr;
     66     private final long since;
     67     private final CalendarDate sinceDate;
     68     private final boolean localTime;
     69 
     70     /**
     71      * Constructs an <code>Era</code> instance.
     72      *
     73      * @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)
     74      * @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")
     75      * @param since the time (millisecond offset from January 1, 1970
     76      * (Gregorian) UTC or local time) when the era starts, inclusive.
     77      * @param localTime <code>true</code> if <code>since</code>
     78      * specifies a local time; <code>false</code> if
     79      * <code>since</code> specifies UTC
     80      */
     81     public Era(String name, String abbr, long since, boolean localTime) {
     82         this.name = name;
     83         this.abbr = abbr;
     84         this.since = since;
     85         this.localTime = localTime;
     86         Gregorian gcal = CalendarSystem.getGregorianCalendar();
     87         BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);
     88         gcal.getCalendarDate(since, d);
     89         sinceDate = new ImmutableGregorianDate(d);
     90     }
     91 
     92     public String getName() {
     93         return name;
     94     }
     95 
     96     public String getDisplayName(Locale locale) {
     97         return name;
     98     }
     99 
    100     public String getAbbreviation() {
    101         return abbr;
    102     }
    103 
    104     public String getDiaplayAbbreviation(Locale locale) {
    105         return abbr;
    106     }
    107 
    108     public long getSince(TimeZone zone) {
    109         if (zone == null || !localTime) {
    110             return since;
    111         }
    112         int offset = zone.getOffset(since);
    113         return since - offset;
    114     }
    115 
    116     public CalendarDate getSinceDate() {
    117         return sinceDate;
    118     }
    119 
    120     public boolean isLocalTime() {
    121         return localTime;
    122     }
    123 
    124     public boolean equals(Object o) {
    125         if (!(o instanceof Era)) {
    126             return false;
    127         }
    128         Era that = (Era) o;
    129         return name.equals(that.name)
    130             && abbr.equals(that.abbr)
    131             && since == that.since
    132             && localTime == that.localTime;
    133     }
    134 
    135     private int hash = 0;
    136 
    137     public int hashCode() {
    138         if (hash == 0) {
    139             hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)
    140                 ^ (localTime ? 1 : 0);
    141         }
    142         return hash;
    143     }
    144 
    145     public String toString() {
    146         StringBuilder sb = new StringBuilder();
    147         sb.append('[');
    148         sb.append(getName()).append(" (");
    149         sb.append(getAbbreviation()).append(')');
    150         sb.append(" since ").append(getSinceDate());
    151         if (localTime) {
    152             sb.setLength(sb.length() - 1); // remove 'Z'
    153             sb.append(" local time");
    154         }
    155         sb.append(']');
    156         return sb.toString();
    157     }
    158 }
    159