1/* 2 * Copyright (c) 2003, 2014, 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 26package sun.util.calendar; 27 28import java.util.Locale; 29import 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 * ----------------------------------------------------------------------- 52 * }</pre> 53 * 54 * @author Masayoshi Okutsu 55 * @since 1.5 56 */ 57 58public final class Era { 59 private final String name; 60 private final String abbr; 61 private final long since; 62 private final CalendarDate sinceDate; 63 private final boolean localTime; 64 65 /** 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, 1970 71 * (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> if 74 * <code>since</code> specifies UTC 75 */ 76 public Era(String name, String abbr, long since, boolean localTime) { 77 this.name = name; 78 this.abbr = abbr; 79 this.since = since; 80 this.localTime = localTime; 81 Gregorian gcal = CalendarSystem.getGregorianCalendar(); 82 BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null); 83 gcal.getCalendarDate(since, d); 84 sinceDate = new ImmutableGregorianDate(d); 85 } 86 87 public String getName() { 88 return name; 89 } 90 91 public String getDisplayName(Locale locale) { 92 return name; 93 } 94 95 public String getAbbreviation() { 96 return abbr; 97 } 98 99 public String getDiaplayAbbreviation(Locale locale) { 100 return abbr; 101 } 102 103 public long getSince(TimeZone zone) { 104 if (zone == null || !localTime) { 105 return since; 106 } 107 int offset = zone.getOffset(since); 108 return since - offset; 109 } 110 111 public CalendarDate getSinceDate() { 112 return sinceDate; 113 } 114 115 public boolean isLocalTime() { 116 return localTime; 117 } 118 119 public boolean equals(Object o) { 120 if (!(o instanceof Era)) { 121 return false; 122 } 123 Era that = (Era) o; 124 return name.equals(that.name) 125 && abbr.equals(that.abbr) 126 && since == that.since 127 && localTime == that.localTime; 128 } 129 130 private int hash = 0; 131 132 public int hashCode() { 133 if (hash == 0) { 134 hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32) 135 ^ (localTime ? 1 : 0); 136 } 137 return hash; 138 } 139 140 public String toString() { 141 StringBuilder sb = new StringBuilder(); 142 sb.append('['); 143 sb.append(getName()).append(" ("); 144 sb.append(getAbbreviation()).append(')'); 145 sb.append(" since ").append(getSinceDate()); 146 if (localTime) { 147 sb.setLength(sb.length() - 1); // remove 'Z' 148 sb.append(" local time"); 149 } 150 sb.append(']'); 151 return sb.toString(); 152 } 153} 154