java.util

Class TimeZone

Implemented Interfaces:
Cloneable, Serializable
Known Direct Subclasses:
SimpleTimeZone

public abstract class TimeZone
extends Object
implements Serializable, Cloneable

This class represents a time zone offset and handles daylight savings. You can get the default time zone with getDefault. This represents the time zone where program is running. Another way to create a time zone is getTimeZone, where you can give an identifier as parameter. For instance, the identifier of the Central European Time zone is "CET". With the getAvailableIDs method, you can get all the supported time zone identifiers.
See Also:
Calendar, SimpleTimeZone, Serialized Form

Field Summary

static int
LONG
Constant used to indicate that a long timezone name should be returned, such as "Eastern Standard Time".
static int
SHORT
Constant used to indicate that a short timezone abbreviation should be returned, such as "EST"

Method Summary

Object
clone()
Returns a clone of this object.
static String[]
getAvailableIDs()
Gets all available IDs.
static String[]
getAvailableIDs(int rawOffset)
Gets the available IDs according to the given time zone offset.
int
getDSTSavings()
Gets the daylight savings offset.
static TimeZone
getDefault()
Returns the time zone under which the host is running.
String
getDisplayName()
This method returns a string name of the time zone suitable for displaying to the user.
String
getDisplayName(boolean dst, int style)
This method returns a string name of the time zone suitable for displaying to the user.
String
getDisplayName(boolean dst, int style, Locale locale)
This method returns a string name of the time zone suitable for displaying to the user.
String
getDisplayName(Locale locale)
This method returns a string name of the time zone suitable for displaying to the user.
String
getID()
Gets the identifier of this time zone.
abstract int
getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds)
Gets the time zone offset, for current date, modified in case of daylight savings.
int
getOffset(long date)
Get the time zone offset for the specified date, modified in case of daylight savings.
abstract int
getRawOffset()
Gets the time zone offset, ignoring daylight savings.
static TimeZone
getTimeZone(String ID)
Gets the TimeZone for the given ID.
boolean
hasSameRules(TimeZone other)
Test if the other time zone uses the same rule and only possibly differs in ID.
abstract boolean
inDaylightTime(Date date)
Returns true, if the given date is in Daylight Savings Time in this time zone.
static void
setDefault(TimeZone zone)
void
setID(String id)
Sets the identifier of this time zone.
abstract void
setRawOffset(int offsetMillis)
Sets the time zone offset, ignoring daylight savings.
abstract boolean
useDaylightTime()
Returns true, if this time zone uses Daylight Savings Time.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

LONG

public static final int LONG
Constant used to indicate that a long timezone name should be returned, such as "Eastern Standard Time".
Field Value:
1

SHORT

public static final int SHORT
Constant used to indicate that a short timezone abbreviation should be returned, such as "EST"
Field Value:
0

Method Details

clone

public Object clone()
Returns a clone of this object. I can't imagine, why this is useful for a time zone.
Overrides:
clone in interface Object

getAvailableIDs

public static String[] getAvailableIDs()
Gets all available IDs.
Returns:
An array of all supported IDs.

getAvailableIDs

public static String[] getAvailableIDs(int rawOffset)
Gets the available IDs according to the given time zone offset.
Parameters:
rawOffset - the given time zone GMT offset.
Returns:
An array of IDs, where the time zone has the specified GMT offset. For example {"Phoenix", "Denver"}, since both have GMT-07:00, but differ in daylight savings behaviour.

getDSTSavings

public int getDSTSavings()
Gets the daylight savings offset. This is a positive offset in milliseconds with respect to standard time. Typically this is one hour, but for some time zones this may be half an our.

The default implementation returns 3600000 milliseconds (one hour) if the time zone uses daylight savings time (as specified by useDaylightTime()), otherwise it returns 0.

Returns:
the daylight savings offset in milliseconds.
Since:
1.4

getDefault

public static TimeZone getDefault()
Returns the time zone under which the host is running. This can be changed with setDefault.
Returns:
A clone of the current default time zone for this host.

getDisplayName

public final String getDisplayName()
This method returns a string name of the time zone suitable for displaying to the user. The string returned will be the long description of the timezone in the current locale. The name displayed will assume daylight savings time is not in effect.
Returns:
The name of the time zone.

getDisplayName

public final String getDisplayName(boolean dst,
                                   int style)
This method returns a string name of the time zone suitable for displaying to the user. The string returned will be of the specified type in the current locale.
Parameters:
dst - Whether or not daylight savings time is in effect.
style - LONG for a long name, SHORT for a short abbreviation.
Returns:
The name of the time zone.

getDisplayName

public String getDisplayName(boolean dst,
                             int style,
                             Locale locale)
This method returns a string name of the time zone suitable for displaying to the user. The string returned will be of the specified type in the specified locale.
Parameters:
dst - Whether or not daylight savings time is in effect.
style - LONG for a long name, SHORT for a short abbreviation.
locale - The locale for this timezone name.
Returns:
The name of the time zone.

getDisplayName

public final String getDisplayName(Locale locale)
This method returns a string name of the time zone suitable for displaying to the user. The string returned will be the long description of the timezone in the specified locale. The name displayed will assume daylight savings time is not in effect.
Parameters:
locale - The locale for this timezone name.
Returns:
The name of the time zone.

getID

public String getID()
Gets the identifier of this time zone. For instance, PST for Pacific Standard Time.

getOffset

public abstract int getOffset(int era,
                              int year,
                              int month,
                              int day,
                              int dayOfWeek,
                              int milliseconds)
Gets the time zone offset, for current date, modified in case of daylight savings. This is the offset to add to UTC to get the local time.
Parameters:
era - the era of the given date
year - the year of the given date
month - the month of the given date, 0 for January.
day - the day of month
dayOfWeek - the day of week
milliseconds - the millis in the day (in local standard time)
Returns:
the time zone offset in milliseconds.

getOffset

public int getOffset(long date)
Get the time zone offset for the specified date, modified in case of daylight savings. This is the offset to add to UTC to get the local time.
Parameters:
date - the date represented in millisecends since January 1, 1970 00:00:00 GMT.
Since:
1.4

getRawOffset

public abstract int getRawOffset()
Gets the time zone offset, ignoring daylight savings. This is the offset to add to UTC to get the local time.
Returns:
the time zone offset in milliseconds.

getTimeZone

public static TimeZone getTimeZone(String ID)
Gets the TimeZone for the given ID.
Parameters:
ID - the time zone identifier.
Returns:
The time zone for the identifier or GMT, if no such time zone exists.

hasSameRules

public boolean hasSameRules(TimeZone other)
Test if the other time zone uses the same rule and only possibly differs in ID. This implementation for this particular class will return true if the raw offsets are identical. Subclasses should override this method if they use daylight savings.
Returns:
true if this zone has the same raw offset

inDaylightTime

public abstract boolean inDaylightTime(Date date)
Returns true, if the given date is in Daylight Savings Time in this time zone.
Parameters:
date - the given Date.

setDefault

public static void setDefault(TimeZone zone)

setID

public void setID(String id)
Sets the identifier of this time zone. For instance, PST for Pacific Standard Time.
Parameters:
id - the new time zone ID.
Throws:
NullPointerException - if id is null

setRawOffset

public abstract void setRawOffset(int offsetMillis)
Sets the time zone offset, ignoring daylight savings. This is the offset to add to UTC to get the local time.
Parameters:
offsetMillis - the time zone offset to GMT.

useDaylightTime

public abstract boolean useDaylightTime()
Returns true, if this time zone uses Daylight Savings Time.

java.util.TimeZone Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath 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 for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.