Print this page
rev 5696 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o packaging changes. by Naoto Sato and Masayoshi Okutsu)
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/text/DateFormat.java
+++ new/src/share/classes/java/text/DateFormat.java
1 1 /*
2 2 * Copyright (c) 1996, 2010, 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 - 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.InvalidObjectException;
42 42 import java.text.spi.DateFormatProvider;
↓ open down ↓ |
42 lines elided |
↑ open up ↑ |
43 43 import java.util.Calendar;
44 44 import java.util.Date;
45 45 import java.util.GregorianCalendar;
46 46 import java.util.HashMap;
47 47 import java.util.Locale;
48 48 import java.util.Map;
49 49 import java.util.MissingResourceException;
50 50 import java.util.ResourceBundle;
51 51 import java.util.TimeZone;
52 52 import java.util.spi.LocaleServiceProvider;
53 -import sun.util.LocaleServiceProviderPool;
53 +import sun.util.locale.provider.LocaleProviderAdapter;
54 +import sun.util.locale.provider.LocaleServiceProviderPool;
54 55
55 56 /**
56 57 * {@code DateFormat} is an abstract class for date/time formatting subclasses which
57 58 * formats and parses dates or time in a language-independent manner.
58 59 * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
59 60 * formatting (i.e., date -> text), parsing (text -> date), and
60 61 * normalization. The date is represented as a <code>Date</code> object or
61 62 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
62 63 *
63 64 * <p>{@code DateFormat} provides many class methods for obtaining default date/time
64 65 * formatters based on the default or a given locale and a number of formatting
65 66 * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
66 67 * detail and examples of using these styles are provided in the method
67 68 * descriptions.
68 69 *
69 70 * <p>{@code DateFormat} helps you to format and parse dates for any locale.
70 71 * Your code can be completely independent of the locale conventions for
71 72 * months, days of the week, or even the calendar format: lunar vs. solar.
72 73 *
73 74 * <p>To format a date for the current Locale, use one of the
74 75 * static factory methods:
75 76 * <pre>
76 77 * myString = DateFormat.getDateInstance().format(myDate);
77 78 * </pre>
78 79 * <p>If you are formatting multiple dates, it is
79 80 * more efficient to get the format and use it multiple times so that
80 81 * the system doesn't have to fetch the information about the local
81 82 * language and country conventions multiple times.
82 83 * <pre>
83 84 * DateFormat df = DateFormat.getDateInstance();
84 85 * for (int i = 0; i < myDate.length; ++i) {
85 86 * output.println(df.format(myDate[i]) + "; ");
86 87 * }
87 88 * </pre>
88 89 * <p>To format a date for a different Locale, specify it in the
89 90 * call to {@link #getDateInstance(int, Locale) getDateInstance()}.
90 91 * <pre>
91 92 * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
92 93 * </pre>
93 94 * <p>You can use a DateFormat to parse also.
94 95 * <pre>
95 96 * myDate = df.parse(myString);
96 97 * </pre>
97 98 * <p>Use {@code getDateInstance} to get the normal date format for that country.
98 99 * There are other static factory methods available.
99 100 * Use {@code getTimeInstance} to get the time format for that country.
100 101 * Use {@code getDateTimeInstance} to get a date and time format. You can pass in
101 102 * different options to these factory methods to control the length of the
102 103 * result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}. The exact result depends
103 104 * on the locale, but generally:
104 105 * <ul><li>{@link #SHORT} is completely numeric, such as {@code 12.13.52} or {@code 3:30pm}
105 106 * <li>{@link #MEDIUM} is longer, such as {@code Jan 12, 1952}
106 107 * <li>{@link #LONG} is longer, such as {@code January 12, 1952} or {@code 3:30:32pm}
107 108 * <li>{@link #FULL} is pretty completely specified, such as
108 109 * {@code Tuesday, April 12, 1952 AD or 3:30:42pm PST}.
109 110 * </ul>
110 111 *
111 112 * <p>You can also set the time zone on the format if you wish.
112 113 * If you want even more control over the format or parsing,
113 114 * (or want to give your users more control),
114 115 * you can try casting the {@code DateFormat} you get from the factory methods
115 116 * to a {@link SimpleDateFormat}. This will work for the majority
116 117 * of countries; just remember to put it in a {@code try} block in case you
117 118 * encounter an unusual one.
118 119 *
119 120 * <p>You can also use forms of the parse and format methods with
120 121 * {@link ParsePosition} and {@link FieldPosition} to
121 122 * allow you to
122 123 * <ul><li>progressively parse through pieces of a string.
123 124 * <li>align any particular field, or find out where it is for selection
124 125 * on the screen.
125 126 * </ul>
126 127 *
127 128 * <h4><a name="synchronization">Synchronization</a></h4>
128 129 *
129 130 * <p>
130 131 * Date formats are not synchronized.
131 132 * It is recommended to create separate format instances for each thread.
132 133 * If multiple threads access a format concurrently, it must be synchronized
133 134 * externally.
134 135 *
135 136 * @see Format
136 137 * @see NumberFormat
137 138 * @see SimpleDateFormat
138 139 * @see java.util.Calendar
139 140 * @see java.util.GregorianCalendar
140 141 * @see java.util.TimeZone
141 142 * @author Mark Davis, Chen-Lieh Huang, Alan Liu
142 143 */
143 144 public abstract class DateFormat extends Format {
144 145
145 146 /**
146 147 * The {@link Calendar} instance used for calculating the date-time fields
147 148 * and the instant of time. This field is used for both formatting and
148 149 * parsing.
149 150 *
150 151 * <p>Subclasses should initialize this field to a {@link Calendar}
151 152 * appropriate for the {@link Locale} associated with this
152 153 * <code>DateFormat</code>.
153 154 * @serial
154 155 */
155 156 protected Calendar calendar;
156 157
157 158 /**
158 159 * The number formatter that <code>DateFormat</code> uses to format numbers
159 160 * in dates and times. Subclasses should initialize this to a number format
160 161 * appropriate for the locale associated with this <code>DateFormat</code>.
161 162 * @serial
162 163 */
163 164 protected NumberFormat numberFormat;
164 165
165 166 /**
166 167 * Useful constant for ERA field alignment.
167 168 * Used in FieldPosition of date/time formatting.
168 169 */
169 170 public final static int ERA_FIELD = 0;
170 171 /**
171 172 * Useful constant for YEAR field alignment.
172 173 * Used in FieldPosition of date/time formatting.
173 174 */
174 175 public final static int YEAR_FIELD = 1;
175 176 /**
176 177 * Useful constant for MONTH field alignment.
177 178 * Used in FieldPosition of date/time formatting.
178 179 */
179 180 public final static int MONTH_FIELD = 2;
180 181 /**
181 182 * Useful constant for DATE field alignment.
182 183 * Used in FieldPosition of date/time formatting.
183 184 */
184 185 public final static int DATE_FIELD = 3;
185 186 /**
186 187 * Useful constant for one-based HOUR_OF_DAY field alignment.
187 188 * Used in FieldPosition of date/time formatting.
188 189 * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
189 190 * For example, 23:59 + 01:00 results in 24:59.
190 191 */
191 192 public final static int HOUR_OF_DAY1_FIELD = 4;
192 193 /**
193 194 * Useful constant for zero-based HOUR_OF_DAY field alignment.
194 195 * Used in FieldPosition of date/time formatting.
195 196 * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
196 197 * For example, 23:59 + 01:00 results in 00:59.
197 198 */
198 199 public final static int HOUR_OF_DAY0_FIELD = 5;
199 200 /**
200 201 * Useful constant for MINUTE field alignment.
201 202 * Used in FieldPosition of date/time formatting.
202 203 */
203 204 public final static int MINUTE_FIELD = 6;
204 205 /**
205 206 * Useful constant for SECOND field alignment.
206 207 * Used in FieldPosition of date/time formatting.
207 208 */
208 209 public final static int SECOND_FIELD = 7;
209 210 /**
210 211 * Useful constant for MILLISECOND field alignment.
211 212 * Used in FieldPosition of date/time formatting.
212 213 */
213 214 public final static int MILLISECOND_FIELD = 8;
214 215 /**
215 216 * Useful constant for DAY_OF_WEEK field alignment.
216 217 * Used in FieldPosition of date/time formatting.
217 218 */
218 219 public final static int DAY_OF_WEEK_FIELD = 9;
219 220 /**
220 221 * Useful constant for DAY_OF_YEAR field alignment.
221 222 * Used in FieldPosition of date/time formatting.
222 223 */
223 224 public final static int DAY_OF_YEAR_FIELD = 10;
224 225 /**
225 226 * Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
226 227 * Used in FieldPosition of date/time formatting.
227 228 */
228 229 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
229 230 /**
230 231 * Useful constant for WEEK_OF_YEAR field alignment.
231 232 * Used in FieldPosition of date/time formatting.
232 233 */
233 234 public final static int WEEK_OF_YEAR_FIELD = 12;
234 235 /**
235 236 * Useful constant for WEEK_OF_MONTH field alignment.
236 237 * Used in FieldPosition of date/time formatting.
237 238 */
238 239 public final static int WEEK_OF_MONTH_FIELD = 13;
239 240 /**
240 241 * Useful constant for AM_PM field alignment.
241 242 * Used in FieldPosition of date/time formatting.
242 243 */
243 244 public final static int AM_PM_FIELD = 14;
244 245 /**
245 246 * Useful constant for one-based HOUR field alignment.
246 247 * Used in FieldPosition of date/time formatting.
247 248 * HOUR1_FIELD is used for the one-based 12-hour clock.
248 249 * For example, 11:30 PM + 1 hour results in 12:30 AM.
249 250 */
250 251 public final static int HOUR1_FIELD = 15;
251 252 /**
252 253 * Useful constant for zero-based HOUR field alignment.
253 254 * Used in FieldPosition of date/time formatting.
254 255 * HOUR0_FIELD is used for the zero-based 12-hour clock.
255 256 * For example, 11:30 PM + 1 hour results in 00:30 AM.
256 257 */
257 258 public final static int HOUR0_FIELD = 16;
258 259 /**
259 260 * Useful constant for TIMEZONE field alignment.
260 261 * Used in FieldPosition of date/time formatting.
261 262 */
262 263 public final static int TIMEZONE_FIELD = 17;
263 264
264 265 // Proclaim serial compatibility with 1.1 FCS
265 266 private static final long serialVersionUID = 7218322306649953788L;
266 267
267 268 /**
268 269 * Overrides Format.
269 270 * Formats a time object into a time string. Examples of time objects
270 271 * are a time value expressed in milliseconds and a Date object.
271 272 * @param obj must be a Number or a Date.
272 273 * @param toAppendTo the string buffer for the returning time string.
273 274 * @return the string buffer passed in as toAppendTo, with formatted text appended.
274 275 * @param fieldPosition keeps track of the position of the field
275 276 * within the returned string.
276 277 * On input: an alignment field,
277 278 * if desired. On output: the offsets of the alignment field. For
278 279 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
279 280 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
280 281 * begin index and end index of fieldPosition will be set to
281 282 * 0 and 4, respectively.
282 283 * Notice that if the same time field appears
283 284 * more than once in a pattern, the fieldPosition will be set for the first
284 285 * occurrence of that time field. For instance, formatting a Date to
285 286 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
286 287 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
287 288 * the begin index and end index of fieldPosition will be set to
288 289 * 5 and 8, respectively, for the first occurrence of the timezone
289 290 * pattern character 'z'.
290 291 * @see java.text.Format
291 292 */
292 293 public final StringBuffer format(Object obj, StringBuffer toAppendTo,
293 294 FieldPosition fieldPosition)
294 295 {
295 296 if (obj instanceof Date)
296 297 return format( (Date)obj, toAppendTo, fieldPosition );
297 298 else if (obj instanceof Number)
298 299 return format( new Date(((Number)obj).longValue()),
299 300 toAppendTo, fieldPosition );
300 301 else
301 302 throw new IllegalArgumentException("Cannot format given Object as a Date");
302 303 }
303 304
304 305 /**
305 306 * Formats a Date into a date/time string.
306 307 * @param date a Date to be formatted into a date/time string.
307 308 * @param toAppendTo the string buffer for the returning date/time string.
308 309 * @param fieldPosition keeps track of the position of the field
309 310 * within the returned string.
310 311 * On input: an alignment field,
311 312 * if desired. On output: the offsets of the alignment field. For
312 313 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
313 314 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
314 315 * begin index and end index of fieldPosition will be set to
315 316 * 0 and 4, respectively.
316 317 * Notice that if the same time field appears
317 318 * more than once in a pattern, the fieldPosition will be set for the first
318 319 * occurrence of that time field. For instance, formatting a Date to
319 320 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
320 321 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
321 322 * the begin index and end index of fieldPosition will be set to
322 323 * 5 and 8, respectively, for the first occurrence of the timezone
323 324 * pattern character 'z'.
324 325 * @return the string buffer passed in as toAppendTo, with formatted text appended.
325 326 */
326 327 public abstract StringBuffer format(Date date, StringBuffer toAppendTo,
327 328 FieldPosition fieldPosition);
328 329
329 330 /**
330 331 * Formats a Date into a date/time string.
331 332 * @param date the time value to be formatted into a time string.
332 333 * @return the formatted time string.
333 334 */
334 335 public final String format(Date date)
335 336 {
336 337 return format(date, new StringBuffer(),
337 338 DontCareFieldPosition.INSTANCE).toString();
338 339 }
339 340
340 341 /**
341 342 * Parses text from the beginning of the given string to produce a date.
342 343 * The method may not use the entire text of the given string.
343 344 * <p>
344 345 * See the {@link #parse(String, ParsePosition)} method for more information
345 346 * on date parsing.
346 347 *
347 348 * @param source A <code>String</code> whose beginning should be parsed.
348 349 * @return A <code>Date</code> parsed from the string.
349 350 * @exception ParseException if the beginning of the specified string
350 351 * cannot be parsed.
351 352 */
352 353 public Date parse(String source) throws ParseException
353 354 {
354 355 ParsePosition pos = new ParsePosition(0);
355 356 Date result = parse(source, pos);
356 357 if (pos.index == 0)
357 358 throw new ParseException("Unparseable date: \"" + source + "\"" ,
358 359 pos.errorIndex);
359 360 return result;
360 361 }
361 362
362 363 /**
363 364 * Parse a date/time string according to the given parse position. For
364 365 * example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date}
365 366 * that is equivalent to {@code Date(837039900000L)}.
366 367 *
367 368 * <p> By default, parsing is lenient: If the input is not in the form used
368 369 * by this object's format method but can still be parsed as a date, then
369 370 * the parse succeeds. Clients may insist on strict adherence to the
370 371 * format by calling {@link #setLenient(boolean) setLenient(false)}.
371 372 *
372 373 * <p>This parsing operation uses the {@link #calendar} to produce
373 374 * a {@code Date}. As a result, the {@code calendar}'s date-time
374 375 * fields and the {@code TimeZone} value may have been
375 376 * overwritten, depending on subclass implementations. Any {@code
376 377 * TimeZone} value that has previously been set by a call to
377 378 * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
378 379 * to be restored for further operations.
379 380 *
380 381 * @param source The date/time string to be parsed
381 382 *
382 383 * @param pos On input, the position at which to start parsing; on
383 384 * output, the position at which parsing terminated, or the
384 385 * start position if the parse failed.
385 386 *
386 387 * @return A {@code Date}, or {@code null} if the input could not be parsed
387 388 */
388 389 public abstract Date parse(String source, ParsePosition pos);
389 390
390 391 /**
391 392 * Parses text from a string to produce a <code>Date</code>.
392 393 * <p>
393 394 * The method attempts to parse text starting at the index given by
394 395 * <code>pos</code>.
395 396 * If parsing succeeds, then the index of <code>pos</code> is updated
396 397 * to the index after the last character used (parsing does not necessarily
397 398 * use all characters up to the end of the string), and the parsed
398 399 * date is returned. The updated <code>pos</code> can be used to
399 400 * indicate the starting point for the next call to this method.
400 401 * If an error occurs, then the index of <code>pos</code> is not
401 402 * changed, the error index of <code>pos</code> is set to the index of
402 403 * the character where the error occurred, and null is returned.
403 404 * <p>
404 405 * See the {@link #parse(String, ParsePosition)} method for more information
405 406 * on date parsing.
406 407 *
407 408 * @param source A <code>String</code>, part of which should be parsed.
408 409 * @param pos A <code>ParsePosition</code> object with index and error
409 410 * index information as described above.
410 411 * @return A <code>Date</code> parsed from the string. In case of
411 412 * error, returns null.
412 413 * @exception NullPointerException if <code>pos</code> is null.
413 414 */
414 415 public Object parseObject(String source, ParsePosition pos) {
415 416 return parse(source, pos);
416 417 }
417 418
418 419 /**
419 420 * Constant for full style pattern.
420 421 */
421 422 public static final int FULL = 0;
422 423 /**
423 424 * Constant for long style pattern.
424 425 */
425 426 public static final int LONG = 1;
426 427 /**
427 428 * Constant for medium style pattern.
428 429 */
429 430 public static final int MEDIUM = 2;
430 431 /**
431 432 * Constant for short style pattern.
432 433 */
433 434 public static final int SHORT = 3;
434 435 /**
435 436 * Constant for default style pattern. Its value is MEDIUM.
436 437 */
437 438 public static final int DEFAULT = MEDIUM;
438 439
439 440 /**
440 441 * Gets the time formatter with the default formatting style
441 442 * for the default locale.
442 443 * @return a time formatter.
443 444 */
444 445 public final static DateFormat getTimeInstance()
445 446 {
446 447 return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
447 448 }
448 449
449 450 /**
450 451 * Gets the time formatter with the given formatting style
451 452 * for the default locale.
452 453 * @param style the given formatting style. For example,
453 454 * SHORT for "h:mm a" in the US locale.
454 455 * @return a time formatter.
455 456 */
456 457 public final static DateFormat getTimeInstance(int style)
457 458 {
458 459 return get(style, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
459 460 }
460 461
461 462 /**
462 463 * Gets the time formatter with the given formatting style
463 464 * for the given locale.
464 465 * @param style the given formatting style. For example,
465 466 * SHORT for "h:mm a" in the US locale.
466 467 * @param aLocale the given locale.
467 468 * @return a time formatter.
468 469 */
469 470 public final static DateFormat getTimeInstance(int style,
470 471 Locale aLocale)
471 472 {
472 473 return get(style, 0, 1, aLocale);
473 474 }
474 475
475 476 /**
476 477 * Gets the date formatter with the default formatting style
477 478 * for the default locale.
478 479 * @return a date formatter.
479 480 */
480 481 public final static DateFormat getDateInstance()
481 482 {
482 483 return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT));
483 484 }
484 485
485 486 /**
486 487 * Gets the date formatter with the given formatting style
487 488 * for the default locale.
488 489 * @param style the given formatting style. For example,
489 490 * SHORT for "M/d/yy" in the US locale.
490 491 * @return a date formatter.
491 492 */
492 493 public final static DateFormat getDateInstance(int style)
493 494 {
494 495 return get(0, style, 2, Locale.getDefault(Locale.Category.FORMAT));
495 496 }
496 497
497 498 /**
498 499 * Gets the date formatter with the given formatting style
499 500 * for the given locale.
500 501 * @param style the given formatting style. For example,
501 502 * SHORT for "M/d/yy" in the US locale.
502 503 * @param aLocale the given locale.
503 504 * @return a date formatter.
504 505 */
505 506 public final static DateFormat getDateInstance(int style,
506 507 Locale aLocale)
507 508 {
508 509 return get(0, style, 2, aLocale);
509 510 }
510 511
511 512 /**
512 513 * Gets the date/time formatter with the default formatting style
513 514 * for the default locale.
514 515 * @return a date/time formatter.
515 516 */
516 517 public final static DateFormat getDateTimeInstance()
517 518 {
518 519 return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT));
519 520 }
520 521
521 522 /**
522 523 * Gets the date/time formatter with the given date and time
523 524 * formatting styles for the default locale.
524 525 * @param dateStyle the given date formatting style. For example,
525 526 * SHORT for "M/d/yy" in the US locale.
526 527 * @param timeStyle the given time formatting style. For example,
527 528 * SHORT for "h:mm a" in the US locale.
528 529 * @return a date/time formatter.
529 530 */
530 531 public final static DateFormat getDateTimeInstance(int dateStyle,
531 532 int timeStyle)
532 533 {
533 534 return get(timeStyle, dateStyle, 3, Locale.getDefault(Locale.Category.FORMAT));
534 535 }
535 536
536 537 /**
537 538 * Gets the date/time formatter with the given formatting styles
538 539 * for the given locale.
539 540 * @param dateStyle the given date formatting style.
540 541 * @param timeStyle the given time formatting style.
541 542 * @param aLocale the given locale.
542 543 * @return a date/time formatter.
543 544 */
544 545 public final static DateFormat
545 546 getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
546 547 {
547 548 return get(timeStyle, dateStyle, 3, aLocale);
548 549 }
549 550
550 551 /**
551 552 * Get a default date/time formatter that uses the SHORT style for both the
552 553 * date and the time.
553 554 */
554 555 public final static DateFormat getInstance() {
555 556 return getDateTimeInstance(SHORT, SHORT);
556 557 }
557 558
558 559 /**
559 560 * Returns an array of all locales for which the
560 561 * <code>get*Instance</code> methods of this class can return
561 562 * localized instances.
562 563 * The returned array represents the union of locales supported by the Java
563 564 * runtime and by installed
564 565 * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
565 566 * It must contain at least a <code>Locale</code> instance equal to
566 567 * {@link java.util.Locale#US Locale.US}.
567 568 *
568 569 * @return An array of locales for which localized
569 570 * <code>DateFormat</code> instances are available.
570 571 */
571 572 public static Locale[] getAvailableLocales()
572 573 {
573 574 LocaleServiceProviderPool pool =
574 575 LocaleServiceProviderPool.getPool(DateFormatProvider.class);
575 576 return pool.getAvailableLocales();
576 577 }
577 578
578 579 /**
579 580 * Set the calendar to be used by this date format. Initially, the default
580 581 * calendar for the specified or default locale is used.
581 582 *
582 583 * <p>Any {@link java.util.TimeZone TimeZone} and {@linkplain
583 584 * #isLenient() leniency} values that have previously been set are
584 585 * overwritten by {@code newCalendar}'s values.
585 586 *
586 587 * @param newCalendar the new {@code Calendar} to be used by the date format
587 588 */
588 589 public void setCalendar(Calendar newCalendar)
589 590 {
590 591 this.calendar = newCalendar;
591 592 }
592 593
593 594 /**
594 595 * Gets the calendar associated with this date/time formatter.
595 596 *
596 597 * @return the calendar associated with this date/time formatter.
597 598 */
598 599 public Calendar getCalendar()
599 600 {
600 601 return calendar;
601 602 }
602 603
603 604 /**
604 605 * Allows you to set the number formatter.
605 606 * @param newNumberFormat the given new NumberFormat.
606 607 */
607 608 public void setNumberFormat(NumberFormat newNumberFormat)
608 609 {
609 610 this.numberFormat = newNumberFormat;
610 611 }
611 612
612 613 /**
613 614 * Gets the number formatter which this date/time formatter uses to
614 615 * format and parse a time.
615 616 * @return the number formatter which this date/time formatter uses.
616 617 */
617 618 public NumberFormat getNumberFormat()
618 619 {
619 620 return numberFormat;
620 621 }
621 622
622 623 /**
623 624 * Sets the time zone for the calendar of this {@code DateFormat} object.
624 625 * This method is equivalent to the following call.
625 626 * <blockquote><pre>
626 627 * getCalendar().setTimeZone(zone)
627 628 * </pre></blockquote>
628 629 *
629 630 * <p>The {@code TimeZone} set by this method is overwritten by a
630 631 * {@link #setCalendar(java.util.Calendar) setCalendar} call.
631 632 *
632 633 * <p>The {@code TimeZone} set by this method may be overwritten as
633 634 * a result of a call to the parse method.
634 635 *
635 636 * @param zone the given new time zone.
636 637 */
637 638 public void setTimeZone(TimeZone zone)
638 639 {
639 640 calendar.setTimeZone(zone);
640 641 }
641 642
642 643 /**
643 644 * Gets the time zone.
644 645 * This method is equivalent to the following call.
645 646 * <blockquote><pre>
646 647 * getCalendar().getTimeZone()
647 648 * </pre></blockquote>
648 649 *
649 650 * @return the time zone associated with the calendar of DateFormat.
650 651 */
651 652 public TimeZone getTimeZone()
652 653 {
653 654 return calendar.getTimeZone();
654 655 }
655 656
656 657 /**
657 658 * Specify whether or not date/time parsing is to be lenient. With
658 659 * lenient parsing, the parser may use heuristics to interpret inputs that
659 660 * do not precisely match this object's format. With strict parsing,
660 661 * inputs must match this object's format.
661 662 *
662 663 * <p>This method is equivalent to the following call.
663 664 * <blockquote><pre>
664 665 * getCalendar().setLenient(lenient)
665 666 * </pre></blockquote>
666 667 *
667 668 * <p>This leniency value is overwritten by a call to {@link
668 669 * #setCalendar(java.util.Calendar) setCalendar()}.
669 670 *
670 671 * @param lenient when {@code true}, parsing is lenient
671 672 * @see java.util.Calendar#setLenient(boolean)
672 673 */
673 674 public void setLenient(boolean lenient)
674 675 {
675 676 calendar.setLenient(lenient);
676 677 }
677 678
678 679 /**
679 680 * Tell whether date/time parsing is to be lenient.
680 681 * This method is equivalent to the following call.
681 682 * <blockquote><pre>
682 683 * getCalendar().isLenient()
683 684 * </pre></blockquote>
684 685 *
685 686 * @return {@code true} if the {@link #calendar} is lenient;
686 687 * {@code false} otherwise.
687 688 * @see java.util.Calendar#isLenient()
688 689 */
689 690 public boolean isLenient()
690 691 {
691 692 return calendar.isLenient();
692 693 }
693 694
694 695 /**
695 696 * Overrides hashCode
696 697 */
697 698 public int hashCode() {
698 699 return numberFormat.hashCode();
699 700 // just enough fields for a reasonable distribution
700 701 }
701 702
702 703 /**
703 704 * Overrides equals
704 705 */
705 706 public boolean equals(Object obj) {
706 707 if (this == obj) return true;
707 708 if (obj == null || getClass() != obj.getClass()) return false;
708 709 DateFormat other = (DateFormat) obj;
709 710 return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET!
710 711 calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() &&
711 712 calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() &&
712 713 calendar.isLenient() == other.calendar.isLenient() &&
713 714 calendar.getTimeZone().equals(other.calendar.getTimeZone()) &&
714 715 numberFormat.equals(other.numberFormat));
715 716 }
716 717
717 718 /**
718 719 * Overrides Cloneable
719 720 */
720 721 public Object clone()
721 722 {
722 723 DateFormat other = (DateFormat) super.clone();
723 724 other.calendar = (Calendar) calendar.clone();
724 725 other.numberFormat = (NumberFormat) numberFormat.clone();
725 726 return other;
726 727 }
727 728
728 729 /**
729 730 * Creates a DateFormat with the given time and/or date style in the given
730 731 * locale.
731 732 * @param timeStyle a value from 0 to 3 indicating the time format,
732 733 * ignored if flags is 2
733 734 * @param dateStyle a value from 0 to 3 indicating the time format,
734 735 * ignored if flags is 1
735 736 * @param flags either 1 for a time format, 2 for a date format,
736 737 * or 3 for a date/time format
737 738 * @param loc the locale for the format
738 739 */
739 740 private static DateFormat get(int timeStyle, int dateStyle,
740 741 int flags, Locale loc) {
741 742 if ((flags & 1) != 0) {
742 743 if (timeStyle < 0 || timeStyle > 3) {
743 744 throw new IllegalArgumentException("Illegal time style " + timeStyle);
744 745 }
↓ open down ↓ |
681 lines elided |
↑ open up ↑ |
745 746 } else {
746 747 timeStyle = -1;
747 748 }
748 749 if ((flags & 2) != 0) {
749 750 if (dateStyle < 0 || dateStyle > 3) {
750 751 throw new IllegalArgumentException("Illegal date style " + dateStyle);
751 752 }
752 753 } else {
753 754 dateStyle = -1;
754 755 }
755 - try {
756 - // Check whether a provider can provide an implementation that's closer
757 - // to the requested locale than what the Java runtime itself can provide.
758 - LocaleServiceProviderPool pool =
759 - LocaleServiceProviderPool.getPool(DateFormatProvider.class);
760 - if (pool.hasProviders()) {
761 - DateFormat providersInstance = pool.getLocalizedObject(
762 - DateFormatGetter.INSTANCE,
763 - loc,
764 - timeStyle,
765 - dateStyle,
766 - flags);
767 - if (providersInstance != null) {
768 - return providersInstance;
769 - }
770 - }
771 756
772 - return new SimpleDateFormat(timeStyle, dateStyle, loc);
773 - } catch (MissingResourceException e) {
774 - return new SimpleDateFormat("M/d/yy h:mm a");
757 + LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
758 + DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc);
759 + if (dateFormat == null) {
760 + dateFormat = get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc);
775 761 }
762 + return dateFormat;
776 763 }
777 764
765 + private static DateFormat get(LocaleProviderAdapter adapter, int timeStyle, int dateStyle, Locale loc) {
766 + DateFormatProvider provider = adapter.getDateFormatProvider();
767 + DateFormat dateFormat;
768 + if (timeStyle == -1) {
769 + dateFormat = provider.getDateInstance(dateStyle, loc);
770 + } else {
771 + if (dateStyle == -1) {
772 + dateFormat = provider.getTimeInstance(timeStyle, loc);
773 + } else {
774 + dateFormat = provider.getDateTimeInstance(dateStyle, timeStyle, loc);
775 + }
776 + }
777 + return dateFormat;
778 + }
779 +
778 780 /**
779 781 * Create a new date format.
780 782 */
781 783 protected DateFormat() {}
782 784
783 785 /**
784 786 * Defines constants that are used as attribute keys in the
785 787 * <code>AttributedCharacterIterator</code> returned
786 788 * from <code>DateFormat.formatToCharacterIterator</code> and as
787 789 * field identifiers in <code>FieldPosition</code>.
788 790 * <p>
789 791 * The class also provides two methods to map
790 792 * between its constants and the corresponding Calendar constants.
791 793 *
792 794 * @since 1.4
793 795 * @see java.util.Calendar
794 796 */
795 797 public static class Field extends Format.Field {
796 798
797 799 // Proclaim serial compatibility with 1.4 FCS
798 800 private static final long serialVersionUID = 7441350119349544720L;
799 801
800 802 // table of all instances in this class, used by readResolve
801 803 private static final Map<String, Field> instanceMap = new HashMap<>(18);
802 804 // Maps from Calendar constant (such as Calendar.ERA) to Field
803 805 // constant (such as Field.ERA).
804 806 private static final Field[] calendarToFieldMapping =
805 807 new Field[Calendar.FIELD_COUNT];
806 808
807 809 /** Calendar field. */
808 810 private int calendarField;
809 811
810 812 /**
811 813 * Returns the <code>Field</code> constant that corresponds to
812 814 * the <code>Calendar</code> constant <code>calendarField</code>.
813 815 * If there is no direct mapping between the <code>Calendar</code>
814 816 * constant and a <code>Field</code>, null is returned.
815 817 *
816 818 * @throws IllegalArgumentException if <code>calendarField</code> is
817 819 * not the value of a <code>Calendar</code> field constant.
818 820 * @param calendarField Calendar field constant
819 821 * @return Field instance representing calendarField.
820 822 * @see java.util.Calendar
821 823 */
822 824 public static Field ofCalendarField(int calendarField) {
823 825 if (calendarField < 0 || calendarField >=
824 826 calendarToFieldMapping.length) {
825 827 throw new IllegalArgumentException("Unknown Calendar constant "
826 828 + calendarField);
827 829 }
828 830 return calendarToFieldMapping[calendarField];
829 831 }
830 832
831 833 /**
832 834 * Creates a <code>Field</code>.
833 835 *
834 836 * @param name the name of the <code>Field</code>
835 837 * @param calendarField the <code>Calendar</code> constant this
836 838 * <code>Field</code> corresponds to; any value, even one
837 839 * outside the range of legal <code>Calendar</code> values may
838 840 * be used, but <code>-1</code> should be used for values
839 841 * that don't correspond to legal <code>Calendar</code> values
840 842 */
841 843 protected Field(String name, int calendarField) {
842 844 super(name);
843 845 this.calendarField = calendarField;
844 846 if (this.getClass() == DateFormat.Field.class) {
845 847 instanceMap.put(name, this);
846 848 if (calendarField >= 0) {
847 849 // assert(calendarField < Calendar.FIELD_COUNT);
848 850 calendarToFieldMapping[calendarField] = this;
849 851 }
850 852 }
851 853 }
852 854
853 855 /**
854 856 * Returns the <code>Calendar</code> field associated with this
855 857 * attribute. For example, if this represents the hours field of
856 858 * a <code>Calendar</code>, this would return
857 859 * <code>Calendar.HOUR</code>. If there is no corresponding
858 860 * <code>Calendar</code> constant, this will return -1.
859 861 *
860 862 * @return Calendar constant for this field
861 863 * @see java.util.Calendar
862 864 */
863 865 public int getCalendarField() {
↓ open down ↓ |
76 lines elided |
↑ open up ↑ |
864 866 return calendarField;
865 867 }
866 868
867 869 /**
868 870 * Resolves instances being deserialized to the predefined constants.
869 871 *
870 872 * @throws InvalidObjectException if the constant could not be
871 873 * resolved.
872 874 * @return resolved DateFormat.Field constant
873 875 */
876 + @Override
874 877 protected Object readResolve() throws InvalidObjectException {
875 878 if (this.getClass() != DateFormat.Field.class) {
876 879 throw new InvalidObjectException("subclass didn't correctly implement readResolve");
877 880 }
878 881
879 882 Object instance = instanceMap.get(getName());
880 883 if (instance != null) {
881 884 return instance;
882 885 } else {
883 886 throw new InvalidObjectException("unknown attribute name");
884 887 }
885 888 }
886 889
887 890 //
888 891 // The constants
889 892 //
890 893
891 894 /**
892 895 * Constant identifying the era field.
893 896 */
894 897 public final static Field ERA = new Field("era", Calendar.ERA);
895 898
896 899 /**
897 900 * Constant identifying the year field.
898 901 */
899 902 public final static Field YEAR = new Field("year", Calendar.YEAR);
900 903
901 904 /**
902 905 * Constant identifying the month field.
903 906 */
904 907 public final static Field MONTH = new Field("month", Calendar.MONTH);
905 908
906 909 /**
907 910 * Constant identifying the day of month field.
908 911 */
909 912 public final static Field DAY_OF_MONTH = new
910 913 Field("day of month", Calendar.DAY_OF_MONTH);
911 914
912 915 /**
913 916 * Constant identifying the hour of day field, where the legal values
914 917 * are 1 to 24.
915 918 */
916 919 public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1);
917 920
918 921 /**
919 922 * Constant identifying the hour of day field, where the legal values
920 923 * are 0 to 23.
921 924 */
922 925 public final static Field HOUR_OF_DAY0 = new
923 926 Field("hour of day", Calendar.HOUR_OF_DAY);
924 927
925 928 /**
926 929 * Constant identifying the minute field.
927 930 */
928 931 public final static Field MINUTE =new Field("minute", Calendar.MINUTE);
929 932
930 933 /**
931 934 * Constant identifying the second field.
932 935 */
933 936 public final static Field SECOND =new Field("second", Calendar.SECOND);
934 937
935 938 /**
936 939 * Constant identifying the millisecond field.
937 940 */
938 941 public final static Field MILLISECOND = new
939 942 Field("millisecond", Calendar.MILLISECOND);
940 943
941 944 /**
942 945 * Constant identifying the day of week field.
943 946 */
944 947 public final static Field DAY_OF_WEEK = new
945 948 Field("day of week", Calendar.DAY_OF_WEEK);
946 949
947 950 /**
948 951 * Constant identifying the day of year field.
949 952 */
950 953 public final static Field DAY_OF_YEAR = new
951 954 Field("day of year", Calendar.DAY_OF_YEAR);
952 955
953 956 /**
954 957 * Constant identifying the day of week field.
955 958 */
956 959 public final static Field DAY_OF_WEEK_IN_MONTH =
957 960 new Field("day of week in month",
958 961 Calendar.DAY_OF_WEEK_IN_MONTH);
959 962
960 963 /**
961 964 * Constant identifying the week of year field.
962 965 */
963 966 public final static Field WEEK_OF_YEAR = new
964 967 Field("week of year", Calendar.WEEK_OF_YEAR);
965 968
966 969 /**
967 970 * Constant identifying the week of month field.
968 971 */
969 972 public final static Field WEEK_OF_MONTH = new
970 973 Field("week of month", Calendar.WEEK_OF_MONTH);
971 974
972 975 /**
973 976 * Constant identifying the time of day indicator
974 977 * (e.g. "a.m." or "p.m.") field.
975 978 */
976 979 public final static Field AM_PM = new
977 980 Field("am pm", Calendar.AM_PM);
978 981
979 982 /**
980 983 * Constant identifying the hour field, where the legal values are
981 984 * 1 to 12.
982 985 */
983 986 public final static Field HOUR1 = new Field("hour 1", -1);
984 987
985 988 /**
986 989 * Constant identifying the hour field, where the legal values are
↓ open down ↓ |
103 lines elided |
↑ open up ↑ |
987 990 * 0 to 11.
988 991 */
989 992 public final static Field HOUR0 = new
990 993 Field("hour", Calendar.HOUR);
991 994
992 995 /**
993 996 * Constant identifying the time zone field.
994 997 */
995 998 public final static Field TIME_ZONE = new Field("time zone", -1);
996 999 }
997 -
998 - /**
999 - * Obtains a DateFormat instance from a DateFormatProvider
1000 - * implementation.
1001 - */
1002 - private static class DateFormatGetter
1003 - implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatProvider, DateFormat> {
1004 - private static final DateFormatGetter INSTANCE = new DateFormatGetter();
1005 -
1006 - public DateFormat getObject(DateFormatProvider dateFormatProvider,
1007 - Locale locale,
1008 - String key,
1009 - Object... params) {
1010 - assert params.length == 3;
1011 -
1012 - int timeStyle = (Integer)params[0];
1013 - int dateStyle = (Integer)params[1];
1014 - int flags = (Integer)params[2];
1015 -
1016 - switch (flags) {
1017 - case 1:
1018 - return dateFormatProvider.getTimeInstance(timeStyle, locale);
1019 - case 2:
1020 - return dateFormatProvider.getDateInstance(dateStyle, locale);
1021 - case 3:
1022 - return dateFormatProvider.getDateTimeInstance(dateStyle, timeStyle, locale);
1023 - default:
1024 - assert false : "should not happen";
1025 - }
1026 -
1027 - return null;
1028 - }
1029 - }
1030 1000 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX