Android APIs
public abstract class

TimeZone

extends Object
implements Serializable Cloneable
java.lang.Object
   ↳ java.util.TimeZone
Known Direct Subclasses

Class Overview

TimeZone represents a time zone, primarily used for configuring a Calendar or SimpleDateFormat instance.

Most applications will use getDefault() which returns a TimeZone based on the time zone where the program is running.

You can also get a specific TimeZone by Olson ID.

It is highly unlikely you'll ever want to use anything but the factory methods yourself. Let classes like Calendar and SimpleDateFormat do 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, and inDaylightTime(Date) must be used to determine whether or not daylight time applies to a given Date. The getRawOffset() method will return a raw offset of (in this case) -8 hours from UTC, which isn't usually very useful. More usefully, the getOffset(int, int, int, int, int, int) methods return the actual offset from UTC for a given point in time; this is the raw offset plus (if the point in time is in daylight time) the applicable DST savings (usually, but not necessarily, 1 hour).
  • Somewhere like Japan, where daylight time is not used. The useDaylightTime() and inDaylightTime(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() and getTimeZone(String) is implementation dependent. This may introduce serialization incompatibility issues between different implementations, or different versions of Android.

Summary

Constants
int LONG The long display name style, such as Pacific Daylight Time.
int SHORT The short display name style, such as PDT.
Public Constructors
TimeZone()
Public Methods
Object clone()
Returns a new time zone with the same ID, raw offset, and daylight savings time rules as this time zone.
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 is offsetMillis.
int getDSTSavings()
Returns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned by getRawOffset()).
static TimeZone getDefault()
Returns the user's preferred time zone.
final String getDisplayName(Locale locale)
Equivalent to getDisplayName(false, TimeZone.LONG, locale).
String getDisplayName(boolean daylightTime, int style, Locale locale)
Returns the short or long name of this time zone with either standard or daylight time, as written in locale.
final String getDisplayName()
Equivalent to getDisplayName(false, TimeZone.LONG, Locale.getDefault()).
final String getDisplayName(boolean daylightTime, int style)
Equivalent to getDisplayName(daylightTime, style, Locale.getDefault()).
String getID()
Returns the ID of this TimeZone, such as America/Los_Angeles, GMT-08:00 or UTC.
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.
int getOffset(long time)
Returns the offset in milliseconds from UTC for this time zone at time.
abstract int getRawOffset()
Returns the offset in milliseconds from UTC of this time zone's standard time.
static TimeZone getTimeZone(String id)
Returns a TimeZone corresponding to the given id, or GMT for unknown ids.
boolean hasSameRules(TimeZone timeZone)
Returns true if timeZone has the same rules as this time zone.
abstract boolean inDaylightTime(Date time)
Returns true if time is in a daylight savings time period for this time zone.
static void setDefault(TimeZone timeZone)
Overrides the default time zone for the current process only.
void setID(String id)
Sets the ID of this TimeZone.
abstract void setRawOffset(int offsetMillis)
Sets the offset in milliseconds from UTC of this time zone's standard time.
abstract boolean useDaylightTime()
Returns true if this time zone has a future transition to or from daylight savings time.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int LONG

Added in API level 1

The long display name style, such as Pacific Daylight Time. Requests for this style may yield GMT offsets like GMT-08:00.

Constant Value: 1 (0x00000001)

public static final int SHORT

Added in API level 1

The short display name style, such as PDT. Requests for this style may yield GMT offsets like GMT-08:00.

Constant Value: 0 (0x00000000)

Public Constructors

public TimeZone ()

Added in API level 1

Public Methods

public Object clone ()

Added in API level 1

Returns a new time zone with the same ID, raw offset, and daylight savings time rules as this time zone.

Returns
Object a copy of this object.

public static String[] getAvailableIDs ()

Added in API level 1

Returns the system's installed time zone IDs. Any of these IDs can be passed to getTimeZone(String) to lookup the corresponding time zone instance.

Returns
String[]

public static String[] getAvailableIDs (int offsetMillis)

Added in API level 1

Returns the IDs of the time zones whose offset from UTC is offsetMillis. Any of these IDs can be passed to getTimeZone(String) to lookup the corresponding time zone instance.

Parameters
offsetMillis int
Returns
String[] a possibly-empty array.

public int getDSTSavings ()

