public abstract class DateTimeZone
extends java.lang.Object
implements java.io.Serializable
A time zone is a system of rules to convert time from one geographic location to another. For example, Paris, France is one hour ahead of London, England. Thus when it is 10:00 in London, it is 11:00 in Paris.
All time zone rules are expressed, for historical reasons, relative to Greenwich, London. Local time in Greenwich is referred to as Greenwich Mean Time (GMT). This is similar, but not precisely identical, to Universal Coordinated Time, or UTC. This library only uses the term UTC.
Using this system, America/Los_Angeles is expressed as UTC-08:00, or UTC-07:00 in the summer. The offset -08:00 indicates that America/Los_Angeles time is obtained from UTC by adding -08:00, that is, by subtracting 8 hours.
The offset differs in the summer because of daylight saving time, or DST. The folowing definitions of time are generally used:
Unlike the Java TimeZone class, DateTimeZone is immutable. It also only supports long format time zone ids. Thus EST and ECT are not accepted. However, the factory that accepts a TimeZone will attempt to convert from the old short id to a suitable long id.
DateTimeZone is thread-safe and immutable, and all subclasses must be as well.
Modifier and Type | Field and Description |
---|---|
static DateTimeZone |
UTC
The time zone for Universal Coordinated Time
|
Modifier | Constructor and Description |
---|---|
protected |
DateTimeZone(java.lang.String id)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
long |
convertLocalToUTC(long instantLocal,
boolean strict)
Converts a local instant to a standard UTC instant with the same
local time.
|
long |
convertUTCToLocal(long instantUTC)
Converts a standard UTC instant to a local instant with the same
local time.
|
abstract boolean |
equals(java.lang.Object object)
Compare this datetime zone with another.
|
static DateTimeZone |
forID(java.lang.String id)
Gets a time zone instance for the specified time zone id.
|
static DateTimeZone |
forOffsetHours(int hoursOffset)
Gets a time zone instance for the specified offset to UTC in hours.
|
static DateTimeZone |
forOffsetHoursMinutes(int hoursOffset,
int minutesOffset)
Gets a time zone instance for the specified offset to UTC in hours and minutes.
|
static DateTimeZone |
forOffsetMillis(int millisOffset)
Gets a time zone instance for the specified offset to UTC in milliseconds.
|
static DateTimeZone |
forTimeZone(java.util.TimeZone zone)
Gets a time zone instance for a JDK TimeZone.
|
static java.util.Set |
getAvailableIDs()
Gets all the available IDs supported.
|
static DateTimeZone |
getDefault()
Gets the default time zone.
|
java.lang.String |
getID()
Gets the ID of this datetime zone.
|
long |
getMillisKeepLocal(DateTimeZone newZone,
long oldInstant)
Gets the millisecond instant in another zone keeping the same local time.
|
java.lang.String |
getName(long instant)
Gets the long name of this datetime zone suitable for display using
the default locale.
|
java.lang.String |
getName(long instant,
java.util.Locale locale)
Gets the long name of this datetime zone suitable for display using
the specified locale.
|
abstract java.lang.String |
getNameKey(long instant)
Returns a non-localized name that is unique to this time zone.
|
static NameProvider |
getNameProvider()
Gets the name provider factory.
|
abstract int |
getOffset(long instant)
Gets the millisecond offset to add to UTC to get local time.
|
int |
getOffset(ReadableInstant instant)
Gets the millisecond offset to add to UTC to get local time.
|
int |
getOffsetFromLocal(long instantLocal)
Gets the millisecond offset to subtract from local time to get UTC time.
|
static Provider |
getProvider()
Gets the zone provider factory.
|
java.lang.String |
getShortName(long instant)
Gets the short name of this datetime zone suitable for display using
the default locale.
|
java.lang.String |
getShortName(long instant,
java.util.Locale locale)
Gets the short name of this datetime zone suitable for display using
the specified locale.
|
abstract int |
getStandardOffset(long instant)
Gets the standard millisecond offset to add to UTC to get local time,
when standard time is in effect.
|
int |
hashCode()
Gets a hash code compatable with equals.
|
abstract boolean |
isFixed()
Returns true if this time zone has no transitions.
|
boolean |
isLocalDateTimeGap(LocalDateTime localDateTime)
Checks if the given
LocalDateTime is within a gap. |
boolean |
isStandardOffset(long instant)
Checks whether, at a particular instant, the offset is standard or not.
|
abstract long |
nextTransition(long instant)
Advances the given instant to where the time zone offset or name changes.
|
abstract long |
previousTransition(long instant)
Retreats the given instant to where the time zone offset or name changes.
|
static void |
setDefault(DateTimeZone zone)
Sets the default time zone.
|
static void |
setNameProvider(NameProvider nameProvider)
Sets the name provider factory.
|
static void |
setProvider(Provider provider)
Sets the zone provider factory.
|
java.lang.String |
toString()
Gets the datetime zone as a string, which is simply its ID.
|
java.util.TimeZone |
toTimeZone()
Get the datetime zone as a
TimeZone . |
protected java.lang.Object |
writeReplace()
By default, when DateTimeZones are serialized, only a "stub" object
referring to the id is written out.
|
public static final DateTimeZone UTC
protected DateTimeZone(java.lang.String id)
id
- the id to usejava.lang.IllegalArgumentException
- if the id is nullpublic static DateTimeZone getDefault()
public static void setDefault(DateTimeZone zone) throws java.lang.SecurityException
zone
- the default datetime zone object, must not be nulljava.lang.IllegalArgumentException
- if the zone is nulljava.lang.SecurityException
- if the application has insufficient security rightspublic static DateTimeZone forID(java.lang.String id)
The time zone id may be one of those returned by getAvailableIDs.
Short ids, as accepted by TimeZone
, are not accepted.
All IDs must be specified in the long format.
The exception is UTC, which is an acceptable id.
Alternatively a locale independent, fixed offset, datetime zone can
be specified. The form [+-]hh:mm
can be used.
id
- the ID of the datetime zone, null means defaultjava.lang.IllegalArgumentException
- if the ID is not recognisedpublic static DateTimeZone forOffsetHours(int hoursOffset) throws java.lang.IllegalArgumentException
This factory is a convenient way of constructing zones with a fixed offset.
hoursOffset
- the offset in hours from UTCjava.lang.IllegalArgumentException
- if the offset is too large or too smallpublic static DateTimeZone forOffsetHoursMinutes(int hoursOffset, int minutesOffset) throws java.lang.IllegalArgumentException
This factory is a convenient way of constructing zones with a fixed offset. The minutes value is always positive and in the range 0 to 59. If constructed with the values (-2, 30), the resultiong zone is '-02:30'.
hoursOffset
- the offset in hours from UTCminutesOffset
- the offset in minutes from UTC, must be between 0 and 59 inclusivejava.lang.IllegalArgumentException
- if the offset or minute is too large or too smallpublic static DateTimeZone forOffsetMillis(int millisOffset)
millisOffset
- the offset in millis from UTCpublic static DateTimeZone forTimeZone(java.util.TimeZone zone)
DateTimeZone only accepts a subset of the IDs from TimeZone. The excluded IDs are the short three letter form (except UTC). This method will attempt to convert between time zones created using the short IDs and the full version.
This method is not designed to parse time zones with rules created by
applications using SimpleTimeZone
directly.
zone
- the zone to convert, null means defaultjava.lang.IllegalArgumentException
- if the zone is not recognisedpublic static java.util.Set getAvailableIDs()
public static Provider getProvider()
The zone provider is a pluggable instance factory that supplies the actual instances of DateTimeZone.
public static void setProvider(Provider provider) throws java.lang.SecurityException
The zone provider is a pluggable instance factory that supplies the actual instances of DateTimeZone.
provider
- provider to use, or null for defaultjava.lang.SecurityException
- if you do not have the permission DateTimeZone.setProviderjava.lang.IllegalArgumentException
- if the provider is invalidpublic static NameProvider getNameProvider()
The name provider is a pluggable instance factory that supplies the names of each DateTimeZone.
public static void setNameProvider(NameProvider nameProvider) throws java.lang.SecurityException
The name provider is a pluggable instance factory that supplies the names of each DateTimeZone.
nameProvider
- provider to use, or null for defaultjava.lang.SecurityException
- if you do not have the permission DateTimeZone.setNameProviderjava.lang.IllegalArgumentException
- if the provider is invalidpublic final java.lang.String getID()
public abstract java.lang.String getNameKey(long instant)
instant
- milliseconds from 1970-01-01T00:00:00Z to get the name forpublic final java.lang.String getShortName(long instant)
If the name is not available for the locale, then this method returns a
string in the format [+-]hh:mm
.
instant
- milliseconds from 1970-01-01T00:00:00Z to get the name forpublic java.lang.String getShortName(long instant, java.util.Locale locale)
If the name is not available for the locale, then this method returns a
string in the format [+-]hh:mm
.
instant
- milliseconds from 1970-01-01T00:00:00Z to get the name forlocale
- the locale to get the name forpublic final java.lang.String getName(long instant)
If the name is not available for the locale, then this method returns a
string in the format [+-]hh:mm
.
instant
- milliseconds from 1970-01-01T00:00:00Z to get the name forpublic java.lang.String getName(long instant, java.util.Locale locale)
If the name is not available for the locale, then this method returns a
string in the format [+-]hh:mm
.
instant
- milliseconds from 1970-01-01T00:00:00Z to get the name forlocale
- the locale to get the name forpublic abstract int getOffset(long instant)
instant
- milliseconds from 1970-01-01T00:00:00Z to get the offset forpublic final int getOffset(ReadableInstant instant)
instant
- instant to get the offset for, null means nowpublic abstract int getStandardOffset(long instant)
instant
- milliseconds from 1970-01-01T00:00:00Z to get the offset forpublic boolean isStandardOffset(long instant)
This method can be used to determine whether Summer Time (DST) applies. As a general rule, if the offset at the specified instant is standard, then either Winter time applies, or there is no Summer Time. If the instant is not standard, then Summer Time applies.
The implementation of the method is simply whether getOffset(long)
equals getStandardOffset(long)
at the specified instant.
instant
- milliseconds from 1970-01-01T00:00:00Z to get the offset forpublic int getOffsetFromLocal(long instantLocal)
millisLocal == millisUTC + getOffset(millisUTC) millisUTC == millisLocal - getOffsetFromLocal(millisLocal)NOTE: After calculating millisLocal, some error may be introduced. At offset transitions (due to DST or other historical changes), ranges of local times may map to different UTC times.
This method will return an offset suitable for calculating an instant
after any DST gap. For example, consider a zone with a cutover
from 01:00 to 01:59:
Input: 00:00 Output: 00:00
Input: 00:30 Output: 00:30
Input: 01:00 Output: 02:00
Input: 01:30 Output: 02:30
Input: 02:00 Output: 02:00
Input: 02:30 Output: 02:30
NOTE: The behaviour of this method changed in v1.5, with the emphasis on returning a consistent result later along the time-line (shown above).
instantLocal
- the millisecond instant, relative to this time zone, to
get the offset forpublic long convertUTCToLocal(long instantUTC)
instantUTC
- the UTC instant to convert to localjava.lang.ArithmeticException
- if the result overflows a longpublic long convertLocalToUTC(long instantLocal, boolean strict)
instantLocal
- the local instant to convert to UTCstrict
- whether the conversion should reject non-existent local timesjava.lang.ArithmeticException
- if the result overflows a longjava.lang.IllegalArgumentException
- if the zone has no eqivalent local timepublic long getMillisKeepLocal(DateTimeZone newZone, long oldInstant)
The conversion is performed by converting the specified UTC millis to local millis in this zone, then converting back to UTC millis in the new zone.
newZone
- the new zone, null means defaultoldInstant
- the UTC millisecond instant to convertpublic boolean isLocalDateTimeGap(LocalDateTime localDateTime)
LocalDateTime
is within a gap.
When switching from standard time to Daylight Savings Time there is typically a gap where a clock hour is missing. This method identifies whether the local datetime refers to such a gap.
localDateTime
- the time to check, not nullpublic abstract boolean isFixed()
public abstract long nextTransition(long instant)
instant
- milliseconds from 1970-01-01T00:00:00Zpublic abstract long previousTransition(long instant)
instant
- milliseconds from 1970-01-01T00:00:00Zpublic java.util.TimeZone toTimeZone()
TimeZone
.public abstract boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- the object to compare withpublic int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
protected java.lang.Object writeReplace() throws java.io.ObjectStreamException
java.io.ObjectStreamException
Copyright (c) 2001-2006 - Joda.org