author | okutsu |
Wed, 16 Feb 2011 16:51:21 +0900 | |
changeset 8375 | 7b37813b7461 |
parent 7003 | 7d8d9506b4ee |
child 8521 | ab64b8d109b8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
2 |
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - All Rights Reserved |
|
29 |
* |
|
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. |
|
36 |
* |
|
37 |
*/ |
|
38 |
||
39 |
package java.util; |
|
40 |
||
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; |
|
46 |
import sun.security.action.GetPropertyAction; |
|
47 |
import sun.util.TimeZoneNameUtility; |
|
48 |
import sun.util.calendar.ZoneInfo; |
|
49 |
import sun.util.calendar.ZoneInfoFile; |
|
50 |
||
51 |
/** |
|
52 |
* <code>TimeZone</code> represents a time zone offset, and also figures out daylight |
|
53 |
* savings. |
|
54 |
* |
|
55 |
* <p> |
|
56 |
* Typically, you get a <code>TimeZone</code> using <code>getDefault</code> |
|
57 |
* which creates a <code>TimeZone</code> based on the time zone where the program |
|
58 |
* is running. For example, for a program running in Japan, <code>getDefault</code> |
|
59 |
* creates a <code>TimeZone</code> object based on Japanese Standard Time. |
|
60 |
* |
|
61 |
* <p> |
|
62 |
* You can also get a <code>TimeZone</code> using <code>getTimeZone</code> |
|
63 |
* along with a time zone ID. For instance, the time zone ID for the |
|
64 |
* U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a |
|
65 |
* U.S. Pacific Time <code>TimeZone</code> object with: |
|
66 |
* <blockquote><pre> |
|
67 |
* TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); |
|
68 |
* </pre></blockquote> |
|
69 |
* You can use the <code>getAvailableIDs</code> method to iterate through |
|
70 |
* all the supported time zone IDs. You can then choose a |
|
71 |
* supported ID to get a <code>TimeZone</code>. |
|
72 |
* If the time zone you want is not represented by one of the |
|
73 |
* supported IDs, then a custom time zone ID can be specified to |
|
74 |
* produce a TimeZone. The syntax of a custom time zone ID is: |
|
75 |
* |
|
76 |
* <blockquote><pre> |
|
77 |
* <a name="CustomID"><i>CustomID:</i></a> |
|
78 |
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> |
|
79 |
* <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i> |
|
80 |
* <code>GMT</code> <i>Sign</i> <i>Hours</i> |
|
81 |
* <i>Sign:</i> one of |
|
82 |
* <code>+ -</code> |
|
83 |
* <i>Hours:</i> |
|
84 |
* <i>Digit</i> |
|
85 |
* <i>Digit</i> <i>Digit</i> |
|
86 |
* <i>Minutes:</i> |
|
87 |
* <i>Digit</i> <i>Digit</i> |
|
88 |
* <i>Digit:</i> one of |
|
89 |
* <code>0 1 2 3 4 5 6 7 8 9</code> |
|
90 |
* </pre></blockquote> |
|
91 |
* |
|
92 |
* <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be |
|
93 |
* between 00 to 59. For example, "GMT+10" and "GMT+0010" mean ten |
|
94 |
* hours and ten minutes ahead of GMT, respectively. |
|
95 |
* <p> |
|
96 |
* The format is locale independent and digits must be taken from the |
|
97 |
* Basic Latin block of the Unicode standard. No daylight saving time |
|
98 |
* transition schedule can be specified with a custom time zone ID. If |
|
99 |
* the specified string doesn't match the syntax, <code>"GMT"</code> |
|
100 |
* is used. |
|
101 |
* <p> |
|
102 |
* When creating a <code>TimeZone</code>, the specified custom time |
|
103 |
* zone ID is normalized in the following syntax: |
|
104 |
* <blockquote><pre> |
|
105 |
* <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a> |
|
106 |
* <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i> |
|
107 |
* <i>Sign:</i> one of |
|
108 |
* <code>+ -</code> |
|
109 |
* <i>TwoDigitHours:</i> |
|
110 |
* <i>Digit</i> <i>Digit</i> |
|
111 |
* <i>Minutes:</i> |
|
112 |
* <i>Digit</i> <i>Digit</i> |
|
113 |
* <i>Digit:</i> one of |
|
114 |
* <code>0 1 2 3 4 5 6 7 8 9</code> |
|
115 |
* </pre></blockquote> |
|
116 |
* For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00". |
|
117 |
* |
|
118 |
* <h4>Three-letter time zone IDs</h4> |
|
119 |
* |
|
120 |
* For compatibility with JDK 1.1.x, some other three-letter time zone IDs |
|
121 |
* (such as "PST", "CTT", "AST") are also supported. However, <strong>their |
|
122 |
* use is deprecated</strong> because the same abbreviation is often used |
|
123 |
* for multiple time zones (for example, "CST" could be U.S. "Central Standard |
|
124 |
* Time" and "China Standard Time"), and the Java platform can then only |
|
125 |
* recognize one of them. |
|
126 |
* |
|
127 |
* |
|
128 |
* @see Calendar |
|
129 |
* @see GregorianCalendar |
|
130 |
* @see SimpleTimeZone |
|
131 |
* @author Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu |
|
132 |
* @since JDK1.1 |
|
133 |
*/ |
|
134 |
abstract public class TimeZone implements Serializable, Cloneable { |
|
135 |
/** |
|
136 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
137 |
* implicit.) |
|
138 |
*/ |
|
139 |
public TimeZone() { |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* A style specifier for <code>getDisplayName()</code> indicating |
|
144 |
* a short name, such as "PST." |
|
145 |
* @see #LONG |
|
146 |
* @since 1.2 |
|
147 |
*/ |
|
148 |
public static final int SHORT = 0; |
|
149 |
||
150 |
/** |
|
151 |
* A style specifier for <code>getDisplayName()</code> indicating |
|
152 |
* a long name, such as "Pacific Standard Time." |
|
153 |
* @see #SHORT |
|
154 |
* @since 1.2 |
|
155 |
*/ |
|
156 |
public static final int LONG = 1; |
|
157 |
||
158 |
// Constants used internally; unit is milliseconds |
|
159 |
private static final int ONE_MINUTE = 60*1000; |
|
160 |
private static final int ONE_HOUR = 60*ONE_MINUTE; |
|
161 |
private static final int ONE_DAY = 24*ONE_HOUR; |
|
162 |
||
163 |
// Proclaim serialization compatibility with JDK 1.1 |
|
164 |
static final long serialVersionUID = 3581463369166924961L; |
|
165 |
||
166 |
/** |
|
167 |
* Gets the time zone offset, for current date, modified in case of |
|
168 |
* daylight savings. This is the offset to add to UTC to get local time. |
|
169 |
* <p> |
|
170 |
* This method returns a historically correct offset if an |
|
171 |
* underlying <code>TimeZone</code> implementation subclass |
|
172 |
* supports historical Daylight Saving Time schedule and GMT |
|
173 |
* offset changes. |
|
174 |
* |
|
175 |
* @param era the era of the given date. |
|
176 |
* @param year the year in the given date. |
|
177 |
* @param month the month in the given date. |
|
178 |
* Month is 0-based. e.g., 0 for January. |
|
179 |
* @param day the day-in-month of the given date. |
|
180 |
* @param dayOfWeek the day-of-week of the given date. |
|
181 |
* @param milliseconds the milliseconds in day in <em>standard</em> |
|
182 |
* local time. |
|
183 |
* |
|
184 |
* @return the offset in milliseconds to add to GMT to get local time. |
|
185 |
* |
|
186 |
* @see Calendar#ZONE_OFFSET |
|
187 |
* @see Calendar#DST_OFFSET |
|
188 |
*/ |
|
189 |
public abstract int getOffset(int era, int year, int month, int day, |
|
190 |
int dayOfWeek, int milliseconds); |
|
191 |
||
192 |
/** |
|
193 |
* Returns the offset of this time zone from UTC at the specified |
|
194 |
* date. If Daylight Saving Time is in effect at the specified |
|
195 |
* date, the offset value is adjusted with the amount of daylight |
|
196 |
* saving. |
|
197 |
* <p> |
|
198 |
* This method returns a historically correct offset value if an |
|
199 |
* underlying TimeZone implementation subclass supports historical |
|
200 |
* Daylight Saving Time schedule and GMT offset changes. |
|
201 |
* |
|
202 |
* @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT |
|
203 |
* @return the amount of time in milliseconds to add to UTC to get local time. |
|
204 |
* |
|
205 |
* @see Calendar#ZONE_OFFSET |
|
206 |
* @see Calendar#DST_OFFSET |
|
207 |
* @since 1.4 |
|
208 |
*/ |
|
209 |
public int getOffset(long date) { |
|
210 |
if (inDaylightTime(new Date(date))) { |
|
211 |
return getRawOffset() + getDSTSavings(); |
|
212 |
} |
|
213 |
return getRawOffset(); |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Gets the raw GMT offset and the amount of daylight saving of this |
|
218 |
* time zone at the given time. |
|
219 |
* @param date the milliseconds (since January 1, 1970, |
|
220 |
* 00:00:00.000 GMT) at which the time zone offset and daylight |
|
221 |
* saving amount are found |
|
222 |
* @param offset an array of int where the raw GMT offset |
|
223 |
* (offset[0]) and daylight saving amount (offset[1]) are stored, |
|
224 |
* or null if those values are not needed. The method assumes that |
|
225 |
* the length of the given array is two or larger. |
|
226 |
* @return the total amount of the raw GMT offset and daylight |
|
227 |
* saving at the specified date. |
|
228 |
* |
|
229 |
* @see Calendar#ZONE_OFFSET |
|
230 |
* @see Calendar#DST_OFFSET |
|
231 |
*/ |
|
232 |
int getOffsets(long date, int[] offsets) { |
|
233 |
int rawoffset = getRawOffset(); |
|
234 |
int dstoffset = 0; |
|
235 |
if (inDaylightTime(new Date(date))) { |
|
236 |
dstoffset = getDSTSavings(); |
|
237 |
} |
|
238 |
if (offsets != null) { |
|
239 |
offsets[0] = rawoffset; |
|
240 |
offsets[1] = dstoffset; |
|
241 |
} |
|
242 |
return rawoffset + dstoffset; |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* Sets the base time zone offset to GMT. |
|
247 |
* This is the offset to add to UTC to get local time. |
|
248 |
* <p> |
|
249 |
* If an underlying <code>TimeZone</code> implementation subclass |
|
250 |
* supports historical GMT offset changes, the specified GMT |
|
251 |
* offset is set as the latest GMT offset and the difference from |
|
252 |
* the known latest GMT offset value is used to adjust all |
|
253 |
* historical GMT offset values. |
|
254 |
* |
|
255 |
* @param offsetMillis the given base time zone offset to GMT. |
|
256 |
*/ |
|
257 |
abstract public void setRawOffset(int offsetMillis); |
|
258 |
||
259 |
/** |
|
260 |
* Returns the amount of time in milliseconds to add to UTC to get |
|
261 |
* standard time in this time zone. Because this value is not |
|
262 |
* affected by daylight saving time, it is called <I>raw |
|
263 |
* offset</I>. |
|
264 |
* <p> |
|
265 |
* If an underlying <code>TimeZone</code> implementation subclass |
|
266 |
* supports historical GMT offset changes, the method returns the |
|
267 |
* raw offset value of the current date. In Honolulu, for example, |
|
268 |
* its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and |
|
269 |
* this method always returns -36000000 milliseconds (i.e., -10 |
|
270 |
* hours). |
|
271 |
* |
|
272 |
* @return the amount of raw offset time in milliseconds to add to UTC. |
|
273 |
* @see Calendar#ZONE_OFFSET |
|
274 |
*/ |
|
275 |
public abstract int getRawOffset(); |
|
276 |
||
277 |
/** |
|
278 |
* Gets the ID of this time zone. |
|
279 |
* @return the ID of this time zone. |
|
280 |
*/ |
|
281 |
public String getID() |
|
282 |
{ |
|
283 |
return ID; |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Sets the time zone ID. This does not change any other data in |
|
288 |
* the time zone object. |
|
289 |
* @param ID the new time zone ID. |
|
290 |
*/ |
|
291 |
public void setID(String ID) |
|
292 |
{ |
|
293 |
if (ID == null) { |
|
294 |
throw new NullPointerException(); |
|
295 |
} |
|
296 |
this.ID = ID; |
|
297 |
} |
|
298 |
||
299 |
/** |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
300 |
* Returns a long standard time name of this {@code TimeZone} suitable for |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
301 |
* presentation to the user in the default locale. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
302 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
303 |
* <p>This method is equivalent to: |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
304 |
* <pre><blockquote> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
305 |
* getDisplayName(false, {@link #LONG}, |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
306 |
* Locale.getDefault({@link Locale.Category#DISPLAY})) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
307 |
* </blockquote></pre> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
308 |
* |
2 | 309 |
* @return the human-readable name of this time zone in the default locale. |
310 |
* @since 1.2 |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
311 |
* @see #getDisplayName(boolean, int, Locale) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
312 |
* @see Locale#getDefault(Locale.Category) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
313 |
* @see Locale.Category |
2 | 314 |
*/ |
315 |
public final String getDisplayName() { |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
316 |
return getDisplayName(false, LONG, |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
317 |
Locale.getDefault(Locale.Category.DISPLAY)); |
2 | 318 |
} |
319 |
||
320 |
/** |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
321 |
* Returns a long standard time name of this {@code TimeZone} suitable for |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
322 |
* presentation to the user in the specified {@code locale}. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
323 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
324 |
* <p>This method is equivalent to: |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
325 |
* <pre><blockquote> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
326 |
* getDisplayName(false, {@link #LONG}, locale) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
327 |
* </blockquote></pre> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
328 |
* |
2 | 329 |
* @param locale the locale in which to supply the display name. |
330 |
* @return the human-readable name of this time zone in the given locale. |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
331 |
* @exception NullPointerException if {@code locale} is {@code null}. |
2 | 332 |
* @since 1.2 |
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
333 |
* @see #getDisplayName(boolean, int, Locale) |
2 | 334 |
*/ |
335 |
public final String getDisplayName(Locale locale) { |
|
336 |
return getDisplayName(false, LONG, locale); |
|
337 |
} |
|
338 |
||
339 |
/** |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
340 |
* Returns a name in the specified {@code style} of this {@code TimeZone} |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
341 |
* suitable for presentation to the user in the default locale. If the |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
342 |
* specified {@code daylight} is {@code true}, a daylight saving time name |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
343 |
* is returned. Otherwise, a standard time name is returned. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
344 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
345 |
* <p>This method is equivalent to: |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
346 |
* <pre><blockquote> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
347 |
* getDisplayName(daylight, style, |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
348 |
* Locale.getDefault({@link Locale.Category#DISPLAY})) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
349 |
* </blockquote></pre> |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
350 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
351 |
* @param daylight if {@code true}, return the daylight saving time name. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
352 |
* @param style either {@link #LONG} or {@link #SHORT} |
2 | 353 |
* @return the human-readable name of this time zone in the default locale. |
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
354 |
* @exception IllegalArgumentException if {@code style} is invalid. |
2 | 355 |
* @since 1.2 |
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
356 |
* @see #getDisplayName(boolean, int, Locale) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
357 |
* @see Locale#getDefault(Locale.Category) |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
358 |
* @see Locale.Category |
2 | 359 |
*/ |
360 |
public final String getDisplayName(boolean daylight, int style) { |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
361 |
return getDisplayName(daylight, style, |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
362 |
Locale.getDefault(Locale.Category.DISPLAY)); |
2 | 363 |
} |
364 |
||
365 |
/** |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
366 |
* Returns a name in the specified {@code style} of this {@code TimeZone} |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
367 |
* suitable for presentation to the user in the specified {@code |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
368 |
* locale}. If the specified {@code daylight} is {@code true}, a daylight |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
369 |
* saving time name is returned. Otherwise, a standard time name is |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
370 |
* returned. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
371 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
372 |
* <p>When looking up a time zone name, the {@linkplain |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
373 |
* ResourceBundle.Control#getCandidateLocales(String,Locale) default |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
374 |
* <code>Locale</code> search path of <code>ResourceBundle</code>} derived |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
375 |
* from the specified {@code locale} is used. (No {@linkplain |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
376 |
* ResourceBundle.Control#getFallbackLocale(String,Locale) fallback |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
377 |
* <code>Locale</code>} search is performed.) If a time zone name in any |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
378 |
* {@code Locale} of the search path, including {@link Locale#ROOT}, is |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
379 |
* found, the name is returned. Otherwise, a string in the |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
380 |
* <a href="#NormalizedCustomID">normalized custom ID format</a> is returned. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
381 |
* |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
382 |
* @param daylight if {@code true}, return the daylight saving time name. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
383 |
* @param style either {@link #LONG} or {@link #SHORT} |
2 | 384 |
* @param locale the locale in which to supply the display name. |
385 |
* @return the human-readable name of this time zone in the given locale. |
|
6841
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
386 |
* @exception IllegalArgumentException if {@code style} is invalid. |
3a4f3e399917
6638110: (tz) TimeZone.getDisplayName(...) spec is inconsistent with implementation for unavailable locales
okutsu
parents:
6489
diff
changeset
|
387 |
* @exception NullPointerException if {@code locale} is {@code null}. |
2 | 388 |
* @since 1.2 |
389 |
*/ |
|
390 |
public String getDisplayName(boolean daylight, int style, Locale locale) { |
|
391 |
if (style != SHORT && style != LONG) { |
|
392 |
throw new IllegalArgumentException("Illegal style: " + style); |
|
393 |
} |
|
394 |
||
395 |
String id = getID(); |
|
396 |
String[] names = getDisplayNames(id, locale); |
|
397 |
if (names == null) { |
|
398 |
if (id.startsWith("GMT")) { |
|
399 |
char sign = id.charAt(3); |
|
400 |
if (sign == '+' || sign == '-') { |
|
401 |
return id; |
|
402 |
} |
|
403 |
} |
|
404 |
int offset = getRawOffset(); |
|
405 |
if (daylight) { |
|
406 |
offset += getDSTSavings(); |
|
407 |
} |
|
408 |
return ZoneInfoFile.toCustomID(offset); |
|
409 |
} |
|
410 |
||
411 |
int index = daylight ? 3 : 1; |
|
412 |
if (style == SHORT) { |
|
413 |
index++; |
|
414 |
} |
|
415 |
return names[index]; |
|
416 |
} |
|
417 |
||
418 |
private static class DisplayNames { |
|
419 |
// Cache for managing display names per timezone per locale |
|
420 |
// The structure is: |
|
421 |
// Map(key=id, value=SoftReference(Map(key=locale, value=displaynames))) |
|
422 |
private static final Map<String, SoftReference<Map<Locale, String[]>>> CACHE = |
|
423 |
new ConcurrentHashMap<String, SoftReference<Map<Locale, String[]>>>(); |
|
424 |
} |
|
425 |
||
426 |
private static final String[] getDisplayNames(String id, Locale locale) { |
|
427 |
Map<String, SoftReference<Map<Locale, String[]>>> displayNames = DisplayNames.CACHE; |
|
428 |
||
429 |
SoftReference<Map<Locale, String[]>> ref = displayNames.get(id); |
|
430 |
if (ref != null) { |
|
431 |
Map<Locale, String[]> perLocale = ref.get(); |
|
432 |
if (perLocale != null) { |
|
433 |
String[] names = perLocale.get(locale); |
|
434 |
if (names != null) { |
|
435 |
return names; |
|
436 |
} |
|
437 |
names = TimeZoneNameUtility.retrieveDisplayNames(id, locale); |
|
438 |
if (names != null) { |
|
439 |
perLocale.put(locale, names); |
|
440 |
} |
|
441 |
return names; |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
String[] names = TimeZoneNameUtility.retrieveDisplayNames(id, locale); |
|
446 |
if (names != null) { |
|
447 |
Map<Locale, String[]> perLocale = new ConcurrentHashMap<Locale, String[]>(); |
|
448 |
perLocale.put(locale, names); |
|
449 |
ref = new SoftReference<Map<Locale, String[]>>(perLocale); |
|
450 |
displayNames.put(id, ref); |
|
451 |
} |
|
452 |
return names; |
|
453 |
} |
|
454 |
||
455 |
/** |
|
456 |
* Returns the amount of time to be added to local standard time |
|
457 |
* to get local wall clock time. |
|
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
458 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
459 |
* <p>The default implementation returns 3600000 milliseconds |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
460 |
* (i.e., one hour) if a call to {@link #useDaylightTime()} |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
461 |
* returns {@code true}. Otherwise, 0 (zero) is returned. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
462 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
463 |
* <p>If an underlying {@code TimeZone} implementation subclass |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
464 |
* supports historical and future Daylight Saving Time schedule |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
465 |
* changes, this method returns the amount of saving time of the |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
466 |
* last known Daylight Saving Time rule that can be a future |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
467 |
* prediction. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
468 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
469 |
* <p>If the amount of saving time at any given time stamp is |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
470 |
* required, construct a {@link Calendar} with this {@code |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
471 |
* TimeZone} and the time stamp, and call {@link Calendar#get(int) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
472 |
* Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}. |
2 | 473 |
* |
474 |
* @return the amount of saving time in milliseconds |
|
475 |
* @since 1.4 |
|
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
476 |
* @see #inDaylightTime(Date) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
477 |
* @see #getOffset(long) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
478 |
* @see #getOffset(int,int,int,int,int,int) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
479 |
* @see Calendar#ZONE_OFFSET |
2 | 480 |
*/ |
481 |
public int getDSTSavings() { |
|
482 |
if (useDaylightTime()) { |
|
483 |
return 3600000; |
|
484 |
} |
|
485 |
return 0; |
|
486 |
} |
|
487 |
||
488 |
/** |
|
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
489 |
* Queries if this {@code TimeZone} uses Daylight Saving Time. |
2 | 490 |
* |
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
491 |
* <p>If an underlying {@code TimeZone} implementation subclass |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
492 |
* supports historical and future Daylight Saving Time schedule |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
493 |
* changes, this method refers to the last known Daylight Saving Time |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
494 |
* rule that can be a future prediction and may not be the same as |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
495 |
* the current rule. Consider calling {@link #observesDaylightTime()} |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
496 |
* if the current rule should also be taken into account. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
497 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
498 |
* @return {@code true} if this {@code TimeZone} uses Daylight Saving Time, |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
499 |
* {@code false}, otherwise. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
500 |
* @see #inDaylightTime(Date) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
501 |
* @see Calendar#DST_OFFSET |
2 | 502 |
*/ |
503 |
public abstract boolean useDaylightTime(); |
|
504 |
||
505 |
/** |
|
8375
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
506 |
* Returns {@code true} if this {@code TimeZone} is currently in |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
507 |
* Daylight Saving Time, or if a transition from Standard Time to |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
508 |
* Daylight Saving Time occurs at any future time. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
509 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
510 |
* <p>The default implementation returns {@code true} if |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
511 |
* {@code useDaylightTime()} or {@code inDaylightTime(new Date())} |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
512 |
* returns {@code true}. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
513 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
514 |
* @return {@code true} if this {@code TimeZone} is currently in |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
515 |
* Daylight Saving Time, or if a transition from Standard Time to |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
516 |
* Daylight Saving Time occurs at any future time; {@code false} |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
517 |
* otherwise. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
518 |
* @since 1.7 |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
519 |
* @see #useDaylightTime() |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
520 |
* @see #inDaylightTime(Date) |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
521 |
* @see Calendar#DST_OFFSET |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
522 |
*/ |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
523 |
public boolean observesDaylightTime() { |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
524 |
return useDaylightTime() || inDaylightTime(new Date()); |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
525 |
} |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
526 |
|
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
527 |
/** |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
528 |
* Queries if the given {@code date} is in Daylight Saving Time in |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
529 |
* this {@code TimeZone}. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
530 |
* |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
531 |
* @param date the given {@code Date}. |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
532 |
* @return {@code true} if the given {@code date} is in Daylight Saving Time, |
7b37813b7461
6936350: API clarification needed on useDaylightTime() for timezones that have defined usage dates
okutsu
parents:
7003
diff
changeset
|
533 |
* {@code false}, otherwise. |
2 | 534 |
*/ |
535 |
abstract public boolean inDaylightTime(Date date); |
|
536 |
||
537 |
/** |
|
538 |
* Gets the <code>TimeZone</code> for the given ID. |
|
539 |
* |
|
540 |
* @param ID the ID for a <code>TimeZone</code>, either an abbreviation |
|
541 |
* such as "PST", a full name such as "America/Los_Angeles", or a custom |
|
542 |
* ID such as "GMT-8:00". Note that the support of abbreviations is |
|
543 |
* for JDK 1.1.x compatibility only and full names should be used. |
|
544 |
* |
|
545 |
* @return the specified <code>TimeZone</code>, or the GMT zone if the given ID |
|
546 |
* cannot be understood. |
|
547 |
*/ |
|
548 |
public static synchronized TimeZone getTimeZone(String ID) { |
|
549 |
return getTimeZone(ID, true); |
|
550 |
} |
|
551 |
||
552 |
private static TimeZone getTimeZone(String ID, boolean fallback) { |
|
553 |
TimeZone tz = ZoneInfo.getTimeZone(ID); |
|
554 |
if (tz == null) { |
|
555 |
tz = parseCustomTimeZone(ID); |
|
556 |
if (tz == null && fallback) { |
|
557 |
tz = new ZoneInfo(GMT_ID, 0); |
|
558 |
} |
|
559 |
} |
|
560 |
return tz; |
|
561 |
} |
|
562 |
||
563 |
/** |
|
564 |
* Gets the available IDs according to the given time zone offset in milliseconds. |
|
565 |
* |
|
566 |
* @param rawOffset the given time zone GMT offset in milliseconds. |
|
567 |
* @return an array of IDs, where the time zone for that ID has |
|
568 |
* the specified GMT offset. For example, "America/Phoenix" and "America/Denver" |
|
569 |
* both have GMT-07:00, but differ in daylight savings behavior. |
|
570 |
* @see #getRawOffset() |
|
571 |
*/ |
|
572 |
public static synchronized String[] getAvailableIDs(int rawOffset) { |
|
573 |
return ZoneInfo.getAvailableIDs(rawOffset); |
|
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* Gets all the available IDs supported. |
|
578 |
* @return an array of IDs. |
|
579 |
*/ |
|
580 |
public static synchronized String[] getAvailableIDs() { |
|
581 |
return ZoneInfo.getAvailableIDs(); |
|
582 |
} |
|
583 |
||
584 |
/** |
|
585 |
* Gets the platform defined TimeZone ID. |
|
586 |
**/ |
|
587 |
private static native String getSystemTimeZoneID(String javaHome, |
|
588 |
String country); |
|
589 |
||
590 |
/** |
|
591 |
* Gets the custom time zone ID based on the GMT offset of the |
|
592 |
* platform. (e.g., "GMT+08:00") |
|
593 |
*/ |
|
594 |
private static native String getSystemGMTOffsetID(); |
|
595 |
||
596 |
/** |
|
597 |
* Gets the default <code>TimeZone</code> for this host. |
|
598 |
* The source of the default <code>TimeZone</code> |
|
599 |
* may vary with implementation. |
|
600 |
* @return a default <code>TimeZone</code>. |
|
601 |
* @see #setDefault |
|
602 |
*/ |
|
603 |
public static TimeZone getDefault() { |
|
604 |
return (TimeZone) getDefaultRef().clone(); |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* Returns the reference to the default TimeZone object. This |
|
609 |
* method doesn't create a clone. |
|
610 |
*/ |
|
611 |
static TimeZone getDefaultRef() { |
|
612 |
TimeZone defaultZone = defaultZoneTL.get(); |
|
613 |
if (defaultZone == null) { |
|
614 |
defaultZone = defaultTimeZone; |
|
615 |
if (defaultZone == null) { |
|
616 |
// Need to initialize the default time zone. |
|
617 |
defaultZone = setDefaultZone(); |
|
618 |
assert defaultZone != null; |
|
619 |
} |
|
620 |
} |
|
621 |
// Don't clone here. |
|
622 |
return defaultZone; |
|
623 |
} |
|
624 |
||
625 |
private static synchronized TimeZone setDefaultZone() { |
|
626 |
TimeZone tz = null; |
|
627 |
// get the time zone ID from the system properties |
|
628 |
String zoneID = AccessController.doPrivileged( |
|
629 |
new GetPropertyAction("user.timezone")); |
|
630 |
||
631 |
// if the time zone ID is not set (yet), perform the |
|
632 |
// platform to Java time zone ID mapping. |
|
633 |
if (zoneID == null || zoneID.equals("")) { |
|
634 |
String country = AccessController.doPrivileged( |
|
635 |
new GetPropertyAction("user.country")); |
|
636 |
String javaHome = AccessController.doPrivileged( |
|
637 |
new GetPropertyAction("java.home")); |
|
638 |
try { |
|
639 |
zoneID = getSystemTimeZoneID(javaHome, country); |
|
640 |
if (zoneID == null) { |
|
641 |
zoneID = GMT_ID; |
|
642 |
} |
|
643 |
} catch (NullPointerException e) { |
|
644 |
zoneID = GMT_ID; |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
// Get the time zone for zoneID. But not fall back to |
|
649 |
// "GMT" here. |
|
650 |
tz = getTimeZone(zoneID, false); |
|
651 |
||
652 |
if (tz == null) { |
|
653 |
// If the given zone ID is unknown in Java, try to |
|
654 |
// get the GMT-offset-based time zone ID, |
|
655 |
// a.k.a. custom time zone ID (e.g., "GMT-08:00"). |
|
656 |
String gmtOffsetID = getSystemGMTOffsetID(); |
|
657 |
if (gmtOffsetID != null) { |
|
658 |
zoneID = gmtOffsetID; |
|
659 |
} |
|
660 |
tz = getTimeZone(zoneID, true); |
|
661 |
} |
|
662 |
assert tz != null; |
|
663 |
||
664 |
final String id = zoneID; |
|
665 |
AccessController.doPrivileged(new PrivilegedAction<Object>() { |
|
666 |
public Object run() { |
|
667 |
System.setProperty("user.timezone", id); |
|
668 |
return null; |
|
669 |
} |
|
670 |
}); |
|
671 |
||
672 |
defaultTimeZone = tz; |
|
673 |
return tz; |
|
674 |
} |
|
675 |
||
676 |
private static boolean hasPermission() { |
|
677 |
boolean hasPermission = true; |
|
678 |
SecurityManager sm = System.getSecurityManager(); |
|
679 |
if (sm != null) { |
|
680 |
try { |
|
681 |
sm.checkPermission(new PropertyPermission |
|
682 |
("user.timezone", "write")); |
|
683 |
} catch (SecurityException e) { |
|
684 |
hasPermission = false; |
|
685 |
} |
|
686 |
} |
|
687 |
return hasPermission; |
|
688 |
} |
|
689 |
||
690 |
/** |
|
691 |
* Sets the <code>TimeZone</code> that is |
|
692 |
* returned by the <code>getDefault</code> method. If <code>zone</code> |
|
693 |
* is null, reset the default to the value it had originally when the |
|
694 |
* VM first started. |
|
695 |
* @param zone the new default time zone |
|
696 |
* @see #getDefault |
|
697 |
*/ |
|
698 |
public static void setDefault(TimeZone zone) |
|
699 |
{ |
|
700 |
if (hasPermission()) { |
|
701 |
synchronized (TimeZone.class) { |
|
702 |
defaultTimeZone = zone; |
|
703 |
defaultZoneTL.set(null); |
|
704 |
} |
|
705 |
} else { |
|
706 |
defaultZoneTL.set(zone); |
|
707 |
} |
|
708 |
} |
|
709 |
||
710 |
/** |
|
711 |
* Returns true if this zone has the same rule and offset as another zone. |
|
712 |
* That is, if this zone differs only in ID, if at all. Returns false |
|
713 |
* if the other zone is null. |
|
714 |
* @param other the <code>TimeZone</code> object to be compared with |
|
715 |
* @return true if the other zone is not null and is the same as this one, |
|
716 |
* with the possible exception of the ID |
|
717 |
* @since 1.2 |
|
718 |
*/ |
|
719 |
public boolean hasSameRules(TimeZone other) { |
|
720 |
return other != null && getRawOffset() == other.getRawOffset() && |
|
721 |
useDaylightTime() == other.useDaylightTime(); |
|
722 |
} |
|
723 |
||
724 |
/** |
|
725 |
* Creates a copy of this <code>TimeZone</code>. |
|
726 |
* |
|
727 |
* @return a clone of this <code>TimeZone</code> |
|
728 |
*/ |
|
729 |
public Object clone() |
|
730 |
{ |
|
731 |
try { |
|
732 |
TimeZone other = (TimeZone) super.clone(); |
|
733 |
other.ID = ID; |
|
734 |
return other; |
|
735 |
} catch (CloneNotSupportedException e) { |
|
736 |
throw new InternalError(); |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
/** |
|
741 |
* The null constant as a TimeZone. |
|
742 |
*/ |
|
743 |
static final TimeZone NO_TIMEZONE = null; |
|
744 |
||
745 |
// =======================privates=============================== |
|
746 |
||
747 |
/** |
|
748 |
* The string identifier of this <code>TimeZone</code>. This is a |
|
749 |
* programmatic identifier used internally to look up <code>TimeZone</code> |
|
750 |
* objects from the system table and also to map them to their localized |
|
751 |
* display names. <code>ID</code> values are unique in the system |
|
752 |
* table but may not be for dynamically created zones. |
|
753 |
* @serial |
|
754 |
*/ |
|
755 |
private String ID; |
|
756 |
private static volatile TimeZone defaultTimeZone; |
|
757 |
private static final InheritableThreadLocal<TimeZone> defaultZoneTL |
|
758 |
= new InheritableThreadLocal<TimeZone>(); |
|
759 |
||
760 |
static final String GMT_ID = "GMT"; |
|
761 |
private static final int GMT_ID_LENGTH = 3; |
|
762 |
||
763 |
/** |
|
764 |
* Parses a custom time zone identifier and returns a corresponding zone. |
|
765 |
* This method doesn't support the RFC 822 time zone format. (e.g., +hhmm) |
|
766 |
* |
|
767 |
* @param id a string of the <a href="#CustomID">custom ID form</a>. |
|
768 |
* @return a newly created TimeZone with the given offset and |
|
769 |
* no daylight saving time, or null if the id cannot be parsed. |
|
770 |
*/ |
|
771 |
private static final TimeZone parseCustomTimeZone(String id) { |
|
772 |
int length; |
|
773 |
||
774 |
// Error if the length of id isn't long enough or id doesn't |
|
775 |
// start with "GMT". |
|
776 |
if ((length = id.length()) < (GMT_ID_LENGTH + 2) || |
|
777 |
id.indexOf(GMT_ID) != 0) { |
|
778 |
return null; |
|
779 |
} |
|
780 |
||
781 |
ZoneInfo zi; |
|
782 |
||
783 |
// First, we try to find it in the cache with the given |
|
784 |
// id. Even the id is not normalized, the returned ZoneInfo |
|
785 |
// should have its normalized id. |
|
786 |
zi = ZoneInfoFile.getZoneInfo(id); |
|
787 |
if (zi != null) { |
|
788 |
return zi; |
|
789 |
} |
|
790 |
||
791 |
int index = GMT_ID_LENGTH; |
|
792 |
boolean negative = false; |
|
793 |
char c = id.charAt(index++); |
|
794 |
if (c == '-') { |
|
795 |
negative = true; |
|
796 |
} else if (c != '+') { |
|
797 |
return null; |
|
798 |
} |
|
799 |
||
800 |
int hours = 0; |
|
801 |
int num = 0; |
|
802 |
int countDelim = 0; |
|
803 |
int len = 0; |
|
804 |
while (index < length) { |
|
805 |
c = id.charAt(index++); |
|
806 |
if (c == ':') { |
|
807 |
if (countDelim > 0) { |
|
808 |
return null; |
|
809 |
} |
|
810 |
if (len > 2) { |
|
811 |
return null; |
|
812 |
} |
|
813 |
hours = num; |
|
814 |
countDelim++; |
|
815 |
num = 0; |
|
816 |
len = 0; |
|
817 |
continue; |
|
818 |
} |
|
819 |
if (c < '0' || c > '9') { |
|
820 |
return null; |
|
821 |
} |
|
822 |
num = num * 10 + (c - '0'); |
|
823 |
len++; |
|
824 |
} |
|
825 |
if (index != length) { |
|
826 |
return null; |
|
827 |
} |
|
828 |
if (countDelim == 0) { |
|
829 |
if (len <= 2) { |
|
830 |
hours = num; |
|
831 |
num = 0; |
|
832 |
} else { |
|
833 |
hours = num / 100; |
|
834 |
num %= 100; |
|
835 |
} |
|
836 |
} else { |
|
837 |
if (len != 2) { |
|
838 |
return null; |
|
839 |
} |
|
840 |
} |
|
841 |
if (hours > 23 || num > 59) { |
|
842 |
return null; |
|
843 |
} |
|
844 |
int gmtOffset = (hours * 60 + num) * 60 * 1000; |
|
845 |
||
846 |
if (gmtOffset == 0) { |
|
847 |
zi = ZoneInfoFile.getZoneInfo(GMT_ID); |
|
848 |
if (negative) { |
|
849 |
zi.setID("GMT-00:00"); |
|
850 |
} else { |
|
851 |
zi.setID("GMT+00:00"); |
|
852 |
} |
|
853 |
} else { |
|
854 |
zi = ZoneInfoFile.getCustomTimeZone(id, negative ? -gmtOffset : gmtOffset); |
|
855 |
} |
|
856 |
return zi; |
|
857 |
} |
|
858 |
} |