Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/text/SimpleDateFormat.java
+++ new/src/share/classes/java/text/SimpleDateFormat.java
1 1 /*
2 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 /*
27 27 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28 28 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
29 29 *
30 30 * The original version of this source code and documentation is copyrighted
31 31 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32 32 * materials are provided under terms of a License Agreement between Taligent
33 33 * and Sun. This technology is protected by multiple US and International
34 34 * patents. This notice and attribution to Taligent may not be removed.
35 35 * Taligent is a registered trademark of Taligent, Inc.
36 36 *
37 37 */
38 38
39 39 package java.text;
40 40
41 41 import java.io.IOException;
42 42 import java.io.InvalidObjectException;
43 43 import java.io.ObjectInputStream;
44 44 import static java.text.DateFormatSymbols.*;
45 45 import java.util.Calendar;
46 46 import java.util.Date;
47 47 import java.util.GregorianCalendar;
48 48 import java.util.Locale;
49 49 import java.util.Map;
50 50 import java.util.SimpleTimeZone;
51 51 import java.util.SortedMap;
52 52 import java.util.TimeZone;
53 53 import java.util.concurrent.ConcurrentHashMap;
54 54 import java.util.concurrent.ConcurrentMap;
55 55 import sun.util.calendar.CalendarUtils;
56 56 import sun.util.calendar.ZoneInfoFile;
57 57 import sun.util.locale.provider.LocaleProviderAdapter;
58 58
59 59 /**
60 60 * <code>SimpleDateFormat</code> is a concrete class for formatting and
61 61 * parsing dates in a locale-sensitive manner. It allows for formatting
62 62 * (date → text), parsing (text → date), and normalization.
63 63 *
64 64 * <p>
65 65 * <code>SimpleDateFormat</code> allows you to start by choosing
66 66 * any user-defined patterns for date-time formatting. However, you
67 67 * are encouraged to create a date-time formatter with either
68 68 * <code>getTimeInstance</code>, <code>getDateInstance</code>, or
69 69 * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each
70 70 * of these class methods can return a date/time formatter initialized
71 71 * with a default format pattern. You may modify the format pattern
72 72 * using the <code>applyPattern</code> methods as desired.
73 73 * For more information on using these methods, see
74 74 * {@link DateFormat}.
75 75 *
76 76 * <h3>Date and Time Patterns</h3>
77 77 * <p>
78 78 * Date and time formats are specified by <em>date and time pattern</em>
79 79 * strings.
80 80 * Within date and time pattern strings, unquoted letters from
81 81 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
82 82 * <code>'z'</code> are interpreted as pattern letters representing the
83 83 * components of a date or time string.
84 84 * Text can be quoted using single quotes (<code>'</code>) to avoid
85 85 * interpretation.
86 86 * <code>"''"</code> represents a single quote.
87 87 * All other characters are not interpreted; they're simply copied into the
88 88 * output string during formatting or matched against the input string
89 89 * during parsing.
90 90 * <p>
91 91 * The following pattern letters are defined (all other characters from
92 92 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
93 93 * <code>'z'</code> are reserved):
94 94 * <blockquote>
95 95 * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
96 96 * <tr style="background-color: rgb(204, 204, 255);">
97 97 * <th align=left>Letter
98 98 * <th align=left>Date or Time Component
99 99 * <th align=left>Presentation
100 100 * <th align=left>Examples
101 101 * <tr>
102 102 * <td><code>G</code>
103 103 * <td>Era designator
104 104 * <td><a href="#text">Text</a>
105 105 * <td><code>AD</code>
106 106 * <tr style="background-color: rgb(238, 238, 255);">
107 107 * <td><code>y</code>
108 108 * <td>Year
109 109 * <td><a href="#year">Year</a>
110 110 * <td><code>1996</code>; <code>96</code>
111 111 * <tr>
112 112 * <td><code>Y</code>
113 113 * <td>Week year
114 114 * <td><a href="#year">Year</a>
115 115 * <td><code>2009</code>; <code>09</code>
116 116 * <tr style="background-color: rgb(238, 238, 255);">
117 117 * <td><code>M</code>
118 118 * <td>Month in year (context sensitive)
119 119 * <td><a href="#month">Month</a>
120 120 * <td><code>July</code>; <code>Jul</code>; <code>07</code>
121 121 * <tr>
122 122 * <td><code>L</code>
123 123 * <td>Month in year (standalone form)
124 124 * <td><a href="#month">Month</a>
125 125 * <td><code>July</code>; <code>Jul</code>; <code>07</code>
126 126 * <tr style="background-color: rgb(238, 238, 255);">
127 127 * <td><code>w</code>
128 128 * <td>Week in year
129 129 * <td><a href="#number">Number</a>
130 130 * <td><code>27</code>
131 131 * <tr>
132 132 * <td><code>W</code>
133 133 * <td>Week in month
134 134 * <td><a href="#number">Number</a>
135 135 * <td><code>2</code>
136 136 * <tr style="background-color: rgb(238, 238, 255);">
137 137 * <td><code>D</code>
138 138 * <td>Day in year
139 139 * <td><a href="#number">Number</a>
140 140 * <td><code>189</code>
141 141 * <tr>
142 142 * <td><code>d</code>
143 143 * <td>Day in month
144 144 * <td><a href="#number">Number</a>
145 145 * <td><code>10</code>
146 146 * <tr style="background-color: rgb(238, 238, 255);">
147 147 * <td><code>F</code>
148 148 * <td>Day of week in month
149 149 * <td><a href="#number">Number</a>
150 150 * <td><code>2</code>
151 151 * <tr>
152 152 * <td><code>E</code>
153 153 * <td>Day name in week
154 154 * <td><a href="#text">Text</a>
155 155 * <td><code>Tuesday</code>; <code>Tue</code>
156 156 * <tr style="background-color: rgb(238, 238, 255);">
157 157 * <td><code>u</code>
158 158 * <td>Day number of week (1 = Monday, ..., 7 = Sunday)
159 159 * <td><a href="#number">Number</a>
160 160 * <td><code>1</code>
161 161 * <tr>
162 162 * <td><code>a</code>
163 163 * <td>Am/pm marker
164 164 * <td><a href="#text">Text</a>
165 165 * <td><code>PM</code>
166 166 * <tr style="background-color: rgb(238, 238, 255);">
167 167 * <td><code>H</code>
168 168 * <td>Hour in day (0-23)
169 169 * <td><a href="#number">Number</a>
170 170 * <td><code>0</code>
171 171 * <tr>
172 172 * <td><code>k</code>
173 173 * <td>Hour in day (1-24)
174 174 * <td><a href="#number">Number</a>
175 175 * <td><code>24</code>
176 176 * <tr style="background-color: rgb(238, 238, 255);">
177 177 * <td><code>K</code>
178 178 * <td>Hour in am/pm (0-11)
179 179 * <td><a href="#number">Number</a>
180 180 * <td><code>0</code>
181 181 * <tr>
182 182 * <td><code>h</code>
183 183 * <td>Hour in am/pm (1-12)
184 184 * <td><a href="#number">Number</a>
185 185 * <td><code>12</code>
186 186 * <tr style="background-color: rgb(238, 238, 255);">
187 187 * <td><code>m</code>
188 188 * <td>Minute in hour
189 189 * <td><a href="#number">Number</a>
190 190 * <td><code>30</code>
191 191 * <tr>
192 192 * <td><code>s</code>
193 193 * <td>Second in minute
194 194 * <td><a href="#number">Number</a>
195 195 * <td><code>55</code>
196 196 * <tr style="background-color: rgb(238, 238, 255);">
197 197 * <td><code>S</code>
198 198 * <td>Millisecond
199 199 * <td><a href="#number">Number</a>
200 200 * <td><code>978</code>
201 201 * <tr>
202 202 * <td><code>z</code>
203 203 * <td>Time zone
204 204 * <td><a href="#timezone">General time zone</a>
205 205 * <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
206 206 * <tr style="background-color: rgb(238, 238, 255);">
207 207 * <td><code>Z</code>
208 208 * <td>Time zone
209 209 * <td><a href="#rfc822timezone">RFC 822 time zone</a>
210 210 * <td><code>-0800</code>
211 211 * <tr>
212 212 * <td><code>X</code>
213 213 * <td>Time zone
214 214 * <td><a href="#iso8601timezone">ISO 8601 time zone</a>
215 215 * <td><code>-08</code>; <code>-0800</code>; <code>-08:00</code>
216 216 * </table>
217 217 * </blockquote>
218 218 * Pattern letters are usually repeated, as their number determines the
219 219 * exact presentation:
220 220 * <ul>
221 221 * <li><strong><a name="text">Text:</a></strong>
222 222 * For formatting, if the number of pattern letters is 4 or more,
223 223 * the full form is used; otherwise a short or abbreviated form
224 224 * is used if available.
225 225 * For parsing, both forms are accepted, independent of the number
226 226 * of pattern letters.<br><br></li>
227 227 * <li><strong><a name="number">Number:</a></strong>
228 228 * For formatting, the number of pattern letters is the minimum
229 229 * number of digits, and shorter numbers are zero-padded to this amount.
230 230 * For parsing, the number of pattern letters is ignored unless
231 231 * it's needed to separate two adjacent fields.<br><br></li>
232 232 * <li><strong><a name="year">Year:</a></strong>
233 233 * If the formatter's {@link #getCalendar() Calendar} is the Gregorian
234 234 * calendar, the following rules are applied.<br>
235 235 * <ul>
236 236 * <li>For formatting, if the number of pattern letters is 2, the year
237 237 * is truncated to 2 digits; otherwise it is interpreted as a
238 238 * <a href="#number">number</a>.
239 239 * <li>For parsing, if the number of pattern letters is more than 2,
240 240 * the year is interpreted literally, regardless of the number of
241 241 * digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to
242 242 * Jan 11, 12 A.D.
243 243 * <li>For parsing with the abbreviated year pattern ("y" or "yy"),
244 244 * <code>SimpleDateFormat</code> must interpret the abbreviated year
245 245 * relative to some century. It does this by adjusting dates to be
246 246 * within 80 years before and 20 years after the time the <code>SimpleDateFormat</code>
247 247 * instance is created. For example, using a pattern of "MM/dd/yy" and a
248 248 * <code>SimpleDateFormat</code> instance created on Jan 1, 1997, the string
249 249 * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
250 250 * would be interpreted as May 4, 1964.
251 251 * During parsing, only strings consisting of exactly two digits, as defined by
252 252 * {@link Character#isDigit(char)}, will be parsed into the default century.
253 253 * Any other numeric string, such as a one digit string, a three or more digit
254 254 * string, or a two digit string that isn't all digits (for example, "-1"), is
255 255 * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the
256 256 * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
257 257 * </ul>
258 258 * Otherwise, calendar system specific forms are applied.
259 259 * For both formatting and parsing, if the number of pattern
260 260 * letters is 4 or more, a calendar specific {@linkplain
261 261 * Calendar#LONG long form} is used. Otherwise, a calendar
262 262 * specific {@linkplain Calendar#SHORT short or abbreviated form}
263 263 * is used.<br>
264 264 * <br>
265 265 * If week year {@code 'Y'} is specified and the {@linkplain
266 266 * #getCalendar() calendar} doesn't support any <a
267 267 * href="../util/GregorianCalendar.html#week_year"> week
268 268 * years</a>, the calendar year ({@code 'y'}) is used instead. The
269 269 * support of week years can be tested with a call to {@link
270 270 * DateFormat#getCalendar() getCalendar()}.{@link
271 271 * java.util.Calendar#isWeekDateSupported()
272 272 * isWeekDateSupported()}.<br><br></li>
273 273 * <li><strong><a name="month">Month:</a></strong>
274 274 * If the number of pattern letters is 3 or more, the month is
275 275 * interpreted as <a href="#text">text</a>; otherwise,
276 276 * it is interpreted as a <a href="#number">number</a>.<br>
277 277 * <ul>
278 278 * <li>Letter <em>M</em> produces context-sensitive month names, such as the
279 279 * embedded form of names. If a {@code DateFormatSymbols} has been set
280 280 * explicitly with constructor {@link #SimpleDateFormat(String,
281 281 * DateFormatSymbols)} or method {@link
282 282 * #setDateFormatSymbols(DateFormatSymbols)}, the month names given by
283 283 * the {@code DateFormatSymbols} are used.</li>
284 284 * <li>Letter <em>L</em> produces the standalone form of month names.</li>
285 285 * </ul>
286 286 * <br></li>
287 287 * <li><strong><a name="timezone">General time zone:</a></strong>
288 288 * Time zones are interpreted as <a href="#text">text</a> if they have
289 289 * names. For time zones representing a GMT offset value, the
290 290 * following syntax is used:
291 291 * <pre>
292 292 * <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a>
293 293 * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
294 294 * <i>Sign:</i> one of
295 295 * <code>+ -</code>
296 296 * <i>Hours:</i>
297 297 * <i>Digit</i>
298 298 * <i>Digit</i> <i>Digit</i>
299 299 * <i>Minutes:</i>
300 300 * <i>Digit</i> <i>Digit</i>
301 301 * <i>Digit:</i> one of
302 302 * <code>0 1 2 3 4 5 6 7 8 9</code></pre>
303 303 * <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between
304 304 * 00 and 59. The format is locale independent and digits must be taken
305 305 * from the Basic Latin block of the Unicode standard.
306 306 * <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also
307 307 * accepted.<br><br></li>
308 308 * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong>
309 309 * For formatting, the RFC 822 4-digit time zone format is used:
310 310 *
311 311 * <pre>
312 312 * <i>RFC822TimeZone:</i>
313 313 * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
314 314 * <i>TwoDigitHours:</i>
315 315 * <i>Digit Digit</i></pre>
316 316 * <i>TwoDigitHours</i> must be between 00 and 23. Other definitions
317 317 * are as for <a href="#timezone">general time zones</a>.
318 318 *
319 319 * <p>For parsing, <a href="#timezone">general time zones</a> are also
320 320 * accepted.
321 321 * <li><strong><a name="iso8601timezone">ISO 8601 Time zone:</a></strong>
322 322 * The number of pattern letters designates the format for both formatting
323 323 * and parsing as follows:
324 324 * <pre>
325 325 * <i>ISO8601TimeZone:</i>
326 326 * <i>OneLetterISO8601TimeZone</i>
327 327 * <i>TwoLetterISO8601TimeZone</i>
328 328 * <i>ThreeLetterISO8601TimeZone</i>
329 329 * <i>OneLetterISO8601TimeZone:</i>
330 330 * <i>Sign</i> <i>TwoDigitHours</i>
331 331 * {@code Z}
332 332 * <i>TwoLetterISO8601TimeZone:</i>
333 333 * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
334 334 * {@code Z}
335 335 * <i>ThreeLetterISO8601TimeZone:</i>
336 336 * <i>Sign</i> <i>TwoDigitHours</i> {@code :} <i>Minutes</i>
337 337 * {@code Z}</pre>
338 338 * Other definitions are as for <a href="#timezone">general time zones</a> or
339 339 * <a href="#rfc822timezone">RFC 822 time zones</a>.
340 340 *
341 341 * <p>For formatting, if the offset value from GMT is 0, {@code "Z"} is
342 342 * produced. If the number of pattern letters is 1, any fraction of an hour
343 343 * is ignored. For example, if the pattern is {@code "X"} and the time zone is
344 344 * {@code "GMT+05:30"}, {@code "+05"} is produced.
345 345 *
346 346 * <p>For parsing, {@code "Z"} is parsed as the UTC time zone designator.
347 347 * <a href="#timezone">General time zones</a> are <em>not</em> accepted.
348 348 *
349 349 * <p>If the number of pattern letters is 4 or more, {@link
350 350 * IllegalArgumentException} is thrown when constructing a {@code
351 351 * SimpleDateFormat} or {@linkplain #applyPattern(String) applying a
352 352 * pattern}.
353 353 * </ul>
354 354 * <code>SimpleDateFormat</code> also supports <em>localized date and time
355 355 * pattern</em> strings. In these strings, the pattern letters described above
356 356 * may be replaced with other, locale dependent, pattern letters.
357 357 * <code>SimpleDateFormat</code> does not deal with the localization of text
358 358 * other than the pattern letters; that's up to the client of the class.
359 359 *
360 360 * <h4>Examples</h4>
361 361 *
362 362 * The following examples show how date and time patterns are interpreted in
363 363 * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
364 364 * in the U.S. Pacific Time time zone.
365 365 * <blockquote>
366 366 * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale">
367 367 * <tr style="background-color: rgb(204, 204, 255);">
368 368 * <th align=left>Date and Time Pattern
369 369 * <th align=left>Result
370 370 * <tr>
371 371 * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
372 372 * <td><code>2001.07.04 AD at 12:08:56 PDT</code>
373 373 * <tr style="background-color: rgb(238, 238, 255);">
374 374 * <td><code>"EEE, MMM d, ''yy"</code>
375 375 * <td><code>Wed, Jul 4, '01</code>
376 376 * <tr>
377 377 * <td><code>"h:mm a"</code>
378 378 * <td><code>12:08 PM</code>
379 379 * <tr style="background-color: rgb(238, 238, 255);">
380 380 * <td><code>"hh 'o''clock' a, zzzz"</code>
381 381 * <td><code>12 o'clock PM, Pacific Daylight Time</code>
382 382 * <tr>
383 383 * <td><code>"K:mm a, z"</code>
384 384 * <td><code>0:08 PM, PDT</code>
385 385 * <tr style="background-color: rgb(238, 238, 255);">
386 386 * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
387 387 * <td><code>02001.July.04 AD 12:08 PM</code>
388 388 * <tr>
389 389 * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
390 390 * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
391 391 * <tr style="background-color: rgb(238, 238, 255);">
392 392 * <td><code>"yyMMddHHmmssZ"</code>
393 393 * <td><code>010704120856-0700</code>
394 394 * <tr>
395 395 * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
396 396 * <td><code>2001-07-04T12:08:56.235-0700</code>
397 397 * <tr style="background-color: rgb(238, 238, 255);">
398 398 * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"</code>
399 399 * <td><code>2001-07-04T12:08:56.235-07:00</code>
400 400 * <tr>
401 401 * <td><code>"YYYY-'W'ww-u"</code>
402 402 * <td><code>2001-W27-3</code>
403 403 * </table>
↓ open down ↓ |
403 lines elided |
↑ open up ↑ |
404 404 * </blockquote>
405 405 *
406 406 * <h4><a name="synchronization">Synchronization</a></h4>
407 407 *
408 408 * <p>
409 409 * Date formats are not synchronized.
410 410 * It is recommended to create separate format instances for each thread.
411 411 * If multiple threads access a format concurrently, it must be synchronized
412 412 * externally.
413 413 *
414 - * @see <a href="http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
414 + * @see <a href="https://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
415 415 * @see java.util.Calendar
416 416 * @see java.util.TimeZone
417 417 * @see DateFormat
418 418 * @see DateFormatSymbols
419 419 * @author Mark Davis, Chen-Lieh Huang, Alan Liu
420 420 */
421 421 public class SimpleDateFormat extends DateFormat {
422 422
423 423 // the official serial version ID which says cryptically
424 424 // which version we're compatible with
425 425 static final long serialVersionUID = 4774881970558875024L;
426 426
427 427 // the internal serial version which says which version was written
428 428 // - 0 (default) for version up to JDK 1.1.3
429 429 // - 1 for version from JDK 1.1.4, which includes a new field
430 430 static final int currentSerialVersion = 1;
431 431
432 432 /**
433 433 * The version of the serialized data on the stream. Possible values:
434 434 * <ul>
435 435 * <li><b>0</b> or not present on stream: JDK 1.1.3. This version
436 436 * has no <code>defaultCenturyStart</code> on stream.
437 437 * <li><b>1</b> JDK 1.1.4 or later. This version adds
438 438 * <code>defaultCenturyStart</code>.
439 439 * </ul>
440 440 * When streaming out this class, the most recent format
441 441 * and the highest allowable <code>serialVersionOnStream</code>
442 442 * is written.
443 443 * @serial
444 444 * @since JDK1.1.4
445 445 */
446 446 private int serialVersionOnStream = currentSerialVersion;
447 447
448 448 /**
449 449 * The pattern string of this formatter. This is always a non-localized
450 450 * pattern. May not be null. See class documentation for details.
451 451 * @serial
452 452 */
453 453 private String pattern;
454 454
455 455 /**
456 456 * Saved numberFormat and pattern.
457 457 * @see SimpleDateFormat#checkNegativeNumberExpression
458 458 */
459 459 transient private NumberFormat originalNumberFormat;
460 460 transient private String originalNumberPattern;
461 461
462 462 /**
463 463 * The minus sign to be used with format and parse.
464 464 */
465 465 transient private char minusSign = '-';
466 466
467 467 /**
468 468 * True when a negative sign follows a number.
469 469 * (True as default in Arabic.)
470 470 */
471 471 transient private boolean hasFollowingMinusSign = false;
472 472
473 473 /**
474 474 * True if standalone form needs to be used.
475 475 */
476 476 transient private boolean forceStandaloneForm = false;
477 477
478 478 /**
479 479 * The compiled pattern.
480 480 */
481 481 transient private char[] compiledPattern;
482 482
483 483 /**
484 484 * Tags for the compiled pattern.
485 485 */
486 486 private final static int TAG_QUOTE_ASCII_CHAR = 100;
487 487 private final static int TAG_QUOTE_CHARS = 101;
488 488
489 489 /**
490 490 * Locale dependent digit zero.
491 491 * @see #zeroPaddingNumber
492 492 * @see java.text.DecimalFormatSymbols#getZeroDigit
493 493 */
494 494 transient private char zeroDigit;
495 495
496 496 /**
497 497 * The symbols used by this formatter for week names, month names,
498 498 * etc. May not be null.
499 499 * @serial
500 500 * @see java.text.DateFormatSymbols
501 501 */
502 502 private DateFormatSymbols formatData;
503 503
504 504 /**
505 505 * We map dates with two-digit years into the century starting at
506 506 * <code>defaultCenturyStart</code>, which may be any date. May
507 507 * not be null.
508 508 * @serial
509 509 * @since JDK1.1.4
510 510 */
511 511 private Date defaultCenturyStart;
512 512
513 513 transient private int defaultCenturyStartYear;
514 514
515 515 private static final int MILLIS_PER_MINUTE = 60 * 1000;
516 516
517 517 // For time zones that have no names, use strings GMT+minutes and
518 518 // GMT-minutes. For instance, in France the time zone is GMT+60.
519 519 private static final String GMT = "GMT";
520 520
521 521 /**
522 522 * Cache NumberFormat instances with Locale key.
523 523 */
524 524 private static final ConcurrentMap<Locale, NumberFormat> cachedNumberFormatData
525 525 = new ConcurrentHashMap<>(3);
526 526
527 527 /**
528 528 * The Locale used to instantiate this
529 529 * <code>SimpleDateFormat</code>. The value may be null if this object
530 530 * has been created by an older <code>SimpleDateFormat</code> and
531 531 * deserialized.
532 532 *
533 533 * @serial
534 534 * @since 1.6
535 535 */
536 536 private Locale locale;
537 537
538 538 /**
539 539 * Indicates whether this <code>SimpleDateFormat</code> should use
540 540 * the DateFormatSymbols. If true, the format and parse methods
541 541 * use the DateFormatSymbols values. If false, the format and
542 542 * parse methods call Calendar.getDisplayName or
543 543 * Calendar.getDisplayNames.
544 544 */
545 545 transient boolean useDateFormatSymbols;
546 546
547 547 /**
548 548 * Constructs a <code>SimpleDateFormat</code> using the default pattern and
549 549 * date format symbols for the default
550 550 * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
551 551 * <b>Note:</b> This constructor may not support all locales.
552 552 * For full coverage, use the factory methods in the {@link DateFormat}
553 553 * class.
554 554 */
555 555 public SimpleDateFormat() {
556 556 this("", Locale.getDefault(Locale.Category.FORMAT));
557 557 applyPatternImpl(LocaleProviderAdapter.getResourceBundleBased().getLocaleResources(locale)
558 558 .getDateTimePattern(SHORT, SHORT, calendar));
559 559 }
560 560
561 561 /**
562 562 * Constructs a <code>SimpleDateFormat</code> using the given pattern and
563 563 * the default date format symbols for the default
564 564 * {@link java.util.Locale.Category#FORMAT FORMAT} locale.
565 565 * <b>Note:</b> This constructor may not support all locales.
566 566 * For full coverage, use the factory methods in the {@link DateFormat}
567 567 * class.
568 568 * <p>This is equivalent to calling
569 569 * {@link #SimpleDateFormat(String, Locale)
570 570 * SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}.
571 571 *
572 572 * @see java.util.Locale#getDefault(java.util.Locale.Category)
573 573 * @see java.util.Locale.Category#FORMAT
574 574 * @param pattern the pattern describing the date and time format
575 575 * @exception NullPointerException if the given pattern is null
576 576 * @exception IllegalArgumentException if the given pattern is invalid
577 577 */
578 578 public SimpleDateFormat(String pattern)
579 579 {
580 580 this(pattern, Locale.getDefault(Locale.Category.FORMAT));
581 581 }
582 582
583 583 /**
584 584 * Constructs a <code>SimpleDateFormat</code> using the given pattern and
585 585 * the default date format symbols for the given locale.
586 586 * <b>Note:</b> This constructor may not support all locales.
587 587 * For full coverage, use the factory methods in the {@link DateFormat}
588 588 * class.
589 589 *
590 590 * @param pattern the pattern describing the date and time format
591 591 * @param locale the locale whose date format symbols should be used
592 592 * @exception NullPointerException if the given pattern or locale is null
593 593 * @exception IllegalArgumentException if the given pattern is invalid
594 594 */
595 595 public SimpleDateFormat(String pattern, Locale locale)
596 596 {
597 597 if (pattern == null || locale == null) {
598 598 throw new NullPointerException();
599 599 }
600 600
601 601 initializeCalendar(locale);
602 602 this.pattern = pattern;
603 603 this.formatData = DateFormatSymbols.getInstanceRef(locale);
604 604 this.locale = locale;
605 605 initialize(locale);
606 606 }
607 607
608 608 /**
609 609 * Constructs a <code>SimpleDateFormat</code> using the given pattern and
610 610 * date format symbols.
611 611 *
612 612 * @param pattern the pattern describing the date and time format
613 613 * @param formatSymbols the date format symbols to be used for formatting
614 614 * @exception NullPointerException if the given pattern or formatSymbols is null
615 615 * @exception IllegalArgumentException if the given pattern is invalid
616 616 */
617 617 public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
618 618 {
619 619 if (pattern == null || formatSymbols == null) {
620 620 throw new NullPointerException();
621 621 }
622 622
623 623 this.pattern = pattern;
624 624 this.formatData = (DateFormatSymbols) formatSymbols.clone();
625 625 this.locale = Locale.getDefault(Locale.Category.FORMAT);
626 626 initializeCalendar(this.locale);
627 627 initialize(this.locale);
628 628 useDateFormatSymbols = true;
629 629 }
630 630
631 631 /* Initialize compiledPattern and numberFormat fields */
632 632 private void initialize(Locale loc) {
633 633 // Verify and compile the given pattern.
634 634 compiledPattern = compile(pattern);
635 635
636 636 /* try the cache first */
637 637 numberFormat = cachedNumberFormatData.get(loc);
638 638 if (numberFormat == null) { /* cache miss */
639 639 numberFormat = NumberFormat.getIntegerInstance(loc);
640 640 numberFormat.setGroupingUsed(false);
641 641
642 642 /* update cache */
643 643 cachedNumberFormatData.putIfAbsent(loc, numberFormat);
644 644 }
645 645 numberFormat = (NumberFormat) numberFormat.clone();
646 646
647 647 initializeDefaultCentury();
648 648 }
649 649
650 650 private void initializeCalendar(Locale loc) {
651 651 if (calendar == null) {
652 652 assert loc != null;
653 653 // The format object must be constructed using the symbols for this zone.
654 654 // However, the calendar should use the current default TimeZone.
655 655 // If this is not contained in the locale zone strings, then the zone
656 656 // will be formatted using generic GMT+/-H:MM nomenclature.
657 657 calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
658 658 }
659 659 }
660 660
661 661 /**
662 662 * Returns the compiled form of the given pattern. The syntax of
663 663 * the compiled pattern is:
664 664 * <blockquote>
665 665 * CompiledPattern:
666 666 * EntryList
667 667 * EntryList:
668 668 * Entry
669 669 * EntryList Entry
670 670 * Entry:
671 671 * TagField
672 672 * TagField data
673 673 * TagField:
674 674 * Tag Length
675 675 * TaggedData
676 676 * Tag:
677 677 * pattern_char_index
678 678 * TAG_QUOTE_CHARS
679 679 * Length:
680 680 * short_length
681 681 * long_length
682 682 * TaggedData:
683 683 * TAG_QUOTE_ASCII_CHAR ascii_char
684 684 *
685 685 * </blockquote>
686 686 *
687 687 * where `short_length' is an 8-bit unsigned integer between 0 and
688 688 * 254. `long_length' is a sequence of an 8-bit integer 255 and a
689 689 * 32-bit signed integer value which is split into upper and lower
690 690 * 16-bit fields in two char's. `pattern_char_index' is an 8-bit
691 691 * integer between 0 and 18. `ascii_char' is an 7-bit ASCII
692 692 * character value. `data' depends on its Tag value.
693 693 * <p>
694 694 * If Length is short_length, Tag and short_length are packed in a
695 695 * single char, as illustrated below.
696 696 * <blockquote>
697 697 * char[0] = (Tag << 8) | short_length;
698 698 * </blockquote>
699 699 *
700 700 * If Length is long_length, Tag and 255 are packed in the first
701 701 * char and a 32-bit integer, as illustrated below.
702 702 * <blockquote>
703 703 * char[0] = (Tag << 8) | 255;
704 704 * char[1] = (char) (long_length >>> 16);
705 705 * char[2] = (char) (long_length & 0xffff);
706 706 * </blockquote>
707 707 * <p>
708 708 * If Tag is a pattern_char_index, its Length is the number of
709 709 * pattern characters. For example, if the given pattern is
710 710 * "yyyy", Tag is 1 and Length is 4, followed by no data.
711 711 * <p>
712 712 * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's
713 713 * following the TagField. For example, if the given pattern is
714 714 * "'o''clock'", Length is 7 followed by a char sequence of
715 715 * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>.
716 716 * <p>
717 717 * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
718 718 * character in place of Length. For example, if the given pattern
719 719 * is "'o'", the TaggedData entry is
720 720 * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
721 721 *
722 722 * @exception NullPointerException if the given pattern is null
723 723 * @exception IllegalArgumentException if the given pattern is invalid
724 724 */
725 725 private char[] compile(String pattern) {
726 726 int length = pattern.length();
727 727 boolean inQuote = false;
728 728 StringBuilder compiledCode = new StringBuilder(length * 2);
729 729 StringBuilder tmpBuffer = null;
730 730 int count = 0, tagcount = 0;
731 731 int lastTag = -1, prevTag = -1;
732 732
733 733 for (int i = 0; i < length; i++) {
734 734 char c = pattern.charAt(i);
735 735
736 736 if (c == '\'') {
737 737 // '' is treated as a single quote regardless of being
738 738 // in a quoted section.
739 739 if ((i + 1) < length) {
740 740 c = pattern.charAt(i + 1);
741 741 if (c == '\'') {
742 742 i++;
743 743 if (count != 0) {
744 744 encode(lastTag, count, compiledCode);
745 745 tagcount++;
746 746 prevTag = lastTag;
747 747 lastTag = -1;
748 748 count = 0;
749 749 }
750 750 if (inQuote) {
751 751 tmpBuffer.append(c);
752 752 } else {
753 753 compiledCode.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
754 754 }
755 755 continue;
756 756 }
757 757 }
758 758 if (!inQuote) {
759 759 if (count != 0) {
760 760 encode(lastTag, count, compiledCode);
761 761 tagcount++;
762 762 prevTag = lastTag;
763 763 lastTag = -1;
764 764 count = 0;
765 765 }
766 766 if (tmpBuffer == null) {
767 767 tmpBuffer = new StringBuilder(length);
768 768 } else {
769 769 tmpBuffer.setLength(0);
770 770 }
771 771 inQuote = true;
772 772 } else {
773 773 int len = tmpBuffer.length();
774 774 if (len == 1) {
775 775 char ch = tmpBuffer.charAt(0);
776 776 if (ch < 128) {
777 777 compiledCode.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | ch));
778 778 } else {
779 779 compiledCode.append((char)(TAG_QUOTE_CHARS << 8 | 1));
780 780 compiledCode.append(ch);
781 781 }
782 782 } else {
783 783 encode(TAG_QUOTE_CHARS, len, compiledCode);
784 784 compiledCode.append(tmpBuffer);
785 785 }
786 786 inQuote = false;
787 787 }
788 788 continue;
789 789 }
790 790 if (inQuote) {
791 791 tmpBuffer.append(c);
792 792 continue;
793 793 }
794 794 if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
795 795 if (count != 0) {
796 796 encode(lastTag, count, compiledCode);
797 797 tagcount++;
798 798 prevTag = lastTag;
799 799 lastTag = -1;
800 800 count = 0;
801 801 }
802 802 if (c < 128) {
803 803 // In most cases, c would be a delimiter, such as ':'.
804 804 compiledCode.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
805 805 } else {
806 806 // Take any contiguous non-ASCII alphabet characters and
807 807 // put them in a single TAG_QUOTE_CHARS.
808 808 int j;
809 809 for (j = i + 1; j < length; j++) {
810 810 char d = pattern.charAt(j);
811 811 if (d == '\'' || (d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) {
812 812 break;
813 813 }
814 814 }
815 815 compiledCode.append((char)(TAG_QUOTE_CHARS << 8 | (j - i)));
816 816 for (; i < j; i++) {
817 817 compiledCode.append(pattern.charAt(i));
818 818 }
819 819 i--;
820 820 }
821 821 continue;
822 822 }
823 823
824 824 int tag;
825 825 if ((tag = DateFormatSymbols.patternChars.indexOf(c)) == -1) {
826 826 throw new IllegalArgumentException("Illegal pattern character " +
827 827 "'" + c + "'");
828 828 }
829 829 if (lastTag == -1 || lastTag == tag) {
830 830 lastTag = tag;
831 831 count++;
832 832 continue;
833 833 }
834 834 encode(lastTag, count, compiledCode);
835 835 tagcount++;
836 836 prevTag = lastTag;
837 837 lastTag = tag;
838 838 count = 1;
839 839 }
840 840
841 841 if (inQuote) {
842 842 throw new IllegalArgumentException("Unterminated quote");
843 843 }
844 844
845 845 if (count != 0) {
846 846 encode(lastTag, count, compiledCode);
847 847 tagcount++;
848 848 prevTag = lastTag;
849 849 }
850 850
851 851 forceStandaloneForm = (tagcount == 1 && prevTag == PATTERN_MONTH);
852 852
853 853 // Copy the compiled pattern to a char array
854 854 int len = compiledCode.length();
855 855 char[] r = new char[len];
856 856 compiledCode.getChars(0, len, r, 0);
857 857 return r;
858 858 }
859 859
860 860 /**
861 861 * Encodes the given tag and length and puts encoded char(s) into buffer.
862 862 */
863 863 private static void encode(int tag, int length, StringBuilder buffer) {
864 864 if (tag == PATTERN_ISO_ZONE && length >= 4) {
865 865 throw new IllegalArgumentException("invalid ISO 8601 format: length=" + length);
866 866 }
867 867 if (length < 255) {
868 868 buffer.append((char)(tag << 8 | length));
869 869 } else {
870 870 buffer.append((char)((tag << 8) | 0xff));
871 871 buffer.append((char)(length >>> 16));
872 872 buffer.append((char)(length & 0xffff));
873 873 }
874 874 }
875 875
876 876 /* Initialize the fields we use to disambiguate ambiguous years. Separate
877 877 * so we can call it from readObject().
878 878 */
879 879 private void initializeDefaultCentury() {
880 880 calendar.setTimeInMillis(System.currentTimeMillis());
881 881 calendar.add( Calendar.YEAR, -80 );
882 882 parseAmbiguousDatesAsAfter(calendar.getTime());
883 883 }
884 884
885 885 /* Define one-century window into which to disambiguate dates using
886 886 * two-digit years.
887 887 */
888 888 private void parseAmbiguousDatesAsAfter(Date startDate) {
889 889 defaultCenturyStart = startDate;
890 890 calendar.setTime(startDate);
891 891 defaultCenturyStartYear = calendar.get(Calendar.YEAR);
892 892 }
893 893
894 894 /**
895 895 * Sets the 100-year period 2-digit years will be interpreted as being in
896 896 * to begin on the date the user specifies.
897 897 *
898 898 * @param startDate During parsing, two digit years will be placed in the range
899 899 * <code>startDate</code> to <code>startDate + 100 years</code>.
900 900 * @see #get2DigitYearStart
901 901 * @since 1.2
902 902 */
903 903 public void set2DigitYearStart(Date startDate) {
904 904 parseAmbiguousDatesAsAfter(new Date(startDate.getTime()));
905 905 }
906 906
907 907 /**
908 908 * Returns the beginning date of the 100-year period 2-digit years are interpreted
909 909 * as being within.
910 910 *
911 911 * @return the start of the 100-year period into which two digit years are
912 912 * parsed
913 913 * @see #set2DigitYearStart
914 914 * @since 1.2
915 915 */
916 916 public Date get2DigitYearStart() {
917 917 return (Date) defaultCenturyStart.clone();
918 918 }
919 919
920 920 /**
921 921 * Formats the given <code>Date</code> into a date/time string and appends
922 922 * the result to the given <code>StringBuffer</code>.
923 923 *
924 924 * @param date the date-time value to be formatted into a date-time string.
925 925 * @param toAppendTo where the new date-time text is to be appended.
926 926 * @param pos the formatting position. On input: an alignment field,
927 927 * if desired. On output: the offsets of the alignment field.
928 928 * @return the formatted date-time string.
929 929 * @exception NullPointerException if the given {@code date} is {@code null}.
930 930 */
931 931 @Override
932 932 public StringBuffer format(Date date, StringBuffer toAppendTo,
933 933 FieldPosition pos)
934 934 {
935 935 pos.beginIndex = pos.endIndex = 0;
936 936 return format(date, toAppendTo, pos.getFieldDelegate());
937 937 }
938 938
939 939 // Called from Format after creating a FieldDelegate
940 940 private StringBuffer format(Date date, StringBuffer toAppendTo,
941 941 FieldDelegate delegate) {
942 942 // Convert input date to time field list
943 943 calendar.setTime(date);
944 944
945 945 boolean useDateFormatSymbols = useDateFormatSymbols();
946 946
947 947 for (int i = 0; i < compiledPattern.length; ) {
948 948 int tag = compiledPattern[i] >>> 8;
949 949 int count = compiledPattern[i++] & 0xff;
950 950 if (count == 255) {
951 951 count = compiledPattern[i++] << 16;
952 952 count |= compiledPattern[i++];
953 953 }
954 954
955 955 switch (tag) {
956 956 case TAG_QUOTE_ASCII_CHAR:
957 957 toAppendTo.append((char)count);
958 958 break;
959 959
960 960 case TAG_QUOTE_CHARS:
961 961 toAppendTo.append(compiledPattern, i, count);
962 962 i += count;
963 963 break;
964 964
965 965 default:
966 966 subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
967 967 break;
968 968 }
969 969 }
970 970 return toAppendTo;
971 971 }
972 972
973 973 /**
974 974 * Formats an Object producing an <code>AttributedCharacterIterator</code>.
975 975 * You can use the returned <code>AttributedCharacterIterator</code>
976 976 * to build the resulting String, as well as to determine information
977 977 * about the resulting String.
978 978 * <p>
979 979 * Each attribute key of the AttributedCharacterIterator will be of type
980 980 * <code>DateFormat.Field</code>, with the corresponding attribute value
981 981 * being the same as the attribute key.
982 982 *
983 983 * @exception NullPointerException if obj is null.
984 984 * @exception IllegalArgumentException if the Format cannot format the
985 985 * given object, or if the Format's pattern string is invalid.
986 986 * @param obj The object to format
987 987 * @return AttributedCharacterIterator describing the formatted value.
988 988 * @since 1.4
989 989 */
990 990 @Override
991 991 public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
992 992 StringBuffer sb = new StringBuffer();
993 993 CharacterIteratorFieldDelegate delegate = new
994 994 CharacterIteratorFieldDelegate();
995 995
996 996 if (obj instanceof Date) {
997 997 format((Date)obj, sb, delegate);
998 998 }
999 999 else if (obj instanceof Number) {
1000 1000 format(new Date(((Number)obj).longValue()), sb, delegate);
1001 1001 }
1002 1002 else if (obj == null) {
1003 1003 throw new NullPointerException(
1004 1004 "formatToCharacterIterator must be passed non-null object");
1005 1005 }
1006 1006 else {
1007 1007 throw new IllegalArgumentException(
1008 1008 "Cannot format given Object as a Date");
1009 1009 }
1010 1010 return delegate.getIterator(sb.toString());
1011 1011 }
1012 1012
1013 1013 // Map index into pattern character string to Calendar field number
1014 1014 private static final int[] PATTERN_INDEX_TO_CALENDAR_FIELD = {
1015 1015 Calendar.ERA,
1016 1016 Calendar.YEAR,
1017 1017 Calendar.MONTH,
1018 1018 Calendar.DATE,
1019 1019 Calendar.HOUR_OF_DAY,
1020 1020 Calendar.HOUR_OF_DAY,
1021 1021 Calendar.MINUTE,
1022 1022 Calendar.SECOND,
1023 1023 Calendar.MILLISECOND,
1024 1024 Calendar.DAY_OF_WEEK,
1025 1025 Calendar.DAY_OF_YEAR,
1026 1026 Calendar.DAY_OF_WEEK_IN_MONTH,
1027 1027 Calendar.WEEK_OF_YEAR,
1028 1028 Calendar.WEEK_OF_MONTH,
1029 1029 Calendar.AM_PM,
1030 1030 Calendar.HOUR,
1031 1031 Calendar.HOUR,
1032 1032 Calendar.ZONE_OFFSET,
1033 1033 Calendar.ZONE_OFFSET,
1034 1034 CalendarBuilder.WEEK_YEAR, // Pseudo Calendar field
1035 1035 CalendarBuilder.ISO_DAY_OF_WEEK, // Pseudo Calendar field
1036 1036 Calendar.ZONE_OFFSET,
1037 1037 Calendar.MONTH
1038 1038 };
1039 1039
1040 1040 // Map index into pattern character string to DateFormat field number
1041 1041 private static final int[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD = {
1042 1042 DateFormat.ERA_FIELD,
1043 1043 DateFormat.YEAR_FIELD,
1044 1044 DateFormat.MONTH_FIELD,
1045 1045 DateFormat.DATE_FIELD,
1046 1046 DateFormat.HOUR_OF_DAY1_FIELD,
1047 1047 DateFormat.HOUR_OF_DAY0_FIELD,
1048 1048 DateFormat.MINUTE_FIELD,
1049 1049 DateFormat.SECOND_FIELD,
1050 1050 DateFormat.MILLISECOND_FIELD,
1051 1051 DateFormat.DAY_OF_WEEK_FIELD,
1052 1052 DateFormat.DAY_OF_YEAR_FIELD,
1053 1053 DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD,
1054 1054 DateFormat.WEEK_OF_YEAR_FIELD,
1055 1055 DateFormat.WEEK_OF_MONTH_FIELD,
1056 1056 DateFormat.AM_PM_FIELD,
1057 1057 DateFormat.HOUR1_FIELD,
1058 1058 DateFormat.HOUR0_FIELD,
1059 1059 DateFormat.TIMEZONE_FIELD,
1060 1060 DateFormat.TIMEZONE_FIELD,
1061 1061 DateFormat.YEAR_FIELD,
1062 1062 DateFormat.DAY_OF_WEEK_FIELD,
1063 1063 DateFormat.TIMEZONE_FIELD,
1064 1064 DateFormat.MONTH_FIELD
1065 1065 };
1066 1066
1067 1067 // Maps from DecimalFormatSymbols index to Field constant
1068 1068 private static final Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = {
1069 1069 Field.ERA,
1070 1070 Field.YEAR,
1071 1071 Field.MONTH,
1072 1072 Field.DAY_OF_MONTH,
1073 1073 Field.HOUR_OF_DAY1,
1074 1074 Field.HOUR_OF_DAY0,
1075 1075 Field.MINUTE,
1076 1076 Field.SECOND,
1077 1077 Field.MILLISECOND,
1078 1078 Field.DAY_OF_WEEK,
1079 1079 Field.DAY_OF_YEAR,
1080 1080 Field.DAY_OF_WEEK_IN_MONTH,
1081 1081 Field.WEEK_OF_YEAR,
1082 1082 Field.WEEK_OF_MONTH,
1083 1083 Field.AM_PM,
1084 1084 Field.HOUR1,
1085 1085 Field.HOUR0,
1086 1086 Field.TIME_ZONE,
1087 1087 Field.TIME_ZONE,
1088 1088 Field.YEAR,
1089 1089 Field.DAY_OF_WEEK,
1090 1090 Field.TIME_ZONE,
1091 1091 Field.MONTH
1092 1092 };
1093 1093
1094 1094 /**
1095 1095 * Private member function that does the real date/time formatting.
1096 1096 */
1097 1097 private void subFormat(int patternCharIndex, int count,
1098 1098 FieldDelegate delegate, StringBuffer buffer,
1099 1099 boolean useDateFormatSymbols)
1100 1100 {
1101 1101 int maxIntCount = Integer.MAX_VALUE;
1102 1102 String current = null;
1103 1103 int beginOffset = buffer.length();
1104 1104
1105 1105 int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1106 1106 int value;
1107 1107 if (field == CalendarBuilder.WEEK_YEAR) {
1108 1108 if (calendar.isWeekDateSupported()) {
1109 1109 value = calendar.getWeekYear();
1110 1110 } else {
1111 1111 // use calendar year 'y' instead
1112 1112 patternCharIndex = PATTERN_YEAR;
1113 1113 field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1114 1114 value = calendar.get(field);
1115 1115 }
1116 1116 } else if (field == CalendarBuilder.ISO_DAY_OF_WEEK) {
1117 1117 value = CalendarBuilder.toISODayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
1118 1118 } else {
1119 1119 value = calendar.get(field);
1120 1120 }
1121 1121
1122 1122 int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
1123 1123 if (!useDateFormatSymbols && field < Calendar.ZONE_OFFSET
1124 1124 && patternCharIndex != PATTERN_MONTH_STANDALONE) {
1125 1125 current = calendar.getDisplayName(field, style, locale);
1126 1126 }
1127 1127
1128 1128 // Note: zeroPaddingNumber() assumes that maxDigits is either
1129 1129 // 2 or maxIntCount. If we make any changes to this,
1130 1130 // zeroPaddingNumber() must be fixed.
1131 1131
1132 1132 switch (patternCharIndex) {
1133 1133 case PATTERN_ERA: // 'G'
1134 1134 if (useDateFormatSymbols) {
1135 1135 String[] eras = formatData.getEras();
1136 1136 if (value < eras.length) {
1137 1137 current = eras[value];
1138 1138 }
1139 1139 }
1140 1140 if (current == null) {
1141 1141 current = "";
1142 1142 }
1143 1143 break;
1144 1144
1145 1145 case PATTERN_WEEK_YEAR: // 'Y'
1146 1146 case PATTERN_YEAR: // 'y'
1147 1147 if (calendar instanceof GregorianCalendar) {
1148 1148 if (count != 2) {
1149 1149 zeroPaddingNumber(value, count, maxIntCount, buffer);
1150 1150 } else {
1151 1151 zeroPaddingNumber(value, 2, 2, buffer);
1152 1152 } // clip 1996 to 96
1153 1153 } else {
1154 1154 if (current == null) {
1155 1155 zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
1156 1156 maxIntCount, buffer);
1157 1157 }
1158 1158 }
1159 1159 break;
1160 1160
1161 1161 case PATTERN_MONTH: // 'M' (context seinsive)
1162 1162 if (useDateFormatSymbols) {
1163 1163 String[] months;
1164 1164 if (count >= 4) {
1165 1165 months = formatData.getMonths();
1166 1166 current = months[value];
1167 1167 } else if (count == 3) {
1168 1168 months = formatData.getShortMonths();
1169 1169 current = months[value];
1170 1170 }
1171 1171 } else {
1172 1172 if (count < 3) {
1173 1173 current = null;
1174 1174 } else if (forceStandaloneForm) {
1175 1175 current = calendar.getDisplayName(field, style | 0x8000, locale);
1176 1176 if (current == null) {
1177 1177 current = calendar.getDisplayName(field, style, locale);
1178 1178 }
1179 1179 }
1180 1180 }
1181 1181 if (current == null) {
1182 1182 zeroPaddingNumber(value+1, count, maxIntCount, buffer);
1183 1183 }
1184 1184 break;
1185 1185
1186 1186 case PATTERN_MONTH_STANDALONE: // 'L'
1187 1187 assert current == null;
1188 1188 if (locale == null) {
1189 1189 String[] months;
1190 1190 if (count >= 4) {
1191 1191 months = formatData.getMonths();
1192 1192 current = months[value];
1193 1193 } else if (count == 3) {
1194 1194 months = formatData.getShortMonths();
1195 1195 current = months[value];
1196 1196 }
1197 1197 } else {
1198 1198 if (count >= 3) {
1199 1199 current = calendar.getDisplayName(field, style | 0x8000, locale);
1200 1200 }
1201 1201 }
1202 1202 if (current == null) {
1203 1203 zeroPaddingNumber(value+1, count, maxIntCount, buffer);
1204 1204 }
1205 1205 break;
1206 1206
1207 1207 case PATTERN_HOUR_OF_DAY1: // 'k' 1-based. eg, 23:59 + 1 hour =>> 24:59
1208 1208 if (current == null) {
1209 1209 if (value == 0) {
1210 1210 zeroPaddingNumber(calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1,
1211 1211 count, maxIntCount, buffer);
1212 1212 } else {
1213 1213 zeroPaddingNumber(value, count, maxIntCount, buffer);
1214 1214 }
1215 1215 }
1216 1216 break;
1217 1217
1218 1218 case PATTERN_DAY_OF_WEEK: // 'E'
1219 1219 if (useDateFormatSymbols) {
1220 1220 String[] weekdays;
1221 1221 if (count >= 4) {
1222 1222 weekdays = formatData.getWeekdays();
1223 1223 current = weekdays[value];
1224 1224 } else { // count < 4, use abbreviated form if exists
1225 1225 weekdays = formatData.getShortWeekdays();
1226 1226 current = weekdays[value];
1227 1227 }
1228 1228 }
1229 1229 break;
1230 1230
1231 1231 case PATTERN_AM_PM: // 'a'
1232 1232 if (useDateFormatSymbols) {
1233 1233 String[] ampm = formatData.getAmPmStrings();
1234 1234 current = ampm[value];
1235 1235 }
1236 1236 break;
1237 1237
1238 1238 case PATTERN_HOUR1: // 'h' 1-based. eg, 11PM + 1 hour =>> 12 AM
1239 1239 if (current == null) {
1240 1240 if (value == 0) {
1241 1241 zeroPaddingNumber(calendar.getLeastMaximum(Calendar.HOUR) + 1,
1242 1242 count, maxIntCount, buffer);
1243 1243 } else {
1244 1244 zeroPaddingNumber(value, count, maxIntCount, buffer);
1245 1245 }
1246 1246 }
1247 1247 break;
1248 1248
1249 1249 case PATTERN_ZONE_NAME: // 'z'
1250 1250 if (current == null) {
1251 1251 if (formatData.locale == null || formatData.isZoneStringsSet) {
1252 1252 int zoneIndex =
1253 1253 formatData.getZoneIndex(calendar.getTimeZone().getID());
1254 1254 if (zoneIndex == -1) {
1255 1255 value = calendar.get(Calendar.ZONE_OFFSET) +
1256 1256 calendar.get(Calendar.DST_OFFSET);
1257 1257 buffer.append(ZoneInfoFile.toCustomID(value));
1258 1258 } else {
1259 1259 int index = (calendar.get(Calendar.DST_OFFSET) == 0) ? 1: 3;
1260 1260 if (count < 4) {
1261 1261 // Use the short name
1262 1262 index++;
1263 1263 }
1264 1264 String[][] zoneStrings = formatData.getZoneStringsWrapper();
1265 1265 buffer.append(zoneStrings[zoneIndex][index]);
1266 1266 }
1267 1267 } else {
1268 1268 TimeZone tz = calendar.getTimeZone();
1269 1269 boolean daylight = (calendar.get(Calendar.DST_OFFSET) != 0);
1270 1270 int tzstyle = (count < 4 ? TimeZone.SHORT : TimeZone.LONG);
1271 1271 buffer.append(tz.getDisplayName(daylight, tzstyle, formatData.locale));
1272 1272 }
1273 1273 }
1274 1274 break;
1275 1275
1276 1276 case PATTERN_ZONE_VALUE: // 'Z' ("-/+hhmm" form)
1277 1277 value = (calendar.get(Calendar.ZONE_OFFSET) +
1278 1278 calendar.get(Calendar.DST_OFFSET)) / 60000;
1279 1279
1280 1280 int width = 4;
1281 1281 if (value >= 0) {
1282 1282 buffer.append('+');
1283 1283 } else {
1284 1284 width++;
1285 1285 }
1286 1286
1287 1287 int num = (value / 60) * 100 + (value % 60);
1288 1288 CalendarUtils.sprintf0d(buffer, num, width);
1289 1289 break;
1290 1290
1291 1291 case PATTERN_ISO_ZONE: // 'X'
1292 1292 value = calendar.get(Calendar.ZONE_OFFSET)
1293 1293 + calendar.get(Calendar.DST_OFFSET);
1294 1294
1295 1295 if (value == 0) {
1296 1296 buffer.append('Z');
1297 1297 break;
1298 1298 }
1299 1299
1300 1300 value /= 60000;
1301 1301 if (value >= 0) {
1302 1302 buffer.append('+');
1303 1303 } else {
1304 1304 buffer.append('-');
1305 1305 value = -value;
1306 1306 }
1307 1307
1308 1308 CalendarUtils.sprintf0d(buffer, value / 60, 2);
1309 1309 if (count == 1) {
1310 1310 break;
1311 1311 }
1312 1312
1313 1313 if (count == 3) {
1314 1314 buffer.append(':');
1315 1315 }
1316 1316 CalendarUtils.sprintf0d(buffer, value % 60, 2);
1317 1317 break;
1318 1318
1319 1319 default:
1320 1320 // case PATTERN_DAY_OF_MONTH: // 'd'
1321 1321 // case PATTERN_HOUR_OF_DAY0: // 'H' 0-based. eg, 23:59 + 1 hour =>> 00:59
1322 1322 // case PATTERN_MINUTE: // 'm'
1323 1323 // case PATTERN_SECOND: // 's'
1324 1324 // case PATTERN_MILLISECOND: // 'S'
1325 1325 // case PATTERN_DAY_OF_YEAR: // 'D'
1326 1326 // case PATTERN_DAY_OF_WEEK_IN_MONTH: // 'F'
1327 1327 // case PATTERN_WEEK_OF_YEAR: // 'w'
1328 1328 // case PATTERN_WEEK_OF_MONTH: // 'W'
1329 1329 // case PATTERN_HOUR0: // 'K' eg, 11PM + 1 hour =>> 0 AM
1330 1330 // case PATTERN_ISO_DAY_OF_WEEK: // 'u' pseudo field, Monday = 1, ..., Sunday = 7
1331 1331 if (current == null) {
1332 1332 zeroPaddingNumber(value, count, maxIntCount, buffer);
1333 1333 }
1334 1334 break;
1335 1335 } // switch (patternCharIndex)
1336 1336
1337 1337 if (current != null) {
1338 1338 buffer.append(current);
1339 1339 }
1340 1340
1341 1341 int fieldID = PATTERN_INDEX_TO_DATE_FORMAT_FIELD[patternCharIndex];
1342 1342 Field f = PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID[patternCharIndex];
1343 1343
1344 1344 delegate.formatted(fieldID, f, f, beginOffset, buffer.length(), buffer);
1345 1345 }
1346 1346
1347 1347 /**
1348 1348 * Formats a number with the specified minimum and maximum number of digits.
1349 1349 */
1350 1350 private void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer buffer)
1351 1351 {
1352 1352 // Optimization for 1, 2 and 4 digit numbers. This should
1353 1353 // cover most cases of formatting date/time related items.
1354 1354 // Note: This optimization code assumes that maxDigits is
1355 1355 // either 2 or Integer.MAX_VALUE (maxIntCount in format()).
1356 1356 try {
1357 1357 if (zeroDigit == 0) {
1358 1358 zeroDigit = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getZeroDigit();
1359 1359 }
1360 1360 if (value >= 0) {
1361 1361 if (value < 100 && minDigits >= 1 && minDigits <= 2) {
1362 1362 if (value < 10) {
1363 1363 if (minDigits == 2) {
1364 1364 buffer.append(zeroDigit);
1365 1365 }
1366 1366 buffer.append((char)(zeroDigit + value));
1367 1367 } else {
1368 1368 buffer.append((char)(zeroDigit + value / 10));
1369 1369 buffer.append((char)(zeroDigit + value % 10));
1370 1370 }
1371 1371 return;
1372 1372 } else if (value >= 1000 && value < 10000) {
1373 1373 if (minDigits == 4) {
1374 1374 buffer.append((char)(zeroDigit + value / 1000));
1375 1375 value %= 1000;
1376 1376 buffer.append((char)(zeroDigit + value / 100));
1377 1377 value %= 100;
1378 1378 buffer.append((char)(zeroDigit + value / 10));
1379 1379 buffer.append((char)(zeroDigit + value % 10));
1380 1380 return;
1381 1381 }
1382 1382 if (minDigits == 2 && maxDigits == 2) {
1383 1383 zeroPaddingNumber(value % 100, 2, 2, buffer);
1384 1384 return;
1385 1385 }
1386 1386 }
1387 1387 }
1388 1388 } catch (Exception e) {
1389 1389 }
1390 1390
1391 1391 numberFormat.setMinimumIntegerDigits(minDigits);
1392 1392 numberFormat.setMaximumIntegerDigits(maxDigits);
1393 1393 numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
1394 1394 }
1395 1395
1396 1396
1397 1397 /**
1398 1398 * Parses text from a string to produce a <code>Date</code>.
1399 1399 * <p>
1400 1400 * The method attempts to parse text starting at the index given by
1401 1401 * <code>pos</code>.
1402 1402 * If parsing succeeds, then the index of <code>pos</code> is updated
1403 1403 * to the index after the last character used (parsing does not necessarily
1404 1404 * use all characters up to the end of the string), and the parsed
1405 1405 * date is returned. The updated <code>pos</code> can be used to
1406 1406 * indicate the starting point for the next call to this method.
1407 1407 * If an error occurs, then the index of <code>pos</code> is not
1408 1408 * changed, the error index of <code>pos</code> is set to the index of
1409 1409 * the character where the error occurred, and null is returned.
1410 1410 *
1411 1411 * <p>This parsing operation uses the {@link DateFormat#calendar
1412 1412 * calendar} to produce a {@code Date}. All of the {@code
1413 1413 * calendar}'s date-time fields are {@linkplain Calendar#clear()
1414 1414 * cleared} before parsing, and the {@code calendar}'s default
1415 1415 * values of the date-time fields are used for any missing
1416 1416 * date-time information. For example, the year value of the
1417 1417 * parsed {@code Date} is 1970 with {@link GregorianCalendar} if
1418 1418 * no year value is given from the parsing operation. The {@code
1419 1419 * TimeZone} value may be overwritten, depending on the given
1420 1420 * pattern and the time zone value in {@code text}. Any {@code
1421 1421 * TimeZone} value that has previously been set by a call to
1422 1422 * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
1423 1423 * to be restored for further operations.
1424 1424 *
1425 1425 * @param text A <code>String</code>, part of which should be parsed.
1426 1426 * @param pos A <code>ParsePosition</code> object with index and error
1427 1427 * index information as described above.
1428 1428 * @return A <code>Date</code> parsed from the string. In case of
1429 1429 * error, returns null.
1430 1430 * @exception NullPointerException if <code>text</code> or <code>pos</code> is null.
1431 1431 */
1432 1432 @Override
1433 1433 public Date parse(String text, ParsePosition pos)
1434 1434 {
1435 1435 checkNegativeNumberExpression();
1436 1436
1437 1437 int start = pos.index;
1438 1438 int oldStart = start;
1439 1439 int textLength = text.length();
1440 1440
1441 1441 boolean[] ambiguousYear = {false};
1442 1442
1443 1443 CalendarBuilder calb = new CalendarBuilder();
1444 1444
1445 1445 for (int i = 0; i < compiledPattern.length; ) {
1446 1446 int tag = compiledPattern[i] >>> 8;
1447 1447 int count = compiledPattern[i++] & 0xff;
1448 1448 if (count == 255) {
1449 1449 count = compiledPattern[i++] << 16;
1450 1450 count |= compiledPattern[i++];
1451 1451 }
1452 1452
1453 1453 switch (tag) {
1454 1454 case TAG_QUOTE_ASCII_CHAR:
1455 1455 if (start >= textLength || text.charAt(start) != (char)count) {
1456 1456 pos.index = oldStart;
1457 1457 pos.errorIndex = start;
1458 1458 return null;
1459 1459 }
1460 1460 start++;
1461 1461 break;
1462 1462
1463 1463 case TAG_QUOTE_CHARS:
1464 1464 while (count-- > 0) {
1465 1465 if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
1466 1466 pos.index = oldStart;
1467 1467 pos.errorIndex = start;
1468 1468 return null;
1469 1469 }
1470 1470 start++;
1471 1471 }
1472 1472 break;
1473 1473
1474 1474 default:
1475 1475 // Peek the next pattern to determine if we need to
1476 1476 // obey the number of pattern letters for
1477 1477 // parsing. It's required when parsing contiguous
1478 1478 // digit text (e.g., "20010704") with a pattern which
1479 1479 // has no delimiters between fields, like "yyyyMMdd".
1480 1480 boolean obeyCount = false;
1481 1481
1482 1482 // In Arabic, a minus sign for a negative number is put after
1483 1483 // the number. Even in another locale, a minus sign can be
1484 1484 // put after a number using DateFormat.setNumberFormat().
1485 1485 // If both the minus sign and the field-delimiter are '-',
1486 1486 // subParse() needs to determine whether a '-' after a number
1487 1487 // in the given text is a delimiter or is a minus sign for the
1488 1488 // preceding number. We give subParse() a clue based on the
1489 1489 // information in compiledPattern.
1490 1490 boolean useFollowingMinusSignAsDelimiter = false;
1491 1491
1492 1492 if (i < compiledPattern.length) {
1493 1493 int nextTag = compiledPattern[i] >>> 8;
1494 1494 if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||
1495 1495 nextTag == TAG_QUOTE_CHARS)) {
1496 1496 obeyCount = true;
1497 1497 }
1498 1498
1499 1499 if (hasFollowingMinusSign &&
1500 1500 (nextTag == TAG_QUOTE_ASCII_CHAR ||
1501 1501 nextTag == TAG_QUOTE_CHARS)) {
1502 1502 int c;
1503 1503 if (nextTag == TAG_QUOTE_ASCII_CHAR) {
1504 1504 c = compiledPattern[i] & 0xff;
1505 1505 } else {
1506 1506 c = compiledPattern[i+1];
1507 1507 }
1508 1508
1509 1509 if (c == minusSign) {
1510 1510 useFollowingMinusSignAsDelimiter = true;
1511 1511 }
1512 1512 }
1513 1513 }
1514 1514 start = subParse(text, start, tag, count, obeyCount,
1515 1515 ambiguousYear, pos,
1516 1516 useFollowingMinusSignAsDelimiter, calb);
1517 1517 if (start < 0) {
1518 1518 pos.index = oldStart;
1519 1519 return null;
1520 1520 }
1521 1521 }
1522 1522 }
1523 1523
1524 1524 // At this point the fields of Calendar have been set. Calendar
1525 1525 // will fill in default values for missing fields when the time
1526 1526 // is computed.
1527 1527
1528 1528 pos.index = start;
1529 1529
1530 1530 Date parsedDate;
1531 1531 try {
1532 1532 parsedDate = calb.establish(calendar).getTime();
1533 1533 // If the year value is ambiguous,
1534 1534 // then the two-digit year == the default start year
1535 1535 if (ambiguousYear[0]) {
1536 1536 if (parsedDate.before(defaultCenturyStart)) {
1537 1537 parsedDate = calb.addYear(100).establish(calendar).getTime();
1538 1538 }
1539 1539 }
1540 1540 }
1541 1541 // An IllegalArgumentException will be thrown by Calendar.getTime()
1542 1542 // if any fields are out of range, e.g., MONTH == 17.
1543 1543 catch (IllegalArgumentException e) {
1544 1544 pos.errorIndex = start;
1545 1545 pos.index = oldStart;
1546 1546 return null;
1547 1547 }
1548 1548
1549 1549 return parsedDate;
1550 1550 }
1551 1551
1552 1552 /**
1553 1553 * Private code-size reduction function used by subParse.
1554 1554 * @param text the time text being parsed.
1555 1555 * @param start where to start parsing.
1556 1556 * @param field the date field being parsed.
1557 1557 * @param data the string array to parsed.
1558 1558 * @return the new start position if matching succeeded; a negative number
1559 1559 * indicating matching failure, otherwise.
1560 1560 */
1561 1561 private int matchString(String text, int start, int field, String[] data, CalendarBuilder calb)
1562 1562 {
1563 1563 int i = 0;
1564 1564 int count = data.length;
1565 1565
1566 1566 if (field == Calendar.DAY_OF_WEEK) {
1567 1567 i = 1;
1568 1568 }
1569 1569
1570 1570 // There may be multiple strings in the data[] array which begin with
1571 1571 // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
1572 1572 // We keep track of the longest match, and return that. Note that this
1573 1573 // unfortunately requires us to test all array elements.
1574 1574 int bestMatchLength = 0, bestMatch = -1;
1575 1575 for (; i<count; ++i)
1576 1576 {
1577 1577 int length = data[i].length();
1578 1578 // Always compare if we have no match yet; otherwise only compare
1579 1579 // against potentially better matches (longer strings).
1580 1580 if (length > bestMatchLength &&
1581 1581 text.regionMatches(true, start, data[i], 0, length))
1582 1582 {
1583 1583 bestMatch = i;
1584 1584 bestMatchLength = length;
1585 1585 }
1586 1586 }
1587 1587 if (bestMatch >= 0)
1588 1588 {
1589 1589 calb.set(field, bestMatch);
1590 1590 return start + bestMatchLength;
1591 1591 }
1592 1592 return -start;
1593 1593 }
1594 1594
1595 1595 /**
1596 1596 * Performs the same thing as matchString(String, int, int,
1597 1597 * String[]). This method takes a Map<String, Integer> instead of
1598 1598 * String[].
1599 1599 */
1600 1600 private int matchString(String text, int start, int field,
1601 1601 Map<String,Integer> data, CalendarBuilder calb) {
1602 1602 if (data != null) {
1603 1603 // TODO: make this default when it's in the spec.
1604 1604 if (data instanceof SortedMap) {
1605 1605 for (String name : data.keySet()) {
1606 1606 if (text.regionMatches(true, start, name, 0, name.length())) {
1607 1607 calb.set(field, data.get(name));
1608 1608 return start + name.length();
1609 1609 }
1610 1610 }
1611 1611 return -start;
1612 1612 }
1613 1613
1614 1614 String bestMatch = null;
1615 1615
1616 1616 for (String name : data.keySet()) {
1617 1617 int length = name.length();
1618 1618 if (bestMatch == null || length > bestMatch.length()) {
1619 1619 if (text.regionMatches(true, start, name, 0, length)) {
1620 1620 bestMatch = name;
1621 1621 }
1622 1622 }
1623 1623 }
1624 1624
1625 1625 if (bestMatch != null) {
1626 1626 calb.set(field, data.get(bestMatch));
1627 1627 return start + bestMatch.length();
1628 1628 }
1629 1629 }
1630 1630 return -start;
1631 1631 }
1632 1632
1633 1633 private int matchZoneString(String text, int start, String[] zoneNames) {
1634 1634 for (int i = 1; i <= 4; ++i) {
1635 1635 // Checking long and short zones [1 & 2],
1636 1636 // and long and short daylight [3 & 4].
1637 1637 String zoneName = zoneNames[i];
1638 1638 if (text.regionMatches(true, start,
1639 1639 zoneName, 0, zoneName.length())) {
1640 1640 return i;
1641 1641 }
1642 1642 }
1643 1643 return -1;
1644 1644 }
1645 1645
1646 1646 private boolean matchDSTString(String text, int start, int zoneIndex, int standardIndex,
1647 1647 String[][] zoneStrings) {
1648 1648 int index = standardIndex + 2;
1649 1649 String zoneName = zoneStrings[zoneIndex][index];
1650 1650 if (text.regionMatches(true, start,
1651 1651 zoneName, 0, zoneName.length())) {
1652 1652 return true;
1653 1653 }
1654 1654 return false;
1655 1655 }
1656 1656
1657 1657 /**
1658 1658 * find time zone 'text' matched zoneStrings and set to internal
1659 1659 * calendar.
1660 1660 */
1661 1661 private int subParseZoneString(String text, int start, CalendarBuilder calb) {
1662 1662 boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
1663 1663 TimeZone currentTimeZone = getTimeZone();
1664 1664
1665 1665 // At this point, check for named time zones by looking through
1666 1666 // the locale data from the TimeZoneNames strings.
1667 1667 // Want to be able to parse both short and long forms.
1668 1668 int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
1669 1669 TimeZone tz = null;
1670 1670 String[][] zoneStrings = formatData.getZoneStringsWrapper();
1671 1671 String[] zoneNames = null;
1672 1672 int nameIndex = 0;
1673 1673 if (zoneIndex != -1) {
1674 1674 zoneNames = zoneStrings[zoneIndex];
1675 1675 if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
1676 1676 if (nameIndex <= 2) {
1677 1677 // Check if the standard name (abbr) and the daylight name are the same.
1678 1678 useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
1679 1679 }
1680 1680 tz = TimeZone.getTimeZone(zoneNames[0]);
1681 1681 }
1682 1682 }
1683 1683 if (tz == null) {
1684 1684 zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
1685 1685 if (zoneIndex != -1) {
1686 1686 zoneNames = zoneStrings[zoneIndex];
1687 1687 if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
1688 1688 if (nameIndex <= 2) {
1689 1689 useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
1690 1690 }
1691 1691 tz = TimeZone.getTimeZone(zoneNames[0]);
1692 1692 }
1693 1693 }
1694 1694 }
1695 1695
1696 1696 if (tz == null) {
1697 1697 int len = zoneStrings.length;
1698 1698 for (int i = 0; i < len; i++) {
1699 1699 zoneNames = zoneStrings[i];
1700 1700 if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
1701 1701 if (nameIndex <= 2) {
1702 1702 useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
1703 1703 }
1704 1704 tz = TimeZone.getTimeZone(zoneNames[0]);
1705 1705 break;
1706 1706 }
1707 1707 }
1708 1708 }
1709 1709 if (tz != null) { // Matched any ?
1710 1710 if (!tz.equals(currentTimeZone)) {
1711 1711 setTimeZone(tz);
1712 1712 }
1713 1713 // If the time zone matched uses the same name
1714 1714 // (abbreviation) for both standard and daylight time,
1715 1715 // let the time zone in the Calendar decide which one.
1716 1716 //
1717 1717 // Also if tz.getDSTSaving() returns 0 for DST, use tz to
1718 1718 // determine the local time. (6645292)
1719 1719 int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
1720 1720 if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
1721 1721 calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
1722 1722 }
1723 1723 return (start + zoneNames[nameIndex].length());
1724 1724 }
1725 1725 return 0;
1726 1726 }
1727 1727
1728 1728 /**
1729 1729 * Parses numeric forms of time zone offset, such as "hh:mm", and
1730 1730 * sets calb to the parsed value.
1731 1731 *
1732 1732 * @param text the text to be parsed
1733 1733 * @param start the character position to start parsing
1734 1734 * @param sign 1: positive; -1: negative
1735 1735 * @param count 0: 'Z' or "GMT+hh:mm" parsing; 1 - 3: the number of 'X's
1736 1736 * @param colon true - colon required between hh and mm; false - no colon required
1737 1737 * @param calb a CalendarBuilder in which the parsed value is stored
1738 1738 * @return updated parsed position, or its negative value to indicate a parsing error
1739 1739 */
1740 1740 private int subParseNumericZone(String text, int start, int sign, int count,
1741 1741 boolean colon, CalendarBuilder calb) {
1742 1742 int index = start;
1743 1743
1744 1744 parse:
1745 1745 try {
1746 1746 char c = text.charAt(index++);
1747 1747 // Parse hh
1748 1748 int hours;
1749 1749 if (!isDigit(c)) {
1750 1750 break parse;
1751 1751 }
1752 1752 hours = c - '0';
1753 1753 c = text.charAt(index++);
1754 1754 if (isDigit(c)) {
1755 1755 hours = hours * 10 + (c - '0');
1756 1756 } else {
1757 1757 // If no colon in RFC 822 or 'X' (ISO), two digits are
1758 1758 // required.
1759 1759 if (count > 0 || !colon) {
1760 1760 break parse;
1761 1761 }
1762 1762 --index;
1763 1763 }
1764 1764 if (hours > 23) {
1765 1765 break parse;
1766 1766 }
1767 1767 int minutes = 0;
1768 1768 if (count != 1) {
1769 1769 // Proceed with parsing mm
1770 1770 c = text.charAt(index++);
1771 1771 if (colon) {
1772 1772 if (c != ':') {
1773 1773 break parse;
1774 1774 }
1775 1775 c = text.charAt(index++);
1776 1776 }
1777 1777 if (!isDigit(c)) {
1778 1778 break parse;
1779 1779 }
1780 1780 minutes = c - '0';
1781 1781 c = text.charAt(index++);
1782 1782 if (!isDigit(c)) {
1783 1783 break parse;
1784 1784 }
1785 1785 minutes = minutes * 10 + (c - '0');
1786 1786 if (minutes > 59) {
1787 1787 break parse;
1788 1788 }
1789 1789 }
1790 1790 minutes += hours * 60;
1791 1791 calb.set(Calendar.ZONE_OFFSET, minutes * MILLIS_PER_MINUTE * sign)
1792 1792 .set(Calendar.DST_OFFSET, 0);
1793 1793 return index;
1794 1794 } catch (IndexOutOfBoundsException e) {
1795 1795 }
1796 1796 return 1 - index; // -(index - 1)
1797 1797 }
1798 1798
1799 1799 private boolean isDigit(char c) {
1800 1800 return c >= '0' && c <= '9';
1801 1801 }
1802 1802
1803 1803 /**
1804 1804 * Private member function that converts the parsed date strings into
1805 1805 * timeFields. Returns -start (for ParsePosition) if failed.
1806 1806 * @param text the time text to be parsed.
1807 1807 * @param start where to start parsing.
1808 1808 * @param patternCharIndex the index of the pattern character.
1809 1809 * @param count the count of a pattern character.
1810 1810 * @param obeyCount if true, then the next field directly abuts this one,
1811 1811 * and we should use the count to know when to stop parsing.
1812 1812 * @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
1813 1813 * is true, then a two-digit year was parsed and may need to be readjusted.
1814 1814 * @param origPos origPos.errorIndex is used to return an error index
1815 1815 * at which a parse error occurred, if matching failure occurs.
1816 1816 * @return the new start position if matching succeeded; -1 indicating
1817 1817 * matching failure, otherwise. In case matching failure occurred,
1818 1818 * an error index is set to origPos.errorIndex.
1819 1819 */
1820 1820 private int subParse(String text, int start, int patternCharIndex, int count,
1821 1821 boolean obeyCount, boolean[] ambiguousYear,
1822 1822 ParsePosition origPos,
1823 1823 boolean useFollowingMinusSignAsDelimiter, CalendarBuilder calb) {
1824 1824 Number number;
1825 1825 int value = 0;
1826 1826 ParsePosition pos = new ParsePosition(0);
1827 1827 pos.index = start;
1828 1828 if (patternCharIndex == PATTERN_WEEK_YEAR && !calendar.isWeekDateSupported()) {
1829 1829 // use calendar year 'y' instead
1830 1830 patternCharIndex = PATTERN_YEAR;
1831 1831 }
1832 1832 int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1833 1833
1834 1834 // If there are any spaces here, skip over them. If we hit the end
1835 1835 // of the string, then fail.
1836 1836 for (;;) {
1837 1837 if (pos.index >= text.length()) {
1838 1838 origPos.errorIndex = start;
1839 1839 return -1;
1840 1840 }
1841 1841 char c = text.charAt(pos.index);
1842 1842 if (c != ' ' && c != '\t') {
1843 1843 break;
1844 1844 }
1845 1845 ++pos.index;
1846 1846 }
1847 1847 // Remember the actual start index
1848 1848 int actualStart = pos.index;
1849 1849
1850 1850 parsing:
1851 1851 {
1852 1852 // We handle a few special cases here where we need to parse
1853 1853 // a number value. We handle further, more generic cases below. We need
1854 1854 // to handle some of them here because some fields require extra processing on
1855 1855 // the parsed value.
1856 1856 if (patternCharIndex == PATTERN_HOUR_OF_DAY1 ||
1857 1857 patternCharIndex == PATTERN_HOUR1 ||
1858 1858 (patternCharIndex == PATTERN_MONTH && count <= 2) ||
1859 1859 patternCharIndex == PATTERN_YEAR ||
1860 1860 patternCharIndex == PATTERN_WEEK_YEAR) {
1861 1861 // It would be good to unify this with the obeyCount logic below,
1862 1862 // but that's going to be difficult.
1863 1863 if (obeyCount) {
1864 1864 if ((start+count) > text.length()) {
1865 1865 break parsing;
1866 1866 }
1867 1867 number = numberFormat.parse(text.substring(0, start+count), pos);
1868 1868 } else {
1869 1869 number = numberFormat.parse(text, pos);
1870 1870 }
1871 1871 if (number == null) {
1872 1872 if (patternCharIndex != PATTERN_YEAR || calendar instanceof GregorianCalendar) {
1873 1873 break parsing;
1874 1874 }
1875 1875 } else {
1876 1876 value = number.intValue();
1877 1877
1878 1878 if (useFollowingMinusSignAsDelimiter && (value < 0) &&
1879 1879 (((pos.index < text.length()) &&
1880 1880 (text.charAt(pos.index) != minusSign)) ||
1881 1881 ((pos.index == text.length()) &&
1882 1882 (text.charAt(pos.index-1) == minusSign)))) {
1883 1883 value = -value;
1884 1884 pos.index--;
1885 1885 }
1886 1886 }
1887 1887 }
1888 1888
1889 1889 boolean useDateFormatSymbols = useDateFormatSymbols();
1890 1890
1891 1891 int index;
1892 1892 switch (patternCharIndex) {
1893 1893 case PATTERN_ERA: // 'G'
1894 1894 if (useDateFormatSymbols) {
1895 1895 if ((index = matchString(text, start, Calendar.ERA, formatData.getEras(), calb)) > 0) {
1896 1896 return index;
1897 1897 }
1898 1898 } else {
1899 1899 Map<String, Integer> map = getDisplayNamesMap(field, locale);
1900 1900 if ((index = matchString(text, start, field, map, calb)) > 0) {
1901 1901 return index;
1902 1902 }
1903 1903 }
1904 1904 break parsing;
1905 1905
1906 1906 case PATTERN_WEEK_YEAR: // 'Y'
1907 1907 case PATTERN_YEAR: // 'y'
1908 1908 if (!(calendar instanceof GregorianCalendar)) {
1909 1909 // calendar might have text representations for year values,
1910 1910 // such as "\u5143" in JapaneseImperialCalendar.
1911 1911 int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
1912 1912 Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
1913 1913 if (map != null) {
1914 1914 if ((index = matchString(text, start, field, map, calb)) > 0) {
1915 1915 return index;
1916 1916 }
1917 1917 }
1918 1918 calb.set(field, value);
1919 1919 return pos.index;
1920 1920 }
1921 1921
1922 1922 // If there are 3 or more YEAR pattern characters, this indicates
1923 1923 // that the year value is to be treated literally, without any
1924 1924 // two-digit year adjustments (e.g., from "01" to 2001). Otherwise
1925 1925 // we made adjustments to place the 2-digit year in the proper
1926 1926 // century, for parsed strings from "00" to "99". Any other string
1927 1927 // is treated literally: "2250", "-1", "1", "002".
1928 1928 if (count <= 2 && (pos.index - actualStart) == 2
1929 1929 && Character.isDigit(text.charAt(actualStart))
1930 1930 && Character.isDigit(text.charAt(actualStart + 1))) {
1931 1931 // Assume for example that the defaultCenturyStart is 6/18/1903.
1932 1932 // This means that two-digit years will be forced into the range
1933 1933 // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
1934 1934 // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
1935 1935 // to 1904, 1905, etc. If the year is 03, then it is 2003 if the
1936 1936 // other fields specify a date before 6/18, or 1903 if they specify a
1937 1937 // date afterwards. As a result, 03 is an ambiguous year. All other
1938 1938 // two-digit years are unambiguous.
1939 1939 int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
1940 1940 ambiguousYear[0] = value == ambiguousTwoDigitYear;
1941 1941 value += (defaultCenturyStartYear/100)*100 +
1942 1942 (value < ambiguousTwoDigitYear ? 100 : 0);
1943 1943 }
1944 1944 calb.set(field, value);
1945 1945 return pos.index;
1946 1946
1947 1947 case PATTERN_MONTH: // 'M'
1948 1948 if (count <= 2) // i.e., M or MM.
1949 1949 {
1950 1950 // Don't want to parse the month if it is a string
1951 1951 // while pattern uses numeric style: M or MM.
1952 1952 // [We computed 'value' above.]
1953 1953 calb.set(Calendar.MONTH, value - 1);
1954 1954 return pos.index;
1955 1955 }
1956 1956
1957 1957 if (useDateFormatSymbols) {
1958 1958 // count >= 3 // i.e., MMM or MMMM
1959 1959 // Want to be able to parse both short and long forms.
1960 1960 // Try count == 4 first:
1961 1961 int newStart;
1962 1962 if ((newStart = matchString(text, start, Calendar.MONTH,
1963 1963 formatData.getMonths(), calb)) > 0) {
1964 1964 return newStart;
1965 1965 }
1966 1966 // count == 4 failed, now try count == 3
1967 1967 if ((index = matchString(text, start, Calendar.MONTH,
1968 1968 formatData.getShortMonths(), calb)) > 0) {
1969 1969 return index;
1970 1970 }
1971 1971 } else {
1972 1972 Map<String, Integer> map = getDisplayNamesMap(field, locale);
1973 1973 if ((index = matchString(text, start, field, map, calb)) > 0) {
1974 1974 return index;
1975 1975 }
1976 1976 }
1977 1977 break parsing;
1978 1978
1979 1979 case PATTERN_HOUR_OF_DAY1: // 'k' 1-based. eg, 23:59 + 1 hour =>> 24:59
1980 1980 if (!isLenient()) {
1981 1981 // Validate the hour value in non-lenient
1982 1982 if (value < 1 || value > 24) {
1983 1983 break parsing;
1984 1984 }
1985 1985 }
1986 1986 // [We computed 'value' above.]
1987 1987 if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY) + 1) {
1988 1988 value = 0;
1989 1989 }
1990 1990 calb.set(Calendar.HOUR_OF_DAY, value);
1991 1991 return pos.index;
1992 1992
1993 1993 case PATTERN_DAY_OF_WEEK: // 'E'
1994 1994 {
1995 1995 if (useDateFormatSymbols) {
1996 1996 // Want to be able to parse both short and long forms.
1997 1997 // Try count == 4 (DDDD) first:
1998 1998 int newStart;
1999 1999 if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
2000 2000 formatData.getWeekdays(), calb)) > 0) {
2001 2001 return newStart;
2002 2002 }
2003 2003 // DDDD failed, now try DDD
2004 2004 if ((index = matchString(text, start, Calendar.DAY_OF_WEEK,
2005 2005 formatData.getShortWeekdays(), calb)) > 0) {
2006 2006 return index;
2007 2007 }
2008 2008 } else {
2009 2009 int[] styles = { Calendar.LONG, Calendar.SHORT };
2010 2010 for (int style : styles) {
2011 2011 Map<String,Integer> map = calendar.getDisplayNames(field, style, locale);
2012 2012 if ((index = matchString(text, start, field, map, calb)) > 0) {
2013 2013 return index;
2014 2014 }
2015 2015 }
2016 2016 }
2017 2017 }
2018 2018 break parsing;
2019 2019
2020 2020 case PATTERN_AM_PM: // 'a'
2021 2021 if (useDateFormatSymbols) {
2022 2022 if ((index = matchString(text, start, Calendar.AM_PM,
2023 2023 formatData.getAmPmStrings(), calb)) > 0) {
2024 2024 return index;
2025 2025 }
2026 2026 } else {
2027 2027 Map<String,Integer> map = getDisplayNamesMap(field, locale);
2028 2028 if ((index = matchString(text, start, field, map, calb)) > 0) {
2029 2029 return index;
2030 2030 }
2031 2031 }
2032 2032 break parsing;
2033 2033
2034 2034 case PATTERN_HOUR1: // 'h' 1-based. eg, 11PM + 1 hour =>> 12 AM
2035 2035 if (!isLenient()) {
2036 2036 // Validate the hour value in non-lenient
2037 2037 if (value < 1 || value > 12) {
2038 2038 break parsing;
2039 2039 }
2040 2040 }
2041 2041 // [We computed 'value' above.]
2042 2042 if (value == calendar.getLeastMaximum(Calendar.HOUR) + 1) {
2043 2043 value = 0;
2044 2044 }
2045 2045 calb.set(Calendar.HOUR, value);
2046 2046 return pos.index;
2047 2047
2048 2048 case PATTERN_ZONE_NAME: // 'z'
2049 2049 case PATTERN_ZONE_VALUE: // 'Z'
2050 2050 {
2051 2051 int sign = 0;
2052 2052 try {
2053 2053 char c = text.charAt(pos.index);
2054 2054 if (c == '+') {
2055 2055 sign = 1;
2056 2056 } else if (c == '-') {
2057 2057 sign = -1;
2058 2058 }
2059 2059 if (sign == 0) {
2060 2060 // Try parsing a custom time zone "GMT+hh:mm" or "GMT".
2061 2061 if ((c == 'G' || c == 'g')
2062 2062 && (text.length() - start) >= GMT.length()
2063 2063 && text.regionMatches(true, start, GMT, 0, GMT.length())) {
2064 2064 pos.index = start + GMT.length();
2065 2065
2066 2066 if ((text.length() - pos.index) > 0) {
2067 2067 c = text.charAt(pos.index);
2068 2068 if (c == '+') {
2069 2069 sign = 1;
2070 2070 } else if (c == '-') {
2071 2071 sign = -1;
2072 2072 }
2073 2073 }
2074 2074
2075 2075 if (sign == 0) { /* "GMT" without offset */
2076 2076 calb.set(Calendar.ZONE_OFFSET, 0)
2077 2077 .set(Calendar.DST_OFFSET, 0);
2078 2078 return pos.index;
2079 2079 }
2080 2080
2081 2081 // Parse the rest as "hh:mm"
2082 2082 int i = subParseNumericZone(text, ++pos.index,
2083 2083 sign, 0, true, calb);
2084 2084 if (i > 0) {
2085 2085 return i;
2086 2086 }
2087 2087 pos.index = -i;
2088 2088 } else {
2089 2089 // Try parsing the text as a time zone
2090 2090 // name or abbreviation.
2091 2091 int i = subParseZoneString(text, pos.index, calb);
2092 2092 if (i > 0) {
2093 2093 return i;
2094 2094 }
2095 2095 pos.index = -i;
2096 2096 }
2097 2097 } else {
2098 2098 // Parse the rest as "hhmm" (RFC 822)
2099 2099 int i = subParseNumericZone(text, ++pos.index,
2100 2100 sign, 0, false, calb);
2101 2101 if (i > 0) {
2102 2102 return i;
2103 2103 }
2104 2104 pos.index = -i;
2105 2105 }
2106 2106 } catch (IndexOutOfBoundsException e) {
2107 2107 }
2108 2108 }
2109 2109 break parsing;
2110 2110
2111 2111 case PATTERN_ISO_ZONE: // 'X'
2112 2112 {
2113 2113 if ((text.length() - pos.index) <= 0) {
2114 2114 break parsing;
2115 2115 }
2116 2116
2117 2117 int sign;
2118 2118 char c = text.charAt(pos.index);
2119 2119 if (c == 'Z') {
2120 2120 calb.set(Calendar.ZONE_OFFSET, 0).set(Calendar.DST_OFFSET, 0);
2121 2121 return ++pos.index;
2122 2122 }
2123 2123
2124 2124 // parse text as "+/-hh[[:]mm]" based on count
2125 2125 if (c == '+') {
2126 2126 sign = 1;
2127 2127 } else if (c == '-') {
2128 2128 sign = -1;
2129 2129 } else {
2130 2130 ++pos.index;
2131 2131 break parsing;
2132 2132 }
2133 2133 int i = subParseNumericZone(text, ++pos.index, sign, count,
2134 2134 count == 3, calb);
2135 2135 if (i > 0) {
2136 2136 return i;
2137 2137 }
2138 2138 pos.index = -i;
2139 2139 }
2140 2140 break parsing;
2141 2141
2142 2142 default:
2143 2143 // case PATTERN_DAY_OF_MONTH: // 'd'
2144 2144 // case PATTERN_HOUR_OF_DAY0: // 'H' 0-based. eg, 23:59 + 1 hour =>> 00:59
2145 2145 // case PATTERN_MINUTE: // 'm'
2146 2146 // case PATTERN_SECOND: // 's'
2147 2147 // case PATTERN_MILLISECOND: // 'S'
2148 2148 // case PATTERN_DAY_OF_YEAR: // 'D'
2149 2149 // case PATTERN_DAY_OF_WEEK_IN_MONTH: // 'F'
2150 2150 // case PATTERN_WEEK_OF_YEAR: // 'w'
2151 2151 // case PATTERN_WEEK_OF_MONTH: // 'W'
2152 2152 // case PATTERN_HOUR0: // 'K' 0-based. eg, 11PM + 1 hour =>> 0 AM
2153 2153 // case PATTERN_ISO_DAY_OF_WEEK: // 'u' (pseudo field);
2154 2154
2155 2155 // Handle "generic" fields
2156 2156 if (obeyCount) {
2157 2157 if ((start+count) > text.length()) {
2158 2158 break parsing;
2159 2159 }
2160 2160 number = numberFormat.parse(text.substring(0, start+count), pos);
2161 2161 } else {
2162 2162 number = numberFormat.parse(text, pos);
2163 2163 }
2164 2164 if (number != null) {
2165 2165 value = number.intValue();
2166 2166
2167 2167 if (useFollowingMinusSignAsDelimiter && (value < 0) &&
2168 2168 (((pos.index < text.length()) &&
2169 2169 (text.charAt(pos.index) != minusSign)) ||
2170 2170 ((pos.index == text.length()) &&
2171 2171 (text.charAt(pos.index-1) == minusSign)))) {
2172 2172 value = -value;
2173 2173 pos.index--;
2174 2174 }
2175 2175
2176 2176 calb.set(field, value);
2177 2177 return pos.index;
2178 2178 }
2179 2179 break parsing;
2180 2180 }
2181 2181 }
2182 2182
2183 2183 // Parsing failed.
2184 2184 origPos.errorIndex = pos.index;
2185 2185 return -1;
2186 2186 }
2187 2187
2188 2188 /**
2189 2189 * Returns true if the DateFormatSymbols has been set explicitly or locale
2190 2190 * is null.
2191 2191 */
2192 2192 private boolean useDateFormatSymbols() {
2193 2193 return useDateFormatSymbols || locale == null;
2194 2194 }
2195 2195
2196 2196 /**
2197 2197 * Translates a pattern, mapping each character in the from string to the
2198 2198 * corresponding character in the to string.
2199 2199 *
2200 2200 * @exception IllegalArgumentException if the given pattern is invalid
2201 2201 */
2202 2202 private String translatePattern(String pattern, String from, String to) {
2203 2203 StringBuilder result = new StringBuilder();
2204 2204 boolean inQuote = false;
2205 2205 for (int i = 0; i < pattern.length(); ++i) {
2206 2206 char c = pattern.charAt(i);
2207 2207 if (inQuote) {
2208 2208 if (c == '\'') {
2209 2209 inQuote = false;
2210 2210 }
2211 2211 }
2212 2212 else {
2213 2213 if (c == '\'') {
2214 2214 inQuote = true;
2215 2215 } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
2216 2216 int ci = from.indexOf(c);
2217 2217 if (ci >= 0) {
2218 2218 // patternChars is longer than localPatternChars due
2219 2219 // to serialization compatibility. The pattern letters
2220 2220 // unsupported by localPatternChars pass through.
2221 2221 if (ci < to.length()) {
2222 2222 c = to.charAt(ci);
2223 2223 }
2224 2224 } else {
2225 2225 throw new IllegalArgumentException("Illegal pattern " +
2226 2226 " character '" +
2227 2227 c + "'");
2228 2228 }
2229 2229 }
2230 2230 }
2231 2231 result.append(c);
2232 2232 }
2233 2233 if (inQuote) {
2234 2234 throw new IllegalArgumentException("Unfinished quote in pattern");
2235 2235 }
2236 2236 return result.toString();
2237 2237 }
2238 2238
2239 2239 /**
2240 2240 * Returns a pattern string describing this date format.
2241 2241 *
2242 2242 * @return a pattern string describing this date format.
2243 2243 */
2244 2244 public String toPattern() {
2245 2245 return pattern;
2246 2246 }
2247 2247
2248 2248 /**
2249 2249 * Returns a localized pattern string describing this date format.
2250 2250 *
2251 2251 * @return a localized pattern string describing this date format.
2252 2252 */
2253 2253 public String toLocalizedPattern() {
2254 2254 return translatePattern(pattern,
2255 2255 DateFormatSymbols.patternChars,
2256 2256 formatData.getLocalPatternChars());
2257 2257 }
2258 2258
2259 2259 /**
2260 2260 * Applies the given pattern string to this date format.
2261 2261 *
2262 2262 * @param pattern the new date and time pattern for this date format
2263 2263 * @exception NullPointerException if the given pattern is null
2264 2264 * @exception IllegalArgumentException if the given pattern is invalid
2265 2265 */
2266 2266 public void applyPattern(String pattern)
2267 2267 {
2268 2268 applyPatternImpl(pattern);
2269 2269 }
2270 2270
2271 2271 private void applyPatternImpl(String pattern) {
2272 2272 compiledPattern = compile(pattern);
2273 2273 this.pattern = pattern;
2274 2274 }
2275 2275
2276 2276 /**
2277 2277 * Applies the given localized pattern string to this date format.
2278 2278 *
2279 2279 * @param pattern a String to be mapped to the new date and time format
2280 2280 * pattern for this format
2281 2281 * @exception NullPointerException if the given pattern is null
2282 2282 * @exception IllegalArgumentException if the given pattern is invalid
2283 2283 */
2284 2284 public void applyLocalizedPattern(String pattern) {
2285 2285 String p = translatePattern(pattern,
2286 2286 formatData.getLocalPatternChars(),
2287 2287 DateFormatSymbols.patternChars);
2288 2288 compiledPattern = compile(p);
2289 2289 this.pattern = p;
2290 2290 }
2291 2291
2292 2292 /**
2293 2293 * Gets a copy of the date and time format symbols of this date format.
2294 2294 *
2295 2295 * @return the date and time format symbols of this date format
2296 2296 * @see #setDateFormatSymbols
2297 2297 */
2298 2298 public DateFormatSymbols getDateFormatSymbols()
2299 2299 {
2300 2300 return (DateFormatSymbols)formatData.clone();
2301 2301 }
2302 2302
2303 2303 /**
2304 2304 * Sets the date and time format symbols of this date format.
2305 2305 *
2306 2306 * @param newFormatSymbols the new date and time format symbols
2307 2307 * @exception NullPointerException if the given newFormatSymbols is null
2308 2308 * @see #getDateFormatSymbols
2309 2309 */
2310 2310 public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
2311 2311 {
2312 2312 this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
2313 2313 useDateFormatSymbols = true;
2314 2314 }
2315 2315
2316 2316 /**
2317 2317 * Creates a copy of this <code>SimpleDateFormat</code>. This also
2318 2318 * clones the format's date format symbols.
2319 2319 *
2320 2320 * @return a clone of this <code>SimpleDateFormat</code>
2321 2321 */
2322 2322 @Override
2323 2323 public Object clone() {
2324 2324 SimpleDateFormat other = (SimpleDateFormat) super.clone();
2325 2325 other.formatData = (DateFormatSymbols) formatData.clone();
2326 2326 return other;
2327 2327 }
2328 2328
2329 2329 /**
2330 2330 * Returns the hash code value for this <code>SimpleDateFormat</code> object.
2331 2331 *
2332 2332 * @return the hash code value for this <code>SimpleDateFormat</code> object.
2333 2333 */
2334 2334 @Override
2335 2335 public int hashCode()
2336 2336 {
2337 2337 return pattern.hashCode();
2338 2338 // just enough fields for a reasonable distribution
2339 2339 }
2340 2340
2341 2341 /**
2342 2342 * Compares the given object with this <code>SimpleDateFormat</code> for
2343 2343 * equality.
2344 2344 *
2345 2345 * @return true if the given object is equal to this
2346 2346 * <code>SimpleDateFormat</code>
2347 2347 */
2348 2348 @Override
2349 2349 public boolean equals(Object obj)
2350 2350 {
2351 2351 if (!super.equals(obj)) {
2352 2352 return false; // super does class check
2353 2353 }
2354 2354 SimpleDateFormat that = (SimpleDateFormat) obj;
2355 2355 return (pattern.equals(that.pattern)
2356 2356 && formatData.equals(that.formatData));
2357 2357 }
2358 2358
2359 2359 private static final int[] REST_OF_STYLES = {
2360 2360 Calendar.SHORT_STANDALONE, Calendar.LONG_FORMAT, Calendar.LONG_STANDALONE,
2361 2361 };
2362 2362 private Map<String, Integer> getDisplayNamesMap(int field, Locale locale) {
2363 2363 Map<String, Integer> map = calendar.getDisplayNames(field, Calendar.SHORT_FORMAT, locale);
2364 2364 // Get all SHORT and LONG styles (avoid NARROW styles).
2365 2365 for (int style : REST_OF_STYLES) {
2366 2366 Map<String, Integer> m = calendar.getDisplayNames(field, style, locale);
2367 2367 if (m != null) {
2368 2368 map.putAll(m);
2369 2369 }
2370 2370 }
2371 2371 return map;
2372 2372 }
2373 2373
2374 2374 /**
2375 2375 * After reading an object from the input stream, the format
2376 2376 * pattern in the object is verified.
2377 2377 * <p>
2378 2378 * @exception InvalidObjectException if the pattern is invalid
2379 2379 */
2380 2380 private void readObject(ObjectInputStream stream)
2381 2381 throws IOException, ClassNotFoundException {
2382 2382 stream.defaultReadObject();
2383 2383
2384 2384 try {
2385 2385 compiledPattern = compile(pattern);
2386 2386 } catch (Exception e) {
2387 2387 throw new InvalidObjectException("invalid pattern");
2388 2388 }
2389 2389
2390 2390 if (serialVersionOnStream < 1) {
2391 2391 // didn't have defaultCenturyStart field
2392 2392 initializeDefaultCentury();
2393 2393 }
2394 2394 else {
2395 2395 // fill in dependent transient field
2396 2396 parseAmbiguousDatesAsAfter(defaultCenturyStart);
2397 2397 }
2398 2398 serialVersionOnStream = currentSerialVersion;
2399 2399
2400 2400 // If the deserialized object has a SimpleTimeZone, try
2401 2401 // to replace it with a ZoneInfo equivalent in order to
2402 2402 // be compatible with the SimpleTimeZone-based
2403 2403 // implementation as much as possible.
2404 2404 TimeZone tz = getTimeZone();
2405 2405 if (tz instanceof SimpleTimeZone) {
2406 2406 String id = tz.getID();
2407 2407 TimeZone zi = TimeZone.getTimeZone(id);
2408 2408 if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
2409 2409 setTimeZone(zi);
2410 2410 }
2411 2411 }
2412 2412 }
2413 2413
2414 2414 /**
2415 2415 * Analyze the negative subpattern of DecimalFormat and set/update values
2416 2416 * as necessary.
2417 2417 */
2418 2418 private void checkNegativeNumberExpression() {
2419 2419 if ((numberFormat instanceof DecimalFormat) &&
2420 2420 !numberFormat.equals(originalNumberFormat)) {
2421 2421 String numberPattern = ((DecimalFormat)numberFormat).toPattern();
2422 2422 if (!numberPattern.equals(originalNumberPattern)) {
2423 2423 hasFollowingMinusSign = false;
2424 2424
2425 2425 int separatorIndex = numberPattern.indexOf(';');
2426 2426 // If the negative subpattern is not absent, we have to analayze
2427 2427 // it in order to check if it has a following minus sign.
2428 2428 if (separatorIndex > -1) {
2429 2429 int minusIndex = numberPattern.indexOf('-', separatorIndex);
2430 2430 if ((minusIndex > numberPattern.lastIndexOf('0')) &&
2431 2431 (minusIndex > numberPattern.lastIndexOf('#'))) {
2432 2432 hasFollowingMinusSign = true;
2433 2433 minusSign = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getMinusSign();
2434 2434 }
2435 2435 }
2436 2436 originalNumberPattern = numberPattern;
2437 2437 }
2438 2438 originalNumberFormat = numberFormat;
2439 2439 }
2440 2440 }
2441 2441
2442 2442 }
↓ open down ↓ |
2018 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX