|
Java example source code file (Era.java)
The Era.java Java example source code
/*
* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.util.calendar;
import java.util.Locale;
import java.util.TimeZone;
/**
* The class <code>Era represents a calendar era that defines a
* period of time in which the same year numbering is used. For
* example, Gregorian year 2004 is <I>Heisei 16 in the Japanese
* calendar system. An era starts at any point of time (Gregorian) that is
* represented by <code>CalendarDate.
*
* <p>
*
* @author Masayoshi Okutsu
* @since 1.5
*/
public final class Era {
private final String name;
private final String abbr;
private final long since;
private final CalendarDate sinceDate;
private final boolean localTime;
/**
* Constructs an <code>Era instance.
*
* @param name the era name (e.g., "BeforeCommonEra" for the Julian calendar system)
* @param abbr the abbreviation of the era name (e.g., "B.C.E." for "BeforeCommonEra")
* @param since the time (millisecond offset from January 1, 1970
* (Gregorian) UTC or local time) when the era starts, inclusive.
* @param localTime <code>true if since
* specifies a local time; <code>false if
* <code>since specifies UTC
*/
public Era(String name, String abbr, long since, boolean localTime) {
this.name = name;
this.abbr = abbr;
this.since = since;
this.localTime = localTime;
Gregorian gcal = CalendarSystem.getGregorianCalendar();
BaseCalendar.Date d = (BaseCalendar.Date) gcal.newCalendarDate(null);
gcal.getCalendarDate(since, d);
sinceDate = new ImmutableGregorianDate(d);
}
public String getName() {
return name;
}
public String getDisplayName(Locale locale) {
return name;
}
public String getAbbreviation() {
return abbr;
}
public String getDiaplayAbbreviation(Locale locale) {
return abbr;
}
public long getSince(TimeZone zone) {
if (zone == null || !localTime) {
return since;
}
int offset = zone.getOffset(since);
return since - offset;
}
public CalendarDate getSinceDate() {
return sinceDate;
}
public boolean isLocalTime() {
return localTime;
}
public boolean equals(Object o) {
if (!(o instanceof Era)) {
return false;
}
Era that = (Era) o;
return name.equals(that.name)
&& abbr.equals(that.abbr)
&& since == that.since
&& localTime == that.localTime;
}
private int hash = 0;
public int hashCode() {
if (hash == 0) {
hash = name.hashCode() ^ abbr.hashCode() ^ (int)since ^ (int)(since >> 32)
^ (localTime ? 1 : 0);
}
return hash;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
sb.append(getName()).append(" (");
sb.append(getAbbreviation()).append(')');
sb.append(" since ").append(getSinceDate());
if (localTime) {
sb.setLength(sb.length() - 1); // remove 'Z'
sb.append(" local time");
}
sb.append(']');
return sb.toString();
}
}
Other Java examples (source code examples)Here is a short list of links related to this Java Era.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.