Don't obfuscate names of fields in objects - otherwise fields provided by two modules may clash
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
30 * The original version of this source code and documentation is copyrighted
31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 * materials are provided under terms of a License Agreement between Taligent
33 * and Sun. This technology is protected by multiple US and International
34 * patents. This notice and attribution to Taligent may not be removed.
35 * Taligent is a registered trademark of Taligent, Inc.
41 import java.io.Serializable;
42 import java.lang.ref.SoftReference;
43 import java.security.AccessController;
44 import java.security.PrivilegedAction;
45 import java.util.concurrent.ConcurrentHashMap;
48 * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
52 * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
53 * which creates a <code>TimeZone</code> based on the time zone where the program
54 * is running. For example, for a program running in Japan, <code>getDefault</code>
55 * creates a <code>TimeZone</code> object based on Japanese Standard Time.
58 * You can also get a <code>TimeZone</code> using <code>getTimeZone</code>
59 * along with a time zone ID. For instance, the time zone ID for the
60 * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a
61 * U.S. Pacific Time <code>TimeZone</code> object with:
63 * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
65 * You can use the <code>getAvailableIDs</code> method to iterate through
66 * all the supported time zone IDs. You can then choose a
67 * supported ID to get a <code>TimeZone</code>.
68 * If the time zone you want is not represented by one of the
69 * supported IDs, then a custom time zone ID can be specified to
70 * produce a TimeZone. The syntax of a custom time zone ID is:
73 * <a name="CustomID"><i>CustomID:</i></a>
74 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
75 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
76 * <code>GMT</code> <i>Sign</i> <i>Hours</i>
81 * <i>Digit</i> <i>Digit</i>
83 * <i>Digit</i> <i>Digit</i>
84 * <i>Digit:</i> one of
85 * <code>0 1 2 3 4 5 6 7 8 9</code>
88 * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
89 * between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten
90 * hours and ten minutes ahead of GMT, respectively.
92 * The format is locale independent and digits must be taken from the
93 * Basic Latin block of the Unicode standard. No daylight saving time
94 * transition schedule can be specified with a custom time zone ID. If
95 * the specified string doesn't match the syntax, <code>"GMT"</code>
98 * When creating a <code>TimeZone</code>, the specified custom time
99 * zone ID is normalized in the following syntax:
101 * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a>
102 * <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i>
103 * <i>Sign:</i> one of
105 * <i>TwoDigitHours:</i>
106 * <i>Digit</i> <i>Digit</i>
108 * <i>Digit</i> <i>Digit</i>
109 * <i>Digit:</i> one of
110 * <code>0 1 2 3 4 5 6 7 8 9</code>
111 * </pre></blockquote>
112 * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00".
114 * <h4>Three-letter time zone IDs</h4>
116 * For compatibility with JDK 1.1.x, some other three-letter time zone IDs
117 * (such as "PST", "CTT", "AST") are also supported. However, <strong>their
118 * use is deprecated</strong> because the same abbreviation is often used
119 * for multiple time zones (for example, "CST" could be U.S. "Central Standard
120 * Time" and "China Standard Time"), and the Java platform can then only
121 * recognize one of them.
125 * @see GregorianCalendar
126 * @see SimpleTimeZone
127 * @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
130 abstract public class TimeZone implements Serializable, Cloneable {
132 * Sole constructor. (For invocation by subclass constructors, typically
139 * A style specifier for <code>getDisplayName()</code> indicating
140 * a short name, such as "PST."
144 public static final int SHORT = 0;
147 * A style specifier for <code>getDisplayName()</code> indicating
148 * a long name, such as "Pacific Standard Time."
152 public static final int LONG = 1;
154 // Constants used internally; unit is milliseconds
155 private static final int ONE_MINUTE = 60*1000;
156 private static final int ONE_HOUR = 60*ONE_MINUTE;
157 private static final int ONE_DAY = 24*ONE_HOUR;
159 // Proclaim serialization compatibility with JDK 1.1
160 static final long serialVersionUID = 3581463369166924961L;
163 * Gets the time zone offset, for current date, modified in case of
164 * daylight savings. This is the offset to add to UTC to get local time.
166 * This method returns a historically correct offset if an
167 * underlying <code>TimeZone</code> implementation subclass
168 * supports historical Daylight Saving Time schedule and GMT
171 * @param era the era of the given date.
172 * @param year the year in the given date.
173 * @param month the month in the given date.
174 * Month is 0-based. e.g., 0 for January.
175 * @param day the day-in-month of the given date.
176 * @param dayOfWeek the day-of-week of the given date.
177 * @param milliseconds the milliseconds in day in <em>standard</em>
180 * @return the offset in milliseconds to add to GMT to get local time.
182 * @see Calendar#ZONE_OFFSET
183 * @see Calendar#DST_OFFSET
185 public abstract int getOffset(int era, int year, int month, int day,
186 int dayOfWeek, int milliseconds);
189 * Returns the offset of this time zone from UTC at the specified
190 * date. If Daylight Saving Time is in effect at the specified
191 * date, the offset value is adjusted with the amount of daylight
194 * This method returns a historically correct offset value if an
195 * underlying TimeZone implementation subclass supports historical
196 * Daylight Saving Time schedule and GMT offset changes.
198 * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
199 * @return the amount of time in milliseconds to add to UTC to get local time.
201 * @see Calendar#ZONE_OFFSET
202 * @see Calendar#DST_OFFSET
205 public int getOffset(long date) {
206 if (inDaylightTime(new Date(date))) {
207 return getRawOffset() + getDSTSavings();
209 return getRawOffset();
213 * Gets the raw GMT offset and the amount of daylight saving of this
214 * time zone at the given time.
215 * @param date the milliseconds (since January 1, 1970,
216 * 00:00:00.000 GMT) at which the time zone offset and daylight
217 * saving amount are found
218 * @param offset an array of int where the raw GMT offset
219 * (offset[0]) and daylight saving amount (offset[1]) are stored,
220 * or null if those values are not needed. The method assumes that
221 * the length of the given array is two or larger.
222 * @return the total amount of the raw GMT offset and daylight
223 * saving at the specified date.
225 * @see Calendar#ZONE_OFFSET
226 * @see Calendar#DST_OFFSET
228 int getOffsets(long date, int[] offsets) {
229 int rawoffset = getRawOffset();
231 if (inDaylightTime(new Date(date))) {
232 dstoffset = getDSTSavings();
234 if (offsets != null) {
235 offsets[0] = rawoffset;
236 offsets[1] = dstoffset;
238 return rawoffset + dstoffset;
242 * Sets the base time zone offset to GMT.
243 * This is the offset to add to UTC to get local time.
245 * If an underlying <code>TimeZone</code> implementation subclass
246 * supports historical GMT offset changes, the specified GMT
247 * offset is set as the latest GMT offset and the difference from
248 * the known latest GMT offset value is used to adjust all
249 * historical GMT offset values.
251 * @param offsetMillis the given base time zone offset to GMT.
253 abstract public void setRawOffset(int offsetMillis);
256 * Returns the amount of time in milliseconds to add to UTC to get
257 * standard time in this time zone. Because this value is not
258 * affected by daylight saving time, it is called <I>raw
261 * If an underlying <code>TimeZone</code> implementation subclass
262 * supports historical GMT offset changes, the method returns the
263 * raw offset value of the current date. In Honolulu, for example,
264 * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and
265 * this method always returns -36000000 milliseconds (i.e., -10
268 * @return the amount of raw offset time in milliseconds to add to UTC.
269 * @see Calendar#ZONE_OFFSET
271 public abstract int getRawOffset();
274 * Gets the ID of this time zone.
275 * @return the ID of this time zone.
277 public String getID()
283 * Sets the time zone ID. This does not change any other data in
284 * the time zone object.
285 * @param ID the new time zone ID.
287 public void setID(String ID)
290 throw new NullPointerException();
296 * Returns a long standard time name of this {@code TimeZone} suitable for
297 * presentation to the user in the default locale.
299 * <p>This method is equivalent to:
301 * getDisplayName(false, {@link #LONG},
302 * Locale.getDefault({@link Locale.Category#DISPLAY}))
303 * </blockquote></pre>
305 * @return the human-readable name of this time zone in the default locale.
307 * @see #getDisplayName(boolean, int, Locale)
308 * @see Locale#getDefault(Locale.Category)
309 * @see Locale.Category
311 public final String getDisplayName() {
312 return getDisplayName(false, LONG,
313 Locale.getDefault(Locale.Category.DISPLAY));
317 * Returns a long standard time name of this {@code TimeZone} suitable for
318 * presentation to the user in the specified {@code locale}.
320 * <p>This method is equivalent to:
322 * getDisplayName(false, {@link #LONG}, locale)
323 * </blockquote></pre>
325 * @param locale the locale in which to supply the display name.
326 * @return the human-readable name of this time zone in the given locale.
327 * @exception NullPointerException if {@code locale} is {@code null}.
329 * @see #getDisplayName(boolean, int, Locale)
331 public final String getDisplayName(Locale locale) {
332 return getDisplayName(false, LONG, locale);
336 * Returns a name in the specified {@code style} of this {@code TimeZone}
337 * suitable for presentation to the user in the default locale. If the
338 * specified {@code daylight} is {@code true}, a Daylight Saving Time name
339 * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving
340 * Time). Otherwise, a Standard Time name is returned.
342 * <p>This method is equivalent to:
344 * getDisplayName(daylight, style,
345 * Locale.getDefault({@link Locale.Category#DISPLAY}))
346 * </blockquote></pre>
348 * @param daylight {@code true} specifying a Daylight Saving Time name, or
349 * {@code false} specifying a Standard Time name
350 * @param style either {@link #LONG} or {@link #SHORT}
351 * @return the human-readable name of this time zone in the default locale.
352 * @exception IllegalArgumentException if {@code style} is invalid.
354 * @see #getDisplayName(boolean, int, Locale)
355 * @see Locale#getDefault(Locale.Category)
356 * @see Locale.Category
357 * @see java.text.DateFormatSymbols#getZoneStrings()
359 public final String getDisplayName(boolean daylight, int style) {
360 return getDisplayName(daylight, style,
361 Locale.getDefault(Locale.Category.DISPLAY));
365 * Returns a name in the specified {@code style} of this {@code TimeZone}
366 * suitable for presentation to the user in the specified {@code
367 * locale}. If the specified {@code daylight} is {@code true}, a Daylight
368 * Saving Time name is returned (even if this {@code TimeZone} doesn't
369 * observe Daylight Saving Time). Otherwise, a Standard Time name is
372 * <p>When looking up a time zone name, the {@linkplain
373 * ResourceBundle.Control#getCandidateLocales(String,Locale) default
374 * <code>Locale</code> search path of <code>ResourceBundle</code>} derived
375 * from the specified {@code locale} is used. (No {@linkplain
376 * ResourceBundle.Control#getFallbackLocale(String,Locale) fallback
377 * <code>Locale</code>} search is performed.) If a time zone name in any
378 * {@code Locale} of the search path, including {@link Locale#ROOT}, is
379 * found, the name is returned. Otherwise, a string in the
380 * <a href="#NormalizedCustomID">normalized custom ID format</a> is returned.
382 * @param daylight {@code true} specifying a Daylight Saving Time name, or
383 * {@code false} specifying a Standard Time name
384 * @param style either {@link #LONG} or {@link #SHORT}
385 * @param locale the locale in which to supply the display name.
386 * @return the human-readable name of this time zone in the given locale.
387 * @exception IllegalArgumentException if {@code style} is invalid.
388 * @exception NullPointerException if {@code locale} is {@code null}.
390 * @see java.text.DateFormatSymbols#getZoneStrings()
392 public String getDisplayName(boolean daylight, int style, Locale locale) {
393 if (style != SHORT && style != LONG) {
394 throw new IllegalArgumentException("Illegal style: " + style);
398 String[] names = getDisplayNames(id, locale);
400 if (id.startsWith("GMT")) {
401 char sign = id.charAt(3);
402 if (sign == '+' || sign == '-') {
406 int offset = getRawOffset();
408 offset += getDSTSavings();
410 // return ZoneInfoFile.toCustomID(offset);
413 int index = daylight ? 3 : 1;
414 if (style == SHORT) {
420 private static class DisplayNames {
421 // Cache for managing display names per timezone per locale
423 // Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))
424 private static final Map<String, SoftReference<Map<Locale, String[]>>> CACHE =
425 new ConcurrentHashMap<String, SoftReference<Map<Locale, String[]>>>();
428 private static final String[] getDisplayNames(String id, Locale locale) {
429 Map<String, SoftReference<Map<Locale, String[]>>> displayNames = DisplayNames.CACHE;
431 SoftReference<Map<Locale, String[]>> ref = displayNames.get(id);
433 Map<Locale, String[]> perLocale = ref.get();
434 if (perLocale != null) {
435 String[] names = perLocale.get(locale);
439 names = null; // TimeZoneNameUtility.retrieveDisplayNames(id, locale);
441 perLocale.put(locale, names);
447 String[] names = null; // TimeZoneNameUtility.retrieveDisplayNames(id, locale);
449 Map<Locale, String[]> perLocale = new ConcurrentHashMap<Locale, String[]>();
450 perLocale.put(locale, names);
451 ref = new SoftReference<Map<Locale, String[]>>(perLocale);
452 displayNames.put(id, ref);
458 * Returns the amount of time to be added to local standard time
459 * to get local wall clock time.
461 * <p>The default implementation returns 3600000 milliseconds
462 * (i.e., one hour) if a call to {@link #useDaylightTime()}
463 * returns {@code true}. Otherwise, 0 (zero) is returned.
465 * <p>If an underlying {@code TimeZone} implementation subclass
466 * supports historical and future Daylight Saving Time schedule
467 * changes, this method returns the amount of saving time of the
468 * last known Daylight Saving Time rule that can be a future
471 * <p>If the amount of saving time at any given time stamp is
472 * required, construct a {@link Calendar} with this {@code
473 * TimeZone} and the time stamp, and call {@link Calendar#get(int)
474 * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}.
476 * @return the amount of saving time in milliseconds
478 * @see #inDaylightTime(Date)
479 * @see #getOffset(long)
480 * @see #getOffset(int,int,int,int,int,int)
481 * @see Calendar#ZONE_OFFSET
483 public int getDSTSavings() {
484 if (useDaylightTime()) {
491 * Queries if this {@code TimeZone} uses Daylight Saving Time.
493 * <p>If an underlying {@code TimeZone} implementation subclass
494 * supports historical and future Daylight Saving Time schedule
495 * changes, this method refers to the last known Daylight Saving Time
496 * rule that can be a future prediction and may not be the same as
497 * the current rule. Consider calling {@link #observesDaylightTime()}
498 * if the current rule should also be taken into account.
500 * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time,
501 * {@code false}, otherwise.
502 * @see #inDaylightTime(Date)
503 * @see Calendar#DST_OFFSET
505 public abstract boolean useDaylightTime();
508 * Returns {@code true} if this {@code TimeZone} is currently in
509 * Daylight Saving Time, or if a transition from Standard Time to
510 * Daylight Saving Time occurs at any future time.
512 * <p>The default implementation returns {@code true} if
513 * {@code useDaylightTime()} or {@code inDaylightTime(new Date())}
514 * returns {@code true}.
516 * @return {@code true} if this {@code TimeZone} is currently in
517 * Daylight Saving Time, or if a transition from Standard Time to
518 * Daylight Saving Time occurs at any future time; {@code false}
521 * @see #useDaylightTime()
522 * @see #inDaylightTime(Date)
523 * @see Calendar#DST_OFFSET
525 public boolean observesDaylightTime() {
526 return useDaylightTime() || inDaylightTime(new Date());
530 * Queries if the given {@code date} is in Daylight Saving Time in
533 * @param date the given Date.
534 * @return {@code true} if the given date is in Daylight Saving Time,
535 * {@code false}, otherwise.
537 abstract public boolean inDaylightTime(Date date);
540 * Gets the <code>TimeZone</code> for the given ID.
542 * @param ID the ID for a <code>TimeZone</code>, either an abbreviation
543 * such as "PST", a full name such as "America/Los_Angeles", or a custom
544 * ID such as "GMT-8:00". Note that the support of abbreviations is
545 * for JDK 1.1.x compatibility only and full names should be used.
547 * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
548 * cannot be understood.
550 public static synchronized TimeZone getTimeZone(String ID) {
551 return getTimeZone(ID, true);
554 private static TimeZone getTimeZone(String ID, boolean fallback) {
555 // TimeZone tz = ZoneInfo.getTimeZone(ID);
557 // tz = parseCustomTimeZone(ID);
558 // if (tz == null && fallback) {
559 // tz = new ZoneInfo(GMT_ID, 0);
563 return TimeZone.NO_TIMEZONE;
567 * Gets the available IDs according to the given time zone offset in milliseconds.
569 * @param rawOffset the given time zone GMT offset in milliseconds.
570 * @return an array of IDs, where the time zone for that ID has
571 * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
572 * both have GMT-07:00, but differ in daylight saving behavior.
573 * @see #getRawOffset()
575 public static synchronized String[] getAvailableIDs(int rawOffset) {
576 return new String[0];//ZoneInfo.getAvailableIDs(rawOffset);
580 * Gets all the available IDs supported.
581 * @return an array of IDs.
583 public static synchronized String[] getAvailableIDs() {
584 return new String[0];//return ZoneInfo.getAvailableIDs();
588 * Gets the platform defined TimeZone ID.
590 private static native String getSystemTimeZoneID(String javaHome,
594 * Gets the custom time zone ID based on the GMT offset of the
595 * platform. (e.g., "GMT+08:00")
597 private static native String getSystemGMTOffsetID();
600 * Gets the default <code>TimeZone</code> for this host.
601 * The source of the default <code>TimeZone</code>
602 * may vary with implementation.
603 * @return a default <code>TimeZone</code>.
606 public static TimeZone getDefault() {
607 return (TimeZone) getDefaultRef().clone();
611 * Returns the reference to the default TimeZone object. This
612 * method doesn't create a clone.
614 static TimeZone getDefaultRef() {
615 TimeZone defaultZone = null;//defaultZoneTL.get();
616 if (defaultZone == null) {
617 defaultZone = defaultTimeZone;
618 if (defaultZone == null) {
619 // Need to initialize the default time zone.
620 defaultZone = TimeZone.NO_TIMEZONE;
621 assert defaultZone != null;
628 private static boolean hasPermission() {
629 boolean hasPermission = false;
630 return hasPermission;
634 * Sets the <code>TimeZone</code> that is
635 * returned by the <code>getDefault</code> method. If <code>zone</code>
636 * is null, reset the default to the value it had originally when the
638 * @param zone the new default time zone
641 public static void setDefault(TimeZone zone)
643 if (hasPermission()) {
644 synchronized (TimeZone.class) {
645 defaultTimeZone = zone;
646 // defaultZoneTL.set(null);
649 //defaultZoneTL.set(zone);
654 * Returns true if this zone has the same rule and offset as another zone.
655 * That is, if this zone differs only in ID, if at all. Returns false
656 * if the other zone is null.
657 * @param other the <code>TimeZone</code> object to be compared with
658 * @return true if the other zone is not null and is the same as this one,
659 * with the possible exception of the ID
662 public boolean hasSameRules(TimeZone other) {
663 return other != null && getRawOffset() == other.getRawOffset() &&
664 useDaylightTime() == other.useDaylightTime();
668 * Creates a copy of this <code>TimeZone</code>.
670 * @return a clone of this <code>TimeZone</code>
672 public Object clone()
675 TimeZone other = (TimeZone) super.clone();
678 } catch (CloneNotSupportedException e) {
679 throw new InternalError();
684 * The null constant as a TimeZone.
686 static final TimeZone NO_TIMEZONE = null;
688 // =======================privates===============================
691 * The string identifier of this <code>TimeZone</code>. This is a
692 * programmatic identifier used internally to look up <code>TimeZone</code>
693 * objects from the system table and also to map them to their localized
694 * display names. <code>ID</code> values are unique in the system
695 * table but may not be for dynamically created zones.
699 private static volatile TimeZone defaultTimeZone;
701 static final String GMT_ID = "GMT";
702 private static final int GMT_ID_LENGTH = 3;
705 * Parses a custom time zone identifier and returns a corresponding zone.
706 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
708 * @param id a string of the <a href="#CustomID">custom ID form</a>.
709 * @return a newly created TimeZone with the given offset and
710 * no daylight saving time, or null if the id cannot be parsed.
712 private static final TimeZone parseCustomTimeZone(String id) {