THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

24.4.2. Time Zones


TimeZone is an abstract class that encapsulates not only offset from GMT but also other offset issues, such as
daylight saving time. As with other locale-sensitive classes, you can get the default TimeZone by invoking
the static method geTDefault. You can change the default time zone by passing setDefault a new
TimeZone object to useor null to reset to the original default time zone. Time zones are understood by
particular calendar types, so you should ensure that the default calendar and time zone are compatible.


Each time zone has a string identifier that is interpreted by the time zone object and can be displayed to the
user. These identifiers use a long form consisting of a major and minor regional name, separated by '/'. For
example, the following are all valid time zone identifiers: America/New_York,
Australia/Brisbane, Africa/Timbuktu. Many time zones have a short form identifier often just a
three letter acronymsome of which are recognized by TimeZone for backward compatibility. You should
endeavor to always use the long formafter all, while many people know that EST stands for "Eastern Standard
Time," that doesn't tell you for which country. TimeZone also recognizes generic identifiers expressed as the
difference in time from GMT. For example, GMT+10:00 and GMT-4:00 are both valid generic time zone
identifiers. You can get an array of all the identifiers available on your system from the static method
getAvailableIDs. If you want only those for a given offset from GMT, you can invoke
getAvailableIDs with that offset. An offset might, for example, have identifiers for both daylight saving
and standard time zones.


You can find the identifier of a given TimeZone object from getID, and you can set it with setID. Setting
the identifier changes only the identifier on the time zoneit does not change the offset or other values. You can
get the time zone for a given identifier by passing it to the static method getTimeZone.


A time zone can be converted into a displayable form by using one of the getdisplayName methods,
similar to those of Locale. These methods allow you to specify whether to use the default locale or a
specified one, and whether to use a short or long format. The string returned by the display methods is
controlled by a DateFormat object (which you'll see a little later). These objects maintain their own tables
of information on how to format different time zones. On a given system they may not maintain information
for all the supported time zones, in which case the generic identifier form is used, such as in the example on
page 696.


Each time zone has a raw offset from GMT, which can be either positive or negative. You can get or set the
raw offset by using getrawOffset or set RawOffset, but you should rarely need to do this.


Daylight saving time supplements the raw offset with a seasonal time shift. The value of this shift can be
obtained from getdSTSavingsthe default implementation returns 3,600,000 (the number of milliseconds in
an hour). You can ask whether a time zone ever uses daylight saving time during the year by invoking the
method useDaylightTime, which returns a boolean. The method inDaylightTime returns true if
the Date argument you pass would fall inside daylight saving time in the zone.


You can obtain the exact offset for a time zone on a given date by specifying that date in milliseconds or by
using calendar fields to specify the year and month and so on.


public intgetOffset(long date)

Returns the offset from GMT for the given time in this time zone, taking any
daylight saving time offset into account

public abstract intgetOffset(int era, int year, int month, int
day, int dayOfWeek, int milliseconds)

Returns the offset from GMT for the given time in this time zone, taking any
Free download pdf