Added in API level 1

Returns the latest daylight savings in milliseconds for this time zone, relative to this time zone's regular UTC offset (as returned by getRawOffset()).

This class returns 3600000 (1 hour) for time zones that use daylight savings time and 0 for timezones that do not, leaving it to subclasses to override this method for other daylight savings offsets. (There are time zones, such as Australia/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 inDaylightTime for the specific time you're interested in. If this method returns a non-zero offset, that only tells you that this TimeZone sometimes 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(int, int, int, int, int, int) instead, which tells you the offset for a specific point in time, and takes daylight savings into account for you.

Returns
int

public static TimeZone getDefault ()

Added in API level 1

Returns the user's preferred time zone. This may have been overridden for this process with setDefault(TimeZone).

Since the user's time zone changes dynamically, avoid caching this value. Instead, use this method to look it up for each use.

Returns
TimeZone

public final String getDisplayName (Locale locale)

Added in API level 1

Equivalent to getDisplayName(false, TimeZone.LONG, locale).

Parameters
locale Locale
Returns
String

public String getDisplayName (boolean daylightTime, int style, Locale locale)

Added in API level 1

Returns the short or long name of this time zone with either standard or daylight time, as written in locale. If the name is not available, the result is in the format GMT[+-]hh:mm.

Parameters
daylightTime boolean: true for daylight time, false for standard time.
style int: either LONG or SHORT.
locale Locale: the display locale.
Returns
String

public final String getDisplayName ()

Added in API level 1

Equivalent to getDisplayName(false, TimeZone.LONG, Locale.getDefault()). Be wary of the default locale.

Returns
String

public final String getDisplayName (boolean daylightTime, int style)

Added in API level 1

Equivalent to getDisplayName(daylightTime, style, Locale.getDefault()). Be wary of the default locale.

Parameters
daylightTime boolean
style int
Returns
String

public String getID ()

Added in API level 1

Returns the ID of this TimeZone, such as America/Los_Angeles, GMT-08:00 or UTC.

Returns
String

public abstract int getOffset (int era, int year, int month, int day, int dayOfWeek, int timeOfDayMillis)

Added in API level 1

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 Calendar to compute DST_OFFSET and ZONE_OFFSET. Application code should have no reason to call this method directly. Each parameter is interpreted in the same way as the corresponding Calendar field. Refer to Calendar for specific definitions of this method's parameters.

Parameters
era int
year int
month int
day int
dayOfWeek int
timeOfDayMillis int
Returns
int

public int getOffset (long time)

Added in API level 1

Returns the offset in milliseconds from UTC for this time zone at time. The offset includes daylight savings time if the specified date is within the daylight savings time period.

Parameters
time long: the date in milliseconds since January 1, 1970 00:00:00 UTC
Returns
int

public abstract int getRawOffset ()

Added in API level 1

Returns the offset in milliseconds from UTC of this time zone's standard time.

Returns
int

public static TimeZone getTimeZone (String id)

Added in API level 1

Returns a TimeZone corresponding to the given id, or GMT for unknown ids.

An ID can be an Olson name of the form Area/Location, such as America/Los_Angeles. The getAvailableIDs() method returns the supported names.

This method can also create a custom TimeZone given 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.

Parameters
id String
Returns
TimeZone

public boolean hasSameRules (TimeZone timeZone)

Added in API level 1

Returns true if timeZone has the same rules as this time zone.

The base implementation returns true if both time zones have the same raw offset.

Parameters
timeZone TimeZone
Returns
boolean

public abstract boolean inDaylightTime (Date time)

Added in API level 1

Returns true if time is in a daylight savings time period for this time zone.

Parameters
time Date
Returns
boolean

public static void setDefault (TimeZone timeZone)

Added in API level 1

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 TimeZone: a custom time zone, or null to set the default to the user's preferred value.

public void setID (String id)

Added in API level 1

Sets the ID of this TimeZone.

Parameters
id String

public abstract void setRawOffset (int offsetMillis)

Added in API level 1

Sets the offset in milliseconds from UTC of this time zone's standard time.

Parameters
offsetMillis int

public abstract boolean useDaylightTime ()

Added in API level 1

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_Lumpur that 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(Date) to find out whether daylight savings is in effect at a specific time.

Most applications should not use this method.

Returns
boolean