2 *******************************************************************************
\r
3 * Copyright (C) 1996-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import java.io.Serializable;
\r
11 import java.util.Date;
\r
12 import java.util.Locale;
\r
15 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
\r
19 * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
\r
20 * which creates a <code>TimeZone</code> based on the time zone where the program
\r
21 * is running. For example, for a program running in Japan, <code>getDefault</code>
\r
22 * creates a <code>TimeZone</code> object based on Japanese Standard Time.
\r
25 * You can also get a <code>TimeZone</code> using <code>getTimeZone</code>
\r
26 * along with a time zone ID. For instance, the time zone ID for the
\r
27 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a
\r
28 * U.S. Pacific Time <code>TimeZone</code> object with:
\r
31 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
\r
34 * You can use <code>getAvailableIDs</code> method to iterate through
\r
35 * all the supported time zone IDs. You can then choose a
\r
36 * supported ID to get a <code>TimeZone</code>.
\r
37 * If the time zone you want is not represented by one of the
\r
38 * supported IDs, then you can create a custom time zone ID with
\r
39 * the following syntax:
\r
47 * For example, you might specify GMT+14:00 as a custom
\r
48 * time zone ID. The <code>TimeZone</code> that is returned
\r
49 * when you specify a custom time zone ID does not include
\r
50 * daylight savings time.
\r
52 * For compatibility with JDK 1.1.x, some other three-letter time zone IDs
\r
53 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their
\r
54 * use is deprecated</strong> because the same abbreviation is often used
\r
55 * for multiple time zones (for example, "CST" could be U.S. "Central Standard
\r
56 * Time" and "China Standard Time"), and the Java platform can then only
\r
57 * recognize one of them.
\r
60 * @see GregorianCalendar
\r
61 * @see SimpleTimeZone
\r
62 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
\r
65 public class TimeZone implements Serializable, Cloneable {
\r
66 private static final long serialVersionUID = 1;
\r
71 public final java.util.TimeZone timeZone;
\r
75 * @param delegate the TimeZone to which to delegate
\r
77 public TimeZone(java.util.TimeZone delegate) {
\r
78 this.timeZone = delegate;
\r
82 * Default constructor to mirror Java's public
\r
83 * constructor. Java's is not callable as a public API, since
\r
84 * their TimeZone is abstract, so this is only useful to
\r
85 * subclasses. In general, subclasses will not work unless
\r
86 * they manipulate the delegate.
\r
89 this.timeZone = java.util.TimeZone.getDefault();
\r
93 * A style specifier for <code>getDisplayName()</code> indicating
\r
94 * a short name, such as "PST."
\r
98 public static final int SHORT = 0;
\r
101 * A style specifier for <code>getDisplayName()</code> indicating
\r
102 * a long name, such as "Pacific Standard Time."
\r
106 public static final int LONG = 1;
\r
109 * Gets the time zone offset, for current date, modified in case of
\r
110 * daylight savings. This is the offset to add *to* UTC to get local time.
\r
111 * @param era the era of the given date.
\r
112 * @param year the year in the given date.
\r
113 * @param month the month in the given date.
\r
114 * Month is 0-based. e.g., 0 for January.
\r
115 * @param day the day-in-month of the given date.
\r
116 * @param dayOfWeek the day-of-week of the given date.
\r
117 * @param milliseconds the millis in day in <em>standard</em> local time.
\r
118 * @return the offset to add *to* GMT to get local time.
\r
121 public int getOffset(int era, int year, int month, int day,
\r
122 int dayOfWeek, int milliseconds) {
\r
123 return timeZone.getOffset(era, year, month, day, dayOfWeek, milliseconds);
\r
127 * Returns the offset of this time zone from UTC at the specified
\r
128 * date. If Daylight Saving Time is in effect at the specified
\r
129 * date, the offset value is adjusted with the amount of daylight
\r
132 * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
\r
133 * @return the amount of time in milliseconds to add to UTC to get local time.
\r
135 * @see Calendar#ZONE_OFFSET
\r
136 * @see Calendar#DST_OFFSET
\r
137 * @see #getOffset(long, boolean, int[])
\r
140 public int getOffset(long date) {
\r
141 if (inDaylightTime(new Date(date))) {
\r
142 return getRawOffset() + getDSTSavings();
\r
144 return getRawOffset();
\r
148 * Sets the base time zone offset to GMT.
\r
149 * This is the offset to add *to* UTC to get local time.
\r
150 * @param offsetMillis the given base time zone offset to GMT.
\r
153 public void setRawOffset(int offsetMillis) {
\r
154 timeZone.setRawOffset(offsetMillis);
\r
158 * Gets unmodified offset, NOT modified in case of daylight savings.
\r
159 * This is the offset to add *to* UTC to get local time.
\r
160 * @return the unmodified offset to add *to* UTC to get local time.
\r
163 public int getRawOffset() {
\r
164 return timeZone.getRawOffset();
\r
168 * Gets the ID of this time zone.
\r
169 * @return the ID of this time zone.
\r
172 public String getID() {
\r
173 return timeZone.getID();
\r
177 * Sets the time zone ID. This does not change any other data in
\r
178 * the time zone object.
\r
179 * @param ID the new time zone ID.
\r
182 public void setID(String ID) {
\r
183 timeZone.setID(ID);
\r
187 * Returns a name of this time zone suitable for presentation to the user
\r
188 * in the default locale.
\r
189 * This method returns the long generic name.
\r
190 * If the display name is not available for the locale,
\r
191 * a fallback based on the country, city, or time zone id will be used.
\r
192 * @return the human-readable name of this time zone in the default locale.
\r
195 public final String getDisplayName() {
\r
196 return timeZone.getDisplayName();
\r
200 * Returns a name of this time zone suitable for presentation to the user
\r
201 * in the specified locale.
\r
202 * This method returns the long generic name.
\r
203 * If the display name is not available for the locale,
\r
204 * a fallback based on the country, city, or time zone id will be used.
\r
205 * @param locale the locale in which to supply the display name.
\r
206 * @return the human-readable name of this time zone in the given locale
\r
207 * or in the default locale if the given locale is not recognized.
\r
210 public final String getDisplayName(Locale locale) {
\r
211 return timeZone.getDisplayName(locale);
\r
215 * Returns a name of this time zone suitable for presentation to the user
\r
216 * in the specified locale.
\r
217 * This method returns the long name, not including daylight savings.
\r
218 * If the display name is not available for the locale,
\r
219 * a fallback based on the country, city, or time zone id will be used.
\r
220 * @param locale the ulocale in which to supply the display name.
\r
221 * @return the human-readable name of this time zone in the given locale
\r
222 * or in the default ulocale if the given ulocale is not recognized.
\r
225 public final String getDisplayName(ULocale locale) {
\r
226 return timeZone.getDisplayName(locale.toLocale());
\r
230 * Returns a name of this time zone suitable for presentation to the user
\r
231 * in the default locale.
\r
232 * If the display name is not available for the locale,
\r
233 * then this method returns a string in the format
\r
234 * <code>GMT[+-]hh:mm</code>.
\r
235 * @param daylight if true, return the daylight savings name.
\r
236 * @param style either <code>LONG</code> or <code>SHORT</code>
\r
237 * @return the human-readable name of this time zone in the default locale.
\r
240 public final String getDisplayName(boolean daylight, int style) {
\r
241 return timeZone.getDisplayName(daylight, style);
\r
245 * Returns a name of this time zone suitable for presentation to the user
\r
246 * in the specified locale.
\r
247 * If the display name is not available for the locale,
\r
248 * then this method returns a string in the format
\r
249 * <code>GMT[+-]hh:mm</code>.
\r
250 * @param daylight if true, return the daylight savings name.
\r
251 * @param style either <code>LONG</code> or <code>SHORT</code>
\r
252 * @param locale the locale in which to supply the display name.
\r
253 * @return the human-readable name of this time zone in the given locale
\r
254 * or in the default locale if the given locale is not recognized.
\r
255 * @exception IllegalArgumentException style is invalid.
\r
258 public String getDisplayName(boolean daylight, int style, Locale locale) {
\r
259 return timeZone.getDisplayName(daylight, style, locale);
\r
263 * Returns a name of this time zone suitable for presentation to the user
\r
264 * in the specified locale.
\r
265 * If the display name is not available for the locale,
\r
266 * then this method returns a string in the format
\r
267 * <code>GMT[+-]hh:mm</code>.
\r
268 * @param daylight if true, return the daylight savings name.
\r
269 * @param style either <code>LONG</code> or <code>SHORT</code>
\r
270 * @param locale the locale in which to supply the display name.
\r
271 * @return the human-readable name of this time zone in the given locale
\r
272 * or in the default locale if the given locale is not recognized.
\r
273 * @exception IllegalArgumentException style is invalid.
\r
276 public String getDisplayName(boolean daylight, int style, ULocale locale) {
\r
277 return timeZone.getDisplayName(daylight, style, locale.toLocale());
\r
281 * Returns the amount of time to be added to local standard time
\r
282 * to get local wall clock time.
\r
284 * The default implementation always returns 3600000 milliseconds
\r
285 * (i.e., one hour) if this time zone observes Daylight Saving
\r
286 * Time. Otherwise, 0 (zero) is returned.
\r
288 * If an underlying TimeZone implementation subclass supports
\r
289 * historical Daylight Saving Time changes, this method returns
\r
290 * the known latest daylight saving value.
\r
292 * @return the amount of saving time in milliseconds
\r
295 public int getDSTSavings() {
\r
296 if (useDaylightTime()) {
\r
303 * Queries if this time zone uses daylight savings time.
\r
304 * @return true if this time zone uses daylight savings time,
\r
305 * false, otherwise.
\r
308 public boolean useDaylightTime() {
\r
309 return timeZone.useDaylightTime();
\r
313 * Queries if the given date is in daylight savings time in
\r
315 * @param date the given Date.
\r
316 * @return true if the given date is in daylight savings time,
\r
317 * false, otherwise.
\r
320 public boolean inDaylightTime(Date date) {
\r
321 return timeZone.inDaylightTime(date);
\r
325 * Gets the <code>TimeZone</code> for the given ID.
\r
327 * @param ID the ID for a <code>TimeZone</code>, either an abbreviation
\r
328 * such as "PST", a full name such as "America/Los_Angeles", or a custom
\r
329 * ID such as "GMT-8:00". Note that the support of abbreviations is
\r
330 * for JDK 1.1.x compatibility only and full names should be used.
\r
332 * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
\r
333 * cannot be understood.
\r
336 public static TimeZone getTimeZone(String ID) {
\r
337 return new TimeZone(java.util.TimeZone.getTimeZone(ID));
\r
341 * Return a new String array containing all system TimeZone IDs
\r
342 * with the given raw offset from GMT. These IDs may be passed to
\r
343 * <code>get()</code> to construct the corresponding TimeZone
\r
345 * @param rawOffset the offset in milliseconds from GMT
\r
346 * @return an array of IDs for system TimeZones with the given
\r
347 * raw offset. If there are none, return a zero-length array.
\r
350 public static String[] getAvailableIDs(int rawOffset) {
\r
351 return java.util.TimeZone.getAvailableIDs(rawOffset);
\r
355 * Return a new String array containing all system TimeZone IDs.
\r
356 * These IDs (and only these IDs) may be passed to
\r
357 * <code>get()</code> to construct the corresponding TimeZone
\r
359 * @return an array of all system TimeZone IDs
\r
362 public static String[] getAvailableIDs() {
\r
363 return java.util.TimeZone.getAvailableIDs();
\r
367 * Gets the default <code>TimeZone</code> for this host.
\r
368 * The source of the default <code>TimeZone</code>
\r
369 * may vary with implementation.
\r
370 * @return a default <code>TimeZone</code>.
\r
373 public static TimeZone getDefault() {
\r
374 return new TimeZone(java.util.TimeZone.getDefault());
\r
378 * Sets the <code>TimeZone</code> that is
\r
379 * returned by the <code>getDefault</code> method. If <code>zone</code>
\r
380 * is null, reset the default to the value it had originally when the
\r
381 * VM first started.
\r
382 * @param tz the new default time zone
\r
385 public static void setDefault(TimeZone tz) {
\r
386 java.util.TimeZone.setDefault(tz.timeZone);
\r
390 * Returns true if this zone has the same rule and offset as another zone.
\r
391 * That is, if this zone differs only in ID, if at all. Returns false
\r
392 * if the other zone is null.
\r
393 * @param other the <code>TimeZone</code> object to be compared with
\r
394 * @return true if the other zone is not null and is the same as this one,
\r
395 * with the possible exception of the ID
\r
398 public boolean hasSameRules(TimeZone other) {
\r
399 return timeZone.hasSameRules(other.timeZone);
\r
403 * Overrides Cloneable
\r
406 public Object clone() {
\r
407 return new TimeZone((java.util.TimeZone)timeZone.clone());
\r
411 * Return true if rhs is a TimeZone and has the same time rules as this.
\r
412 * @return true if rhs equals this
\r
413 * @stable ICU 3.4.2
\r
415 public boolean equals(Object rhs){
\r
417 return timeZone.equals(((TimeZone)rhs).timeZone);
\r
419 catch (Exception e) {
\r
425 * Return a hashCode.
\r
426 * @return a hashCode
\r
427 * @stable ICU 3.4.2
\r
429 public int hashCode(){
\r
430 return timeZone.hashCode();
\r