2004-09-25 Mark Wielaard <mark@klomp.org>
[official-gcc.git] / libjava / java / util / TimeZone.java
blob1e06e535611bc12ec2ce1b6ec036c90d39ffbbdd
1 /* java.util.TimeZone
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.util;
42 import java.security.AccessController;
43 import java.security.PrivilegedAction;
44 import java.text.DateFormatSymbols;
46 /**
47 * This class represents a time zone offset and handles daylight savings.
49 * You can get the default time zone with <code>getDefault</code>.
50 * This represents the time zone where program is running.
52 * Another way to create a time zone is <code>getTimeZone</code>, where
53 * you can give an identifier as parameter. For instance, the identifier
54 * of the Central European Time zone is "CET".
56 * With the <code>getAvailableIDs</code> method, you can get all the
57 * supported time zone identifiers.
59 * @see Calendar
60 * @see SimpleTimeZone
61 * @author Jochen Hoenicke
63 public abstract class TimeZone implements java.io.Serializable, Cloneable
66 /**
67 * Constant used to indicate that a short timezone abbreviation should
68 * be returned, such as "EST"
70 public static final int SHORT = 0;
72 /**
73 * Constant used to indicate that a long timezone name should be
74 * returned, such as "Eastern Standard Time".
76 public static final int LONG = 1;
78 /**
79 * The time zone identifier, e.g. PST.
81 private String ID;
83 /**
84 * The default time zone, as returned by getDefault.
86 private static TimeZone defaultZone0;
88 /**
89 * Tries to get the default TimeZone for this system if not already
90 * set. It will call <code>getDefaultTimeZone(String)</code> with
91 * the result of <code>System.getProperty("user.timezone")</code>.
92 * If that fails it calls <code>VMTimeZone.getDefaultTimeZoneId()</code>.
93 * If that also fails GMT is returned.
95 private static synchronized TimeZone defaultZone()
97 /* Look up default timezone */
98 if (defaultZone0 == null)
100 defaultZone0 = (TimeZone) AccessController.doPrivileged
101 (new PrivilegedAction()
103 public Object run()
105 TimeZone zone = null;
107 // Prefer System property user.timezone.
108 String tzid = System.getProperty("user.timezone");
109 if (tzid != null && !tzid.equals(""))
110 zone = getDefaultTimeZone(tzid);
112 // Try platfom specific way.
113 if (zone == null)
114 zone = VMTimeZone.getDefaultTimeZoneId();
116 // Fall back on GMT.
117 if (zone == null)
118 zone = (TimeZone) timezones().get("GMT");
120 return zone;
125 return defaultZone0;
128 private static final long serialVersionUID = 3581463369166924961L;
131 * HashMap for timezones by ID.
133 private static HashMap timezones0;
134 /* initialize this static field lazily to overhead if
135 * it is not needed:
137 private static synchronized HashMap timezones()
139 if (timezones0 == null)
141 HashMap timezones = new HashMap();
142 timezones0 = timezones;
144 TimeZone tz;
145 // Automatically generated by scripts/timezones.pl
146 // XXX - Should we read this data from a file?
147 tz = new SimpleTimeZone(-11000 * 3600, "MIT");
148 timezones0.put("MIT", tz);
149 timezones0.put("Pacific/Apia", tz);
150 timezones0.put("Pacific/Midway", tz);
151 timezones0.put("Pacific/Niue", tz);
152 timezones0.put("Pacific/Pago_Pago", tz);
153 tz = new SimpleTimeZone
154 (-10000 * 3600, "America/Adak",
155 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
156 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
157 timezones0.put("America/Adak", tz);
158 tz = new SimpleTimeZone(-10000 * 3600, "HST");
159 timezones0.put("HST", tz);
160 timezones0.put("Pacific/Fakaofo", tz);
161 timezones0.put("Pacific/Honolulu", tz);
162 timezones0.put("Pacific/Johnston", tz);
163 timezones0.put("Pacific/Rarotonga", tz);
164 timezones0.put("Pacific/Tahiti", tz);
165 tz = new SimpleTimeZone(-9500 * 3600, "Pacific/Marquesas");
166 timezones0.put("Pacific/Marquesas", tz);
167 tz = new SimpleTimeZone
168 (-9000 * 3600, "AST",
169 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
170 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
171 timezones0.put("AST", tz);
172 timezones0.put("America/Anchorage", tz);
173 timezones0.put("America/Juneau", tz);
174 timezones0.put("America/Nome", tz);
175 timezones0.put("America/Yakutat", tz);
176 tz = new SimpleTimeZone(-9000 * 3600, "Pacific/Gambier");
177 timezones0.put("Pacific/Gambier", tz);
178 tz = new SimpleTimeZone
179 (-8000 * 3600, "PST",
180 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
181 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
182 timezones0.put("PST", tz);
183 timezones0.put("PST8PDT", tz);
184 timezones0.put("America/Dawson", tz);
185 timezones0.put("America/Los_Angeles", tz);
186 timezones0.put("America/Tijuana", tz);
187 timezones0.put("America/Vancouver", tz);
188 timezones0.put("America/Whitehorse", tz);
189 timezones0.put("US/Pacific-New", tz);
190 tz = new SimpleTimeZone(-8000 * 3600, "Pacific/Pitcairn");
191 timezones0.put("Pacific/Pitcairn", tz);
192 tz = new SimpleTimeZone
193 (-7000 * 3600, "MST",
194 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
195 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
196 timezones0.put("MST", tz);
197 timezones0.put("MST7MDT", tz);
198 timezones0.put("America/Boise", tz);
199 timezones0.put("America/Chihuahua", tz);
200 timezones0.put("America/Denver", tz);
201 timezones0.put("America/Edmonton", tz);
202 timezones0.put("America/Inuvik", tz);
203 timezones0.put("America/Mazatlan", tz);
204 timezones0.put("America/Shiprock", tz);
205 timezones0.put("America/Yellowknife", tz);
206 tz = new SimpleTimeZone(-7000 * 3600, "MST7");
207 timezones0.put("MST7", tz);
208 timezones0.put("PNT", tz);
209 timezones0.put("America/Dawson_Creek", tz);
210 timezones0.put("America/Hermosillo", tz);
211 timezones0.put("America/Phoenix", tz);
212 tz = new SimpleTimeZone
213 (-6000 * 3600, "CST",
214 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
215 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
216 timezones0.put("CST", tz);
217 timezones0.put("CST6CDT", tz);
218 timezones0.put("America/Cambridge_Bay", tz);
219 timezones0.put("America/Cancun", tz);
220 timezones0.put("America/Chicago", tz);
221 timezones0.put("America/Menominee", tz);
222 timezones0.put("America/Merida", tz);
223 timezones0.put("America/Mexico_City", tz);
224 timezones0.put("America/Monterrey", tz);
225 timezones0.put("America/Rainy_River", tz);
226 timezones0.put("America/Winnipeg", tz);
227 tz = new SimpleTimeZone(-6000 * 3600, "America/Belize");
228 timezones0.put("America/Belize", tz);
229 timezones0.put("America/Costa_Rica", tz);
230 timezones0.put("America/El_Salvador", tz);
231 timezones0.put("America/Guatemala", tz);
232 timezones0.put("America/Managua", tz);
233 timezones0.put("America/Regina", tz);
234 timezones0.put("America/Swift_Current", tz);
235 timezones0.put("America/Tegucigalpa", tz);
236 timezones0.put("Pacific/Galapagos", tz);
237 tz = new SimpleTimeZone
238 (-6000 * 3600, "Pacific/Easter",
239 Calendar.OCTOBER, 9, -Calendar.SUNDAY, 0 * 3600,
240 Calendar.MARCH, 9, -Calendar.SUNDAY, 0 * 3600);
241 timezones0.put("Pacific/Easter", tz);
242 tz = new SimpleTimeZone
243 (-5000 * 3600, "America/Grand_Turk",
244 Calendar.APRIL, 1, Calendar.SUNDAY, 0 * 3600,
245 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
246 timezones0.put("America/Grand_Turk", tz);
247 timezones0.put("America/Havana", tz);
248 tz = new SimpleTimeZone(-5000 * 3600, "EST5");
249 timezones0.put("EST5", tz);
250 timezones0.put("IET", tz);
251 timezones0.put("America/Bogota", tz);
252 timezones0.put("America/Cayman", tz);
253 timezones0.put("America/Eirunepe", tz);
254 timezones0.put("America/Guayaquil", tz);
255 timezones0.put("America/Indiana/Indianapolis", tz);
256 timezones0.put("America/Indiana/Knox", tz);
257 timezones0.put("America/Indiana/Marengo", tz);
258 timezones0.put("America/Indiana/Vevay", tz);
259 timezones0.put("America/Indianapolis", tz);
260 timezones0.put("America/Iqaluit", tz);
261 timezones0.put("America/Jamaica", tz);
262 timezones0.put("America/Lima", tz);
263 timezones0.put("America/Panama", tz);
264 timezones0.put("America/Pangnirtung", tz);
265 timezones0.put("America/Port-au-Prince", tz);
266 timezones0.put("America/Porto_Acre", tz);
267 timezones0.put("America/Rankin_Inlet", tz);
268 tz = new SimpleTimeZone
269 (-5000 * 3600, "EST",
270 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
271 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
272 timezones0.put("EST", tz);
273 timezones0.put("EST5EDT", tz);
274 timezones0.put("America/Detroit", tz);
275 timezones0.put("America/Kentucky/Louisville", tz);
276 timezones0.put("America/Kentucky/Monticello", tz);
277 timezones0.put("America/Louisville", tz);
278 timezones0.put("America/Montreal", tz);
279 timezones0.put("America/Nassau", tz);
280 timezones0.put("America/New_York", tz);
281 timezones0.put("America/Nipigon", tz);
282 timezones0.put("America/Thunder_Bay", tz);
283 tz = new SimpleTimeZone(-4000 * 3600, "PRT");
284 timezones0.put("PRT", tz);
285 timezones0.put("America/Anguilla", tz);
286 timezones0.put("America/Antigua", tz);
287 timezones0.put("America/Aruba", tz);
288 timezones0.put("America/Barbados", tz);
289 timezones0.put("America/Boa_Vista", tz);
290 timezones0.put("America/Caracas", tz);
291 timezones0.put("America/Curacao", tz);
292 timezones0.put("America/Dominica", tz);
293 timezones0.put("America/Grenada", tz);
294 timezones0.put("America/Guadeloupe", tz);
295 timezones0.put("America/Guyana", tz);
296 timezones0.put("America/La_Paz", tz);
297 timezones0.put("America/Manaus", tz);
298 timezones0.put("America/Martinique", tz);
299 timezones0.put("America/Montserrat", tz);
300 timezones0.put("America/Port_of_Spain", tz);
301 timezones0.put("America/Porto_Velho", tz);
302 timezones0.put("America/Puerto_Rico", tz);
303 timezones0.put("America/Santo_Domingo", tz);
304 timezones0.put("America/St_Kitts", tz);
305 timezones0.put("America/St_Lucia", tz);
306 timezones0.put("America/St_Thomas", tz);
307 timezones0.put("America/St_Vincent", tz);
308 timezones0.put("America/Tortola", tz);
309 tz = new SimpleTimeZone
310 (-4000 * 3600, "America/Asuncion",
311 Calendar.OCTOBER, 1, Calendar.SUNDAY, 0 * 3600,
312 Calendar.FEBRUARY, -1, Calendar.SUNDAY, 0 * 3600);
313 timezones0.put("America/Asuncion", tz);
314 tz = new SimpleTimeZone
315 (-4000 * 3600, "America/Cuiaba",
316 Calendar.OCTOBER, 2, Calendar.SUNDAY, 0 * 3600,
317 Calendar.FEBRUARY, 3, Calendar.SUNDAY, 0 * 3600);
318 timezones0.put("America/Cuiaba", tz);
319 tz = new SimpleTimeZone
320 (-4000 * 3600, "America/Goose_Bay",
321 Calendar.APRIL, 1, Calendar.SUNDAY, 60000,
322 Calendar.OCTOBER, -1, Calendar.SUNDAY, 60000);
323 timezones0.put("America/Goose_Bay", tz);
324 tz = new SimpleTimeZone
325 (-4000 * 3600, "America/Glace_Bay",
326 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
327 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
328 timezones0.put("America/Glace_Bay", tz);
329 timezones0.put("America/Halifax", tz);
330 timezones0.put("America/Thule", tz);
331 timezones0.put("Atlantic/Bermuda", tz);
332 tz = new SimpleTimeZone
333 (-4000 * 3600, "America/Santiago",
334 Calendar.OCTOBER, 9, -Calendar.SUNDAY, 0 * 3600,
335 Calendar.MARCH, 9, -Calendar.SUNDAY, 0 * 3600);
336 timezones0.put("America/Santiago", tz);
337 timezones0.put("Antarctica/Palmer", tz);
338 tz = new SimpleTimeZone
339 (-4000 * 3600, "Atlantic/Stanley",
340 Calendar.SEPTEMBER, 2, Calendar.SUNDAY, 0 * 3600,
341 Calendar.APRIL, 16, -Calendar.SUNDAY, 0 * 3600);
342 timezones0.put("Atlantic/Stanley", tz);
343 tz = new SimpleTimeZone
344 (-3500 * 3600, "CNT",
345 Calendar.APRIL, 1, Calendar.SUNDAY, 60000,
346 Calendar.OCTOBER, -1, Calendar.SUNDAY, 60000);
347 timezones0.put("CNT", tz);
348 timezones0.put("America/St_Johns", tz);
349 tz = new SimpleTimeZone
350 (-3000 * 3600, "America/Araguaina",
351 Calendar.OCTOBER, 2, Calendar.SUNDAY, 0 * 3600,
352 Calendar.FEBRUARY, 3, Calendar.SUNDAY, 0 * 3600);
353 timezones0.put("America/Araguaina", tz);
354 timezones0.put("America/Sao_Paulo", tz);
355 tz = new SimpleTimeZone(-3000 * 3600, "AGT");
356 timezones0.put("AGT", tz);
357 timezones0.put("America/Belem", tz);
358 timezones0.put("America/Buenos_Aires", tz);
359 timezones0.put("America/Catamarca", tz);
360 timezones0.put("America/Cayenne", tz);
361 timezones0.put("America/Cordoba", tz);
362 timezones0.put("America/Fortaleza", tz);
363 timezones0.put("America/Jujuy", tz);
364 timezones0.put("America/Maceio", tz);
365 timezones0.put("America/Mendoza", tz);
366 timezones0.put("America/Montevideo", tz);
367 timezones0.put("America/Paramaribo", tz);
368 timezones0.put("America/Recife", tz);
369 timezones0.put("America/Rosario", tz);
370 tz = new SimpleTimeZone
371 (-3000 * 3600, "America/Godthab",
372 Calendar.MARCH, 30, -Calendar.SATURDAY, 22000 * 3600,
373 Calendar.OCTOBER, 30, -Calendar.SATURDAY, 22000 * 3600);
374 timezones0.put("America/Godthab", tz);
375 tz = new SimpleTimeZone
376 (-3000 * 3600, "America/Miquelon",
377 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600,
378 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
379 timezones0.put("America/Miquelon", tz);
380 tz = new SimpleTimeZone(-2000 * 3600, "America/Noronha");
381 timezones0.put("America/Noronha", tz);
382 timezones0.put("Atlantic/South_Georgia", tz);
383 tz = new SimpleTimeZone
384 (-1000 * 3600, "America/Scoresbysund",
385 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * 3600,
386 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
387 timezones0.put("America/Scoresbysund", tz);
388 timezones0.put("Atlantic/Azores", tz);
389 tz = new SimpleTimeZone(-1000 * 3600, "Atlantic/Cape_Verde");
390 timezones0.put("Atlantic/Cape_Verde", tz);
391 timezones0.put("Atlantic/Jan_Mayen", tz);
392 tz = new SimpleTimeZone(0 * 3600, "GMT");
393 timezones0.put("GMT", tz);
394 timezones0.put("UTC", tz);
395 timezones0.put("Africa/Abidjan", tz);
396 timezones0.put("Africa/Accra", tz);
397 timezones0.put("Africa/Bamako", tz);
398 timezones0.put("Africa/Banjul", tz);
399 timezones0.put("Africa/Bissau", tz);
400 timezones0.put("Africa/Casablanca", tz);
401 timezones0.put("Africa/Conakry", tz);
402 timezones0.put("Africa/Dakar", tz);
403 timezones0.put("Africa/El_Aaiun", tz);
404 timezones0.put("Africa/Freetown", tz);
405 timezones0.put("Africa/Lome", tz);
406 timezones0.put("Africa/Monrovia", tz);
407 timezones0.put("Africa/Nouakchott", tz);
408 timezones0.put("Africa/Ouagadougou", tz);
409 timezones0.put("Africa/Sao_Tome", tz);
410 timezones0.put("Africa/Timbuktu", tz);
411 timezones0.put("Atlantic/Reykjavik", tz);
412 timezones0.put("Atlantic/St_Helena", tz);
413 timezones0.put("Europe/Belfast", tz);
414 timezones0.put("Europe/Dublin", tz);
415 timezones0.put("Europe/London", tz);
416 tz = new SimpleTimeZone
417 (0 * 3600, "WET",
418 Calendar.MARCH, -1, Calendar.SUNDAY, 1000 * 3600,
419 Calendar.OCTOBER, -1, Calendar.SUNDAY, 1000 * 3600);
420 timezones0.put("WET", tz);
421 timezones0.put("Atlantic/Canary", tz);
422 timezones0.put("Atlantic/Faeroe", tz);
423 timezones0.put("Atlantic/Madeira", tz);
424 timezones0.put("Europe/Lisbon", tz);
425 tz = new SimpleTimeZone(1000 * 3600, "Africa/Algiers");
426 timezones0.put("Africa/Algiers", tz);
427 timezones0.put("Africa/Bangui", tz);
428 timezones0.put("Africa/Brazzaville", tz);
429 timezones0.put("Africa/Douala", tz);
430 timezones0.put("Africa/Kinshasa", tz);
431 timezones0.put("Africa/Lagos", tz);
432 timezones0.put("Africa/Libreville", tz);
433 timezones0.put("Africa/Luanda", tz);
434 timezones0.put("Africa/Malabo", tz);
435 timezones0.put("Africa/Ndjamena", tz);
436 timezones0.put("Africa/Niamey", tz);
437 timezones0.put("Africa/Porto-Novo", tz);
438 timezones0.put("Africa/Tunis", tz);
439 tz = new SimpleTimeZone
440 (1000 * 3600, "Africa/Windhoek",
441 Calendar.SEPTEMBER, 1, Calendar.SUNDAY, 2000 * 3600,
442 Calendar.APRIL, 1, Calendar.SUNDAY, 2000 * 3600);
443 timezones0.put("Africa/Windhoek", tz);
444 tz = new SimpleTimeZone
445 (1000 * 3600, "CET",
446 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
447 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
448 timezones0.put("CET", tz);
449 timezones0.put("ECT", tz);
450 timezones0.put("MET", tz);
451 timezones0.put("Africa/Ceuta", tz);
452 timezones0.put("Arctic/Longyearbyen", tz);
453 timezones0.put("Europe/Amsterdam", tz);
454 timezones0.put("Europe/Andorra", tz);
455 timezones0.put("Europe/Belgrade", tz);
456 timezones0.put("Europe/Berlin", tz);
457 timezones0.put("Europe/Bratislava", tz);
458 timezones0.put("Europe/Brussels", tz);
459 timezones0.put("Europe/Budapest", tz);
460 timezones0.put("Europe/Copenhagen", tz);
461 timezones0.put("Europe/Gibraltar", tz);
462 timezones0.put("Europe/Ljubljana", tz);
463 timezones0.put("Europe/Luxembourg", tz);
464 timezones0.put("Europe/Madrid", tz);
465 timezones0.put("Europe/Malta", tz);
466 timezones0.put("Europe/Monaco", tz);
467 timezones0.put("Europe/Oslo", tz);
468 timezones0.put("Europe/Paris", tz);
469 timezones0.put("Europe/Prague", tz);
470 timezones0.put("Europe/Rome", tz);
471 timezones0.put("Europe/San_Marino", tz);
472 timezones0.put("Europe/Sarajevo", tz);
473 timezones0.put("Europe/Skopje", tz);
474 timezones0.put("Europe/Stockholm", tz);
475 timezones0.put("Europe/Tirane", tz);
476 timezones0.put("Europe/Vaduz", tz);
477 timezones0.put("Europe/Vatican", tz);
478 timezones0.put("Europe/Vienna", tz);
479 timezones0.put("Europe/Warsaw", tz);
480 timezones0.put("Europe/Zagreb", tz);
481 timezones0.put("Europe/Zurich", tz);
482 tz = new SimpleTimeZone
483 (2000 * 3600, "ART",
484 Calendar.APRIL, -1, Calendar.FRIDAY, 0 * 3600,
485 Calendar.SEPTEMBER, -1, Calendar.THURSDAY, 23000 * 3600);
486 timezones0.put("ART", tz);
487 timezones0.put("Africa/Cairo", tz);
488 tz = new SimpleTimeZone(2000 * 3600, "CAT");
489 timezones0.put("CAT", tz);
490 timezones0.put("Africa/Blantyre", tz);
491 timezones0.put("Africa/Bujumbura", tz);
492 timezones0.put("Africa/Gaborone", tz);
493 timezones0.put("Africa/Harare", tz);
494 timezones0.put("Africa/Johannesburg", tz);
495 timezones0.put("Africa/Kigali", tz);
496 timezones0.put("Africa/Lubumbashi", tz);
497 timezones0.put("Africa/Lusaka", tz);
498 timezones0.put("Africa/Maputo", tz);
499 timezones0.put("Africa/Maseru", tz);
500 timezones0.put("Africa/Mbabane", tz);
501 timezones0.put("Africa/Tripoli", tz);
502 timezones0.put("Europe/Riga", tz);
503 timezones0.put("Europe/Tallinn", tz);
504 timezones0.put("Europe/Vilnius", tz);
505 tz = new SimpleTimeZone
506 (2000 * 3600, "Asia/Amman",
507 Calendar.MARCH, -1, Calendar.THURSDAY, 0 * 3600,
508 Calendar.SEPTEMBER, -1, Calendar.THURSDAY, 0 * 3600);
509 timezones0.put("Asia/Amman", tz);
510 tz = new SimpleTimeZone
511 (2000 * 3600, "Asia/Beirut",
512 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * 3600,
513 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
514 timezones0.put("Asia/Beirut", tz);
515 tz = new SimpleTimeZone
516 (2000 * 3600, "Asia/Damascus",
517 Calendar.APRIL, 1, 0, 0 * 3600,
518 Calendar.OCTOBER, 1, 0, 0 * 3600);
519 timezones0.put("Asia/Damascus", tz);
520 tz = new SimpleTimeZone
521 (2000 * 3600, "Asia/Gaza",
522 Calendar.APRIL, 3, Calendar.FRIDAY, 0 * 3600,
523 Calendar.OCTOBER, 3, Calendar.FRIDAY, 0 * 3600);
524 timezones0.put("Asia/Gaza", tz);
525 tz = new SimpleTimeZone
526 (2000 * 3600, "Asia/Jerusalem",
527 Calendar.APRIL, 1, 0, 1000 * 3600,
528 Calendar.OCTOBER, 1, 0, 1000 * 3600);
529 timezones0.put("Asia/Jerusalem", tz);
530 tz = new SimpleTimeZone
531 (2000 * 3600, "EET",
532 Calendar.MARCH, -1, Calendar.SUNDAY, 3000 * 3600,
533 Calendar.OCTOBER, -1, Calendar.SUNDAY, 3000 * 3600);
534 timezones0.put("EET", tz);
535 timezones0.put("Asia/Istanbul", tz);
536 timezones0.put("Asia/Nicosia", tz);
537 timezones0.put("Europe/Athens", tz);
538 timezones0.put("Europe/Bucharest", tz);
539 timezones0.put("Europe/Chisinau", tz);
540 timezones0.put("Europe/Helsinki", tz);
541 timezones0.put("Europe/Istanbul", tz);
542 timezones0.put("Europe/Kiev", tz);
543 timezones0.put("Europe/Nicosia", tz);
544 timezones0.put("Europe/Simferopol", tz);
545 timezones0.put("Europe/Sofia", tz);
546 timezones0.put("Europe/Uzhgorod", tz);
547 timezones0.put("Europe/Zaporozhye", tz);
548 tz = new SimpleTimeZone
549 (2000 * 3600, "Europe/Kaliningrad",
550 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
551 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
552 timezones0.put("Europe/Kaliningrad", tz);
553 timezones0.put("Europe/Minsk", tz);
554 tz = new SimpleTimeZone
555 (3000 * 3600, "Asia/Baghdad",
556 Calendar.APRIL, 1, 0, 3000 * 3600,
557 Calendar.OCTOBER, 1, 0, 3000 * 3600);
558 timezones0.put("Asia/Baghdad", tz);
559 tz = new SimpleTimeZone
560 (3000 * 3600, "Europe/Moscow",
561 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
562 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
563 timezones0.put("Europe/Moscow", tz);
564 timezones0.put("Europe/Tiraspol", tz);
565 tz = new SimpleTimeZone(3000 * 3600, "EAT");
566 timezones0.put("EAT", tz);
567 timezones0.put("Africa/Addis_Ababa", tz);
568 timezones0.put("Africa/Asmera", tz);
569 timezones0.put("Africa/Dar_es_Salaam", tz);
570 timezones0.put("Africa/Djibouti", tz);
571 timezones0.put("Africa/Kampala", tz);
572 timezones0.put("Africa/Khartoum", tz);
573 timezones0.put("Africa/Mogadishu", tz);
574 timezones0.put("Africa/Nairobi", tz);
575 timezones0.put("Antarctica/Syowa", tz);
576 timezones0.put("Asia/Aden", tz);
577 timezones0.put("Asia/Bahrain", tz);
578 timezones0.put("Asia/Kuwait", tz);
579 timezones0.put("Asia/Qatar", tz);
580 timezones0.put("Asia/Riyadh", tz);
581 timezones0.put("Indian/Antananarivo", tz);
582 timezones0.put("Indian/Comoro", tz);
583 timezones0.put("Indian/Mayotte", tz);
584 tz = new SimpleTimeZone(3500 * 3600, "Asia/Tehran");
585 timezones0.put("Asia/Tehran", tz);
586 tz = new SimpleTimeZone
587 (4000 * 3600, "Asia/Baku",
588 Calendar.MARCH, -1, Calendar.SUNDAY, 1000 * 3600,
589 Calendar.OCTOBER, -1, Calendar.SUNDAY, 1000 * 3600);
590 timezones0.put("Asia/Baku", tz);
591 tz = new SimpleTimeZone
592 (4000 * 3600, "Asia/Aqtau",
593 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * 3600,
594 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
595 timezones0.put("Asia/Aqtau", tz);
596 timezones0.put("Asia/Tbilisi", tz);
597 tz = new SimpleTimeZone
598 (4000 * 3600, "Asia/Yerevan",
599 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
600 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
601 timezones0.put("Asia/Yerevan", tz);
602 timezones0.put("Europe/Samara", tz);
603 tz = new SimpleTimeZone(4000 * 3600, "NET");
604 timezones0.put("NET", tz);
605 timezones0.put("Asia/Dubai", tz);
606 timezones0.put("Asia/Muscat", tz);
607 timezones0.put("Indian/Mahe", tz);
608 timezones0.put("Indian/Mauritius", tz);
609 timezones0.put("Indian/Reunion", tz);
610 tz = new SimpleTimeZone(4500 * 3600, "Asia/Kabul");
611 timezones0.put("Asia/Kabul", tz);
612 tz = new SimpleTimeZone
613 (5000 * 3600, "Asia/Aqtobe",
614 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * 3600,
615 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
616 timezones0.put("Asia/Aqtobe", tz);
617 tz = new SimpleTimeZone
618 (5000 * 3600, "Asia/Bishkek",
619 Calendar.MARCH, -1, Calendar.SUNDAY, 2500 * 3600,
620 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2500 * 3600);
621 timezones0.put("Asia/Bishkek", tz);
622 tz = new SimpleTimeZone
623 (5000 * 3600, "Asia/Yekaterinburg",
624 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
625 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
626 timezones0.put("Asia/Yekaterinburg", tz);
627 tz = new SimpleTimeZone(5000 * 3600, "PLT");
628 timezones0.put("PLT", tz);
629 timezones0.put("Asia/Ashgabat", tz);
630 timezones0.put("Asia/Dushanbe", tz);
631 timezones0.put("Asia/Karachi", tz);
632 timezones0.put("Asia/Samarkand", tz);
633 timezones0.put("Asia/Tashkent", tz);
634 timezones0.put("Indian/Chagos", tz);
635 timezones0.put("Indian/Kerguelen", tz);
636 timezones0.put("Indian/Maldives", tz);
637 tz = new SimpleTimeZone(5500 * 3600, "IST");
638 timezones0.put("IST", tz);
639 timezones0.put("Asia/Calcutta", tz);
640 tz = new SimpleTimeZone(5750 * 3600, "Asia/Katmandu");
641 timezones0.put("Asia/Katmandu", tz);
642 tz = new SimpleTimeZone(6000 * 3600, "BST");
643 timezones0.put("BST", tz);
644 timezones0.put("Antarctica/Mawson", tz);
645 timezones0.put("Asia/Colombo", tz);
646 timezones0.put("Asia/Dhaka", tz);
647 timezones0.put("Asia/Thimphu", tz);
648 tz = new SimpleTimeZone
649 (6000 * 3600, "Asia/Almaty",
650 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * 3600,
651 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * 3600);
652 timezones0.put("Asia/Almaty", tz);
653 tz = new SimpleTimeZone
654 (6000 * 3600, "Asia/Novosibirsk",
655 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
656 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
657 timezones0.put("Asia/Novosibirsk", tz);
658 timezones0.put("Asia/Omsk", tz);
659 tz = new SimpleTimeZone(6500 * 3600, "Asia/Rangoon");
660 timezones0.put("Asia/Rangoon", tz);
661 timezones0.put("Indian/Cocos", tz);
662 tz = new SimpleTimeZone(7000 * 3600, "VST");
663 timezones0.put("VST", tz);
664 timezones0.put("Antarctica/Davis", tz);
665 timezones0.put("Asia/Bangkok", tz);
666 timezones0.put("Asia/Hovd", tz);
667 timezones0.put("Asia/Jakarta", tz);
668 timezones0.put("Asia/Phnom_Penh", tz);
669 timezones0.put("Asia/Saigon", tz);
670 timezones0.put("Asia/Vientiane", tz);
671 timezones0.put("Indian/Christmas", tz);
672 tz = new SimpleTimeZone
673 (7000 * 3600, "Asia/Krasnoyarsk",
674 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
675 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
676 timezones0.put("Asia/Krasnoyarsk", tz);
677 tz = new SimpleTimeZone(8000 * 3600, "CTT");
678 timezones0.put("CTT", tz);
679 timezones0.put("Antarctica/Casey", tz);
680 timezones0.put("Asia/Brunei", tz);
681 timezones0.put("Asia/Chungking", tz);
682 timezones0.put("Asia/Harbin", tz);
683 timezones0.put("Asia/Hong_Kong", tz);
684 timezones0.put("Asia/Kashgar", tz);
685 timezones0.put("Asia/Kuala_Lumpur", tz);
686 timezones0.put("Asia/Kuching", tz);
687 timezones0.put("Asia/Macao", tz);
688 timezones0.put("Asia/Manila", tz);
689 timezones0.put("Asia/Shanghai", tz);
690 timezones0.put("Asia/Singapore", tz);
691 timezones0.put("Asia/Taipei", tz);
692 timezones0.put("Asia/Ujung_Pandang", tz);
693 timezones0.put("Asia/Ulaanbaatar", tz);
694 timezones0.put("Asia/Urumqi", tz);
695 timezones0.put("Australia/Perth", tz);
696 tz = new SimpleTimeZone
697 (8000 * 3600, "Asia/Irkutsk",
698 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
699 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
700 timezones0.put("Asia/Irkutsk", tz);
701 tz = new SimpleTimeZone(9000 * 3600, "JST");
702 timezones0.put("JST", tz);
703 timezones0.put("Asia/Dili", tz);
704 timezones0.put("Asia/Jayapura", tz);
705 timezones0.put("Asia/Pyongyang", tz);
706 timezones0.put("Asia/Seoul", tz);
707 timezones0.put("Asia/Tokyo", tz);
708 timezones0.put("Pacific/Palau", tz);
709 tz = new SimpleTimeZone
710 (9000 * 3600, "Asia/Yakutsk",
711 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
712 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
713 timezones0.put("Asia/Yakutsk", tz);
714 tz = new SimpleTimeZone
715 (9500 * 3600, "Australia/Adelaide",
716 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600,
717 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600);
718 timezones0.put("Australia/Adelaide", tz);
719 timezones0.put("Australia/Broken_Hill", tz);
720 tz = new SimpleTimeZone(9500 * 3600, "ACT");
721 timezones0.put("ACT", tz);
722 timezones0.put("Australia/Darwin", tz);
723 tz = new SimpleTimeZone(10000 * 3600, "Antarctica/DumontDUrville");
724 timezones0.put("Antarctica/DumontDUrville", tz);
725 timezones0.put("Australia/Brisbane", tz);
726 timezones0.put("Australia/Lindeman", tz);
727 timezones0.put("Pacific/Guam", tz);
728 timezones0.put("Pacific/Port_Moresby", tz);
729 timezones0.put("Pacific/Saipan", tz);
730 timezones0.put("Pacific/Truk", tz);
731 timezones0.put("Pacific/Yap", tz);
732 tz = new SimpleTimeZone
733 (10000 * 3600, "Asia/Vladivostok",
734 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
735 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
736 timezones0.put("Asia/Vladivostok", tz);
737 tz = new SimpleTimeZone
738 (10000 * 3600, "Australia/Hobart",
739 Calendar.OCTOBER, 1, Calendar.SUNDAY, 2000 * 3600,
740 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600);
741 timezones0.put("Australia/Hobart", tz);
742 tz = new SimpleTimeZone
743 (10000 * 3600, "AET",
744 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600,
745 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600);
746 timezones0.put("AET", tz);
747 timezones0.put("Australia/Melbourne", tz);
748 timezones0.put("Australia/Sydney", tz);
749 tz = new SimpleTimeZone
750 (10500 * 3600, "Australia/Lord_Howe",
751 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600,
752 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600, 500 * 3600);
753 timezones0.put("Australia/Lord_Howe", tz);
754 tz = new SimpleTimeZone
755 (11000 * 3600, "Asia/Magadan",
756 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
757 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
758 timezones0.put("Asia/Magadan", tz);
759 tz = new SimpleTimeZone(11000 * 3600, "SST");
760 timezones0.put("SST", tz);
761 timezones0.put("Pacific/Efate", tz);
762 timezones0.put("Pacific/Guadalcanal", tz);
763 timezones0.put("Pacific/Kosrae", tz);
764 timezones0.put("Pacific/Noumea", tz);
765 timezones0.put("Pacific/Ponape", tz);
766 tz = new SimpleTimeZone(11500 * 3600, "Pacific/Norfolk");
767 timezones0.put("Pacific/Norfolk", tz);
768 tz = new SimpleTimeZone
769 (12000 * 3600, "NST",
770 Calendar.OCTOBER, 1, Calendar.SUNDAY, 2000 * 3600,
771 Calendar.MARCH, 3, Calendar.SUNDAY, 2000 * 3600);
772 timezones0.put("NST", tz);
773 timezones0.put("Antarctica/McMurdo", tz);
774 timezones0.put("Antarctica/South_Pole", tz);
775 timezones0.put("Pacific/Auckland", tz);
776 tz = new SimpleTimeZone
777 (12000 * 3600, "Asia/Anadyr",
778 Calendar.MARCH, -1, Calendar.SUNDAY, 2000 * 3600,
779 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2000 * 3600);
780 timezones0.put("Asia/Anadyr", tz);
781 timezones0.put("Asia/Kamchatka", tz);
782 tz = new SimpleTimeZone(12000 * 3600, "Pacific/Fiji");
783 timezones0.put("Pacific/Fiji", tz);
784 timezones0.put("Pacific/Funafuti", tz);
785 timezones0.put("Pacific/Kwajalein", tz);
786 timezones0.put("Pacific/Majuro", tz);
787 timezones0.put("Pacific/Nauru", tz);
788 timezones0.put("Pacific/Tarawa", tz);
789 timezones0.put("Pacific/Wake", tz);
790 timezones0.put("Pacific/Wallis", tz);
791 tz = new SimpleTimeZone
792 (12750 * 3600, "Pacific/Chatham",
793 Calendar.OCTOBER, 1, Calendar.SUNDAY, 2750 * 3600,
794 Calendar.MARCH, 3, Calendar.SUNDAY, 2750 * 3600);
795 timezones0.put("Pacific/Chatham", tz);
796 tz = new SimpleTimeZone(13000 * 3600, "Pacific/Enderbury");
797 timezones0.put("Pacific/Enderbury", tz);
798 timezones0.put("Pacific/Tongatapu", tz);
799 tz = new SimpleTimeZone(14000 * 3600, "Pacific/Kiritimati");
800 timezones0.put("Pacific/Kiritimati", tz);
802 return timezones0;
806 * Maps a time zone name (with optional GMT offset and daylight time
807 * zone name) to one of the known time zones. This method called
808 * with the result of <code>System.getProperty("user.timezone")</code>
809 * or <code>getDefaultTimeZoneId()</code>. Note that giving one of
810 * the standard tz data names from ftp://elsie.nci.nih.gov/pub/ is
811 * preferred. The time zone name can be given as follows:
812 * <code>(standard zone name)[(GMT offset)[(daylight time zone name)]]</code>
813 * <p>
814 * If only a (standard zone name) is given (no numbers in the
815 * String) then it gets mapped directly to the TimeZone with that
816 * name, if that fails null is returned.
817 * <p>
818 * A GMT offset is the offset to add to the local time to get GMT.
819 * If a (GMT offset) is included (either in seconds or hours) then
820 * an attempt is made to find a TimeZone name matching both the name
821 * and the offset (that doesn't observe daylight time, if the
822 * timezone observes daylight time then you must include a daylight
823 * time zone name after the offset), if that fails then a TimeZone
824 * with the given GMT offset is returned (whether or not the
825 * TimeZone observes daylight time is ignored), if that also fails
826 * the GMT TimeZone is returned.
827 * <p>
828 * If the String ends with (GMT offset)(daylight time zone name)
829 * then an attempt is made to find a TimeZone with the given name and
830 * GMT offset that also observes (the daylight time zone name is not
831 * currently used in any other way), if that fails a TimeZone with
832 * the given GMT offset that observes daylight time is returned, if
833 * that also fails the GMT TimeZone is returned.
834 * <p>
835 * Examples: In Chicago, the time zone id could be "CST6CDT", but
836 * the preferred name would be "America/Chicago". In Indianapolis
837 * (which does not have Daylight Savings Time) the string could be
838 * "EST5", but the preferred name would be "America/Indianapolis".
839 * The standard time zone name for The Netherlands is "Europe/Amsterdam",
840 * but can also be given as "CET-1CEST".
842 static TimeZone getDefaultTimeZone(String sysTimeZoneId)
844 // First find start of GMT offset info and any Daylight zone name.
845 int startGMToffset = 0;
846 int sysTimeZoneIdLength = sysTimeZoneId.length();
847 for (int i = 0; i < sysTimeZoneIdLength && startGMToffset == 0; i++)
849 char c = sysTimeZoneId.charAt(i);
850 if (Character.isDigit(c))
851 startGMToffset = i;
852 else if ((c == '+' || c == '-')
853 && i + 1 < sysTimeZoneIdLength
854 && Character.isDigit(sysTimeZoneId.charAt(i + 1)))
855 startGMToffset = i;
858 String tzBasename;
859 if (startGMToffset == 0)
860 tzBasename = sysTimeZoneId;
861 else
862 tzBasename = sysTimeZoneId.substring (0, startGMToffset);
864 int startDaylightZoneName = 0;
865 for (int i = sysTimeZoneIdLength - 1;
866 i >= 0 && !Character.isDigit(sysTimeZoneId.charAt(i)); --i)
867 startDaylightZoneName = i;
869 boolean useDaylightTime = startDaylightZoneName > 0;
871 // Integer.parseInt() doesn't handle leading +.
872 if (sysTimeZoneId.charAt(startGMToffset) == '+')
873 startGMToffset++;
875 int gmtOffset = 0;
876 if (startGMToffset > 0)
878 gmtOffset = Integer.parseInt
879 (startDaylightZoneName == 0
880 ? sysTimeZoneId.substring(startGMToffset)
881 : sysTimeZoneId.substring(startGMToffset,
882 startDaylightZoneName));
884 // Offset could be in hours or seconds. Convert to millis.
885 // The offset is given as the time to add to local time to get GMT
886 // we need the time to add to GMT to get localtime.
887 if (Math.abs(gmtOffset) < 24)
888 gmtOffset *= 60 * 60;
889 gmtOffset *= -1000;
892 // Try to be optimistic and get the timezone that matches the base name.
893 // If we only have the base name then just accept this timezone.
894 // Otherwise check the gmtOffset and day light attributes.
895 TimeZone tz = (TimeZone) timezones().get(tzBasename);
896 if (tz != null
897 && (tzBasename == sysTimeZoneId
898 || (tz.getRawOffset() == gmtOffset
899 && tz.useDaylightTime() == useDaylightTime)))
900 return tz;
902 // Maybe there is one with the daylight zone name?
903 if (useDaylightTime)
905 String daylightZoneName;
906 daylightZoneName = sysTimeZoneId.substring(startDaylightZoneName);
907 if (!daylightZoneName.equals(tzBasename))
909 tz = (TimeZone) timezones().get(tzBasename);
910 if (tz != null
911 && tz.getRawOffset() == gmtOffset
912 && tz.useDaylightTime())
913 return tz;
917 // If no match, see if a valid timezone has similar attributes as this
918 // and then use it instead. We take the first one that looks OKish.
919 if (startGMToffset > 0)
921 String[] ids = getAvailableIDs(gmtOffset);
922 for (int i = 0; i < ids.length; i++)
924 tz = (TimeZone) timezones().get(ids[i]);
925 if (tz.useDaylightTime() == useDaylightTime)
926 return tz;
930 return null;
934 * Gets the time zone offset, for current date, modified in case of
935 * daylight savings. This is the offset to add to UTC to get the local
936 * time.
937 * @param era the era of the given date
938 * @param year the year of the given date
939 * @param month the month of the given date, 0 for January.
940 * @param day the day of month
941 * @param dayOfWeek the day of week
942 * @param milliseconds the millis in the day (in local standard time)
943 * @return the time zone offset in milliseconds.
945 public abstract int getOffset(int era, int year, int month,
946 int day, int dayOfWeek, int milliseconds);
949 * Get the time zone offset for the specified date, modified in case of
950 * daylight savings. This is the offset to add to UTC to get the local
951 * time.
952 * @param date the date represented in millisecends
953 * since January 1, 1970 00:00:00 GMT.
954 * @since 1.4
956 public int getOffset(long date)
958 return (inDaylightTime(new Date(date))
959 ? getRawOffset() + getDSTSavings()
960 : getRawOffset());
964 * Gets the time zone offset, ignoring daylight savings. This is
965 * the offset to add to UTC to get the local time.
966 * @return the time zone offset in milliseconds.
968 public abstract int getRawOffset();
971 * Sets the time zone offset, ignoring daylight savings. This is
972 * the offset to add to UTC to get the local time.
973 * @param offsetMillis the time zone offset to GMT.
975 public abstract void setRawOffset(int offsetMillis);
978 * Gets the identifier of this time zone. For instance, PST for
979 * Pacific Standard Time.
980 * @returns the ID of this time zone.
982 public String getID()
984 return ID;
988 * Sets the identifier of this time zone. For instance, PST for
989 * Pacific Standard Time.
990 * @param id the new time zone ID.
992 public void setID(String id)
994 this.ID = id;
998 * This method returns a string name of the time zone suitable
999 * for displaying to the user. The string returned will be the long
1000 * description of the timezone in the current locale. The name
1001 * displayed will assume daylight savings time is not in effect.
1003 * @return The name of the time zone.
1005 public final String getDisplayName()
1007 return (getDisplayName(false, LONG, Locale.getDefault()));
1011 * This method returns a string name of the time zone suitable
1012 * for displaying to the user. The string returned will be the long
1013 * description of the timezone in the specified locale. The name
1014 * displayed will assume daylight savings time is not in effect.
1016 * @param locale The locale for this timezone name.
1018 * @return The name of the time zone.
1020 public final String getDisplayName(Locale locale)
1022 return (getDisplayName(false, LONG, locale));
1026 * This method returns a string name of the time zone suitable
1027 * for displaying to the user. The string returned will be of the
1028 * specified type in the current locale.
1030 * @param dst Whether or not daylight savings time is in effect.
1031 * @param style <code>LONG</code> for a long name, <code>SHORT</code> for
1032 * a short abbreviation.
1034 * @return The name of the time zone.
1036 public final String getDisplayName(boolean dst, int style)
1038 return (getDisplayName(dst, style, Locale.getDefault()));
1043 * This method returns a string name of the time zone suitable
1044 * for displaying to the user. The string returned will be of the
1045 * specified type in the specified locale.
1047 * @param dst Whether or not daylight savings time is in effect.
1048 * @param style <code>LONG</code> for a long name, <code>SHORT</code> for
1049 * a short abbreviation.
1050 * @param locale The locale for this timezone name.
1052 * @return The name of the time zone.
1054 public String getDisplayName(boolean dst, int style, Locale locale)
1056 DateFormatSymbols dfs;
1059 dfs = new DateFormatSymbols(locale);
1061 // The format of the value returned is defined by us.
1062 String[][]zoneinfo = dfs.getZoneStrings();
1063 for (int i = 0; i < zoneinfo.length; i++)
1065 if (zoneinfo[i][0].equals(getID()))
1067 if (!dst)
1069 if (style == SHORT)
1070 return (zoneinfo[i][2]);
1071 else
1072 return (zoneinfo[i][1]);
1074 else
1076 if (style == SHORT)
1077 return (zoneinfo[i][4]);
1078 else
1079 return (zoneinfo[i][3]);
1084 catch (MissingResourceException e)
1088 return getDefaultDisplayName(dst);
1091 private String getDefaultDisplayName(boolean dst)
1093 int offset = getRawOffset();
1094 if (dst && this instanceof SimpleTimeZone)
1096 // ugly, but this is a design failure of the API:
1097 // getDisplayName takes a dst parameter even though
1098 // TimeZone knows nothing about daylight saving offsets.
1099 offset += ((SimpleTimeZone) this).getDSTSavings();
1102 StringBuffer sb = new StringBuffer(9);
1103 sb.append("GMT");
1104 sb.append(offset >= 0 ? '+' : '-');
1106 offset = Math.abs(offset) / (1000 * 60);
1107 int hours = offset / 60;
1108 int minutes = offset % 60;
1110 sb.append((char) ('0' + hours / 10)).append((char) ('0' + hours % 10));
1111 sb.append(':');
1112 sb.append((char) ('0' + minutes / 10)).append((char) ('0' + minutes % 10));
1113 return sb.toString();
1116 /**
1117 * Returns true, if this time zone uses Daylight Savings Time.
1119 public abstract boolean useDaylightTime();
1122 * Returns true, if the given date is in Daylight Savings Time in this
1123 * time zone.
1124 * @param date the given Date.
1126 public abstract boolean inDaylightTime(Date date);
1129 * Gets the daylight savings offset. This is a positive offset in
1130 * milliseconds with respect to standard time. Typically this
1131 * is one hour, but for some time zones this may be half an our.
1132 * <p>The default implementation returns 3600000 milliseconds
1133 * (one hour) if the time zone uses daylight savings time
1134 * (as specified by {@link #useDaylightTime()}), otherwise
1135 * it returns 0.
1136 * @return the daylight savings offset in milliseconds.
1137 * @since 1.4
1139 public int getDSTSavings ()
1141 return useDaylightTime () ? 3600000 : 0;
1145 * Gets the TimeZone for the given ID.
1146 * @param ID the time zone identifier.
1147 * @return The time zone for the identifier or GMT, if no such time
1148 * zone exists.
1150 // FIXME: XXX: JCL indicates this and other methods are synchronized.
1151 public static TimeZone getTimeZone(String ID)
1153 // First check timezones hash
1154 TimeZone tz = (TimeZone) timezones().get(ID);
1155 if (tz != null)
1157 if (tz.getID().equals(ID))
1158 return tz;
1160 // We always return a timezone with the requested ID.
1161 // This is the same behaviour as with JDK1.2.
1162 tz = (TimeZone) tz.clone();
1163 tz.setID(ID);
1164 // We also save the alias, so that we return the same
1165 // object again if getTimeZone is called with the same
1166 // alias.
1167 timezones().put(ID, tz);
1168 return tz;
1171 // See if the ID is really a GMT offset form.
1172 // Note that GMT is in the table so we know it is different.
1173 if (ID.startsWith("GMT"))
1175 int pos = 3;
1176 int offset_direction = 1;
1178 if (ID.charAt(pos) == '-')
1180 offset_direction = -1;
1181 pos++;
1183 else if (ID.charAt(pos) == '+')
1185 pos++;
1190 int hour, minute;
1192 String offset_str = ID.substring(pos);
1193 int idx = offset_str.indexOf(":");
1194 if (idx != -1)
1196 hour = Integer.parseInt(offset_str.substring(0, idx));
1197 minute = Integer.parseInt(offset_str.substring(idx + 1));
1199 else
1201 int offset_length = offset_str.length();
1202 if (offset_length <= 2)
1204 // Only hour
1205 hour = Integer.parseInt(offset_str);
1206 minute = 0;
1208 else
1210 // hour and minute, not separated by colon
1211 hour = Integer.parseInt
1212 (offset_str.substring(0, offset_length - 2));
1213 minute = Integer.parseInt
1214 (offset_str.substring(offset_length - 2));
1218 return new SimpleTimeZone((hour * (60 * 60 * 1000) +
1219 minute * (60 * 1000))
1220 * offset_direction, ID);
1222 catch (NumberFormatException e)
1227 // Finally, return GMT per spec
1228 return getTimeZone("GMT");
1232 * Gets the available IDs according to the given time zone
1233 * offset.
1234 * @param rawOffset the given time zone GMT offset.
1235 * @return An array of IDs, where the time zone has the specified GMT
1236 * offset. For example <code>{"Phoenix", "Denver"}</code>, since both have
1237 * GMT-07:00, but differ in daylight savings behaviour.
1239 public static String[] getAvailableIDs(int rawOffset)
1241 int count = 0;
1242 Iterator iter = timezones().entrySet().iterator();
1243 while (iter.hasNext())
1245 // Don't iterate the values, since we want to count
1246 // doubled values (aliases)
1247 Map.Entry entry = (Map.Entry) iter.next();
1248 if (((TimeZone) entry.getValue()).getRawOffset() == rawOffset)
1249 count++;
1252 String[] ids = new String[count];
1253 count = 0;
1254 iter = timezones().entrySet().iterator();
1255 while (iter.hasNext())
1257 Map.Entry entry = (Map.Entry) iter.next();
1258 if (((TimeZone) entry.getValue()).getRawOffset() == rawOffset)
1259 ids[count++] = (String) entry.getKey();
1261 return ids;
1265 * Gets all available IDs.
1266 * @return An array of all supported IDs.
1268 public static String[] getAvailableIDs()
1270 return (String[])
1271 timezones().keySet().toArray(new String[timezones().size()]);
1275 * Returns the time zone under which the host is running. This
1276 * can be changed with setDefault.
1278 * @return A clone of the current default time zone for this host.
1279 * @see #setDefault
1281 public static TimeZone getDefault()
1283 return (TimeZone) defaultZone().clone();
1286 public static void setDefault(TimeZone zone)
1288 // Hmmmm. No Security checks?
1289 defaultZone0 = zone;
1293 * Test if the other time zone uses the same rule and only
1294 * possibly differs in ID. This implementation for this particular
1295 * class will return true if the raw offsets are identical. Subclasses
1296 * should override this method if they use daylight savings.
1297 * @return true if this zone has the same raw offset
1299 public boolean hasSameRules(TimeZone other)
1301 return other.getRawOffset() == getRawOffset();
1305 * Returns a clone of this object. I can't imagine, why this is
1306 * useful for a time zone.
1308 public Object clone()
1312 return super.clone();
1314 catch (CloneNotSupportedException ex)
1316 return null;