Class TimeZone
- java.lang.Object
-
- java.util.TimeZone
-
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
SimpleTimeZone,TimeZoneAdapter,ZoneInfo
public abstract class TimeZone extends Object implements Serializable, Cloneable
TimeZonerepresents a time zone, primarily used for configuring aCalendarorSimpleDateFormatinstance.Most applications will use
getDefault()which returns aTimeZonebased on the time zone where the program is running.You can also get a specific
TimeZoneby Olson ID.It is highly unlikely you'll ever want to use anything but the factory methods yourself. Let classes like
CalendarandSimpleDateFormatdo the date computations for you.If you do need to do date computations manually, there are two common cases to take into account:
- Somewhere like California, where daylight time is used.
The
useDaylightTime()method will always return true, andinDaylightTime(java.util.Date)must be used to determine whether or not daylight time applies to a givenDate. ThegetRawOffset()method will return a raw offset of (in this case) -8 hours from UTC, which isn't usually very useful. More usefully, thegetOffset(long)methods return the actual offset from UTC for a given point in time; this is the raw offset plus (if the point in time isin daylight time) the applicableDST savings(usually, but not necessarily, 1 hour). - Somewhere like Japan, where daylight time is not used.
The
useDaylightTime()andinDaylightTime(java.util.Date)methods both always return false, and the raw and actual offsets will always be the same.
Note the type returned by the factory methods
getDefault()andgetTimeZone(java.lang.String)is implementation dependent. This may introduce serialization incompatibility issues between different implementations, or different versions of Android.- See Also:
Calendar,GregorianCalendar,SimpleDateFormat, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TimeZone()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Objectclone()Returns a new time zone with the same ID, raw offset, and daylight savings time rules as this time zone.static StringcreateGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis)Returns a string representation of an offset from UTC.static String[]getAvailableIDs()Returns the system's installed time zone IDs.static String[]getAvailableIDs(int offsetMillis)Returns the IDs of the time zones whose offset from UTC isoffsetMillis.static TimeZonegetDefault()Returns the user's preferred time zone.StringgetDisplayName()Equivalent togetDisplayName(false, TimeZone.LONG, Locale.getDefault()).StringgetDisplayName(boolean daylightTime, int style)Equivalent togetDisplayName(daylightTime, style, Locale.getDefault()).StringgetDisplayName(boolean daylightTime, int style, Locale locale)StringgetDisplayName(Locale locale)Equivalent togetDisplayName(false, TimeZone.LONG, locale).intgetDSTSavings()Returns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned bygetRawOffset()).StringgetID()Returns the ID of thisTimeZone, such asAmerica/Los_Angeles,GMT-08:00orUTC.abstract intgetOffset(int era, int year, int month, int day, int dayOfWeek, int timeOfDayMillis)Returns this time zone's offset in milliseconds from UTC at the specified date and time.intgetOffset(long time)Returns the offset in milliseconds from UTC for this time zone attime.abstract intgetRawOffset()Returns the offset in milliseconds from UTC of this time zone's standard time.static TimeZonegetTimeZone(String id)Returns aTimeZonecorresponding to the givenid, orGMTfor unknown ids.booleanhasSameRules(TimeZone timeZone)Returns true iftimeZonehas the same rules as this time zone.abstract booleaninDaylightTime(Date time)Returns true iftimeis in a daylight savings time period for this time zone.static voidsetDefault(TimeZone timeZone)Overrides the default time zone for the current process only.voidsetID(String id)Sets the ID of thisTimeZone.abstract voidsetRawOffset(int offsetMillis)Sets the offset in milliseconds from UTC of this time zone's standard time.abstract booleanuseDaylightTime()Returns true if this time zone has a future transition to or from daylight savings time.
-
-
-
Field Detail
-
SHORT
public static final int SHORT
The short display name style, such asPDT. Requests for this style may yield GMT offsets likeGMT-08:00.- See Also:
- Constant Field Values
-
LONG
public static final int LONG
The long display name style, such asPacific Daylight Time. Requests for this style may yield GMT offsets likeGMT-08:00.- See Also:
- Constant Field Values
-
-
Method Detail
-
clone
public Object clone()
Returns a new time zone with the same ID, raw offset, and daylight savings time rules as this time zone.
-
getAvailableIDs
public static String[] getAvailableIDs()
Returns the system's installed time zone IDs. Any of these IDs can be passed togetTimeZone(java.lang.String)to lookup the corresponding time zone instance.
-
getAvailableIDs
public static String[] getAvailableIDs(int offsetMillis)
Returns the IDs of the time zones whose offset from UTC isoffsetMillis. Any of these IDs can be passed togetTimeZone(java.lang.String)to lookup the corresponding time zone instance.- Returns:
- a possibly-empty array.
-
getDefault
public static TimeZone getDefault()
Returns the user's preferred time zone. This may have been overridden for this process withsetDefault(java.util.TimeZone).Since the user's time zone changes dynamically, avoid caching this value. Instead, use this method to look it up for each use.
-
getDisplayName
public final String getDisplayName()
Equivalent togetDisplayName(false, TimeZone.LONG, Locale.getDefault()). Be wary of the default locale.
-
getDisplayName
public final String getDisplayName(Locale locale)
Equivalent togetDisplayName(false, TimeZone.LONG, locale).
-
getDisplayName
public final String getDisplayName(boolean daylightTime, int style)
Equivalent togetDisplayName(daylightTime, style, Locale.getDefault()). Be wary of the default locale.
-
createGmtOffsetString
public static String createGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis)
Returns a string representation of an offset from UTC.The format is "[GMT](+|-)HH[:]MM". The output is not localized.
- Parameters:
includeGmt- true to include "GMT", false to excludeincludeMinuteSeparator- true to include the separator between hours and minutes, false to exclude.offsetMillis- the offset from UTC
-
getID
public String getID()
Returns the ID of thisTimeZone, such asAmerica/Los_Angeles,GMT-08:00orUTC.
-
getDSTSavings
public int getDSTSavings()
Returns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned bygetRawOffset()).This class returns
3600000(1 hour) for time zones that use daylight savings time and0for timezones that do not, leaving it to subclasses to override this method for other daylight savings offsets. (There are time zones, such asAustralia/Lord_Howe, that use other values.)Note that this method doesn't tell you whether or not to apply the offset: you need to call
inDaylightTimefor the specific time you're interested in. If this method returns a non-zero offset, that only tells you that thisTimeZonesometimes observes daylight savings.Note also that this method doesn't necessarily return the value you need to apply to the time you're working with. This value can and does change over time for a given time zone.
It's highly unlikely that you should ever call this method. You probably want
getOffset(long)instead, which tells you the offset for a specific point in time, and takes daylight savings into account for you.
-
getOffset
public int getOffset(long time)
Returns the offset in milliseconds from UTC for this time zone attime. The offset includes daylight savings time if the specified date is within the daylight savings time period.- Parameters:
time- the date in milliseconds since January 1, 1970 00:00:00 UTC
-
getOffset
public abstract int getOffset(int era, int year, int month, int day, int dayOfWeek, int timeOfDayMillis)Returns this time zone's offset in milliseconds from UTC at the specified date and time. The offset includes daylight savings time if the date and time is within the daylight savings time period.This method is intended to be used by
Calendarto computeCalendar.DST_OFFSETandCalendar.ZONE_OFFSET. Application code should have no reason to call this method directly. Each parameter is interpreted in the same way as the correspondingCalendarfield. Refer toCalendarfor specific definitions of this method's parameters.
-
getRawOffset
public abstract int getRawOffset()
Returns the offset in milliseconds from UTC of this time zone's standard time.
-
getTimeZone
public static TimeZone getTimeZone(String id)
Returns aTimeZonecorresponding to the givenid, orGMTfor unknown ids.An ID can be an Olson name of the form Area/Location, such as
America/Los_Angeles. ThegetAvailableIDs()method returns the supported names.This method can also create a custom
TimeZonegiven an ID with the following syntax:GMT[+|-]hh[[:]mm]. For example,"GMT+05:00","GMT+0500","GMT+5:00","GMT+500","GMT+05", and"GMT+5"all return an object with a raw offset of +5 hours from UTC, and which does not use daylight savings. These are rarely useful, because they don't correspond to time zones actually in use by humans.Other than the special cases "UTC" and "GMT" (which are synonymous in this context, both corresponding to UTC), Android does not support the deprecated three-letter time zone IDs used in Java 1.1.
-
hasSameRules
public boolean hasSameRules(TimeZone timeZone)
Returns true iftimeZonehas the same rules as this time zone.The base implementation returns true if both time zones have the same raw offset.
-
inDaylightTime
public abstract boolean inDaylightTime(Date time)
Returns true iftimeis in a daylight savings time period for this time zone.
-
setDefault
public static void setDefault(TimeZone timeZone)
Overrides the default time zone for the current process only.Warning: avoid using this method to use a custom time zone in your process. This value may be cleared or overwritten at any time, which can cause unexpected behavior. Instead, manually supply a custom time zone as needed.
- Parameters:
timeZone- a custom time zone, ornullto set the default to the user's preferred value.
-
setID
public void setID(String id)
Sets the ID of thisTimeZone.
-
setRawOffset
public abstract void setRawOffset(int offsetMillis)
Sets the offset in milliseconds from UTC of this time zone's standard time.
-
useDaylightTime
public abstract boolean useDaylightTime()
Returns true if this time zone has a future transition to or from daylight savings time.Warning: this returns false for time zones like
Asia/Kuala_Lumpurthat have previously used DST but do not currently. A hypothetical country that has never observed daylight savings before but plans to start next year would return true.Warning: this returns true for time zones that use DST, even when it is not active.
Use
inDaylightTime(java.util.Date)to find out whether daylight savings is in effect at a specific time.Most applications should not use this method.
-
-