jdk/src/share/classes/sun/util/calendar/Era.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <!-- TODO: use HTML table -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   Calendar system         Era name         Since (in Gregorian)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   -----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   Japanese calendar       Meiji            1868-01-01 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *                           Taisho           1912-07-30 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *                           Showa            1926-12-26 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *                           Heisei           1989-01-08 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   Julian calendar         BeforeCommonEra  -292275055-05-16T16:47:04.192Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *                           CommonEra        0000-12-30 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   Taiwanese calendar      MinGuo           1911-01-01 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   Thai Buddhist calendar  BuddhistEra      -543-01-01 midnight local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   -----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Masayoshi Okutsu
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public final class Era {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final String abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final long since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final CalendarDate sinceDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final boolean localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructs an <code>Era</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param since the time (millisecond offset from January 1, 1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * (Gregorian) UTC or local time) when the era starts, inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param localTime <code>true</code> if <code>since</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * specifies a local time; <code>false</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>since</code> specifies UTC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public Era(String name, String abbr, long since, boolean localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.abbr = abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.since = since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.localTime = localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Gregorian gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        gcal.getCalendarDate(since, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        sinceDate = new ImmutableGregorianDate(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public String getDisplayName(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public String getAbbreviation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public String getDiaplayAbbreviation(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return abbr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public long getSince(TimeZone zone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (zone == null || !localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return since;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int offset = zone.getOffset(since);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return since - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public CalendarDate getSinceDate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return sinceDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public boolean isLocalTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (!(o instanceof Era)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Era that = (Era) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return name.equals(that.name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            && abbr.equals(that.abbr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            && since == that.since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            && localTime == that.localTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private int hash = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (hash == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                ^ (localTime ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        sb.append('[');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        sb.append(getName()).append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        sb.append(getAbbreviation()).append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        sb.append(" since ").append(getSinceDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (localTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            sb.setLength(sb.length() - 1); // remove 'Z'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            sb.append(" local time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        sb.append(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}