2 * Copyright (C) 1996-2010, International Business Machines
\r
3 * Corporation and others. All Rights Reserved.
\r
6 package com.ibm.icu.text;
\r
8 import java.io.InvalidObjectException;
\r
9 import java.text.FieldPosition;
\r
10 import java.text.Format;
\r
11 import java.text.ParseException;
\r
12 import java.text.ParsePosition;
\r
13 import java.util.Date;
\r
14 import java.util.HashMap;
\r
15 import java.util.Locale;
\r
16 import java.util.Map;
\r
17 import java.util.MissingResourceException;
\r
19 import com.ibm.icu.impl.ICUResourceBundle;
\r
20 import com.ibm.icu.impl.RelativeDateFormat;
\r
21 import com.ibm.icu.util.Calendar;
\r
22 import com.ibm.icu.util.GregorianCalendar;
\r
23 import com.ibm.icu.util.TimeZone;
\r
24 import com.ibm.icu.util.ULocale;
\r
27 * {@icuenhanced java.text.DateFormat}.{@icu _usage_}
\r
29 * <p>DateFormat is an abstract class for date/time formatting subclasses which
\r
30 * formats and parses dates or time in a language-independent manner.
\r
31 * The date/time formatting subclass, such as SimpleDateFormat, allows for
\r
32 * formatting (i.e., date -> text), parsing (text -> date), and
\r
33 * normalization. The date is represented as a <code>Date</code> object or
\r
34 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
\r
36 * <p>DateFormat provides many class methods for obtaining default date/time
\r
37 * formatters based on the default or a given locale and a number of formatting
\r
38 * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
\r
39 * detail and examples of using these styles are provided in the method
\r
42 * <p>DateFormat helps you to format and parse dates for any locale.
\r
43 * Your code can be completely independent of the locale conventions for
\r
44 * months, days of the week, or even the calendar format: lunar vs. solar.
\r
46 * <p>To format a date for the current Locale, use one of the
\r
47 * static factory methods:
\r
49 * myString = DateFormat.getDateInstance().format(myDate);
\r
51 * <p>If you are formatting multiple numbers, it is
\r
52 * more efficient to get the format and use it multiple times so that
\r
53 * the system doesn't have to fetch the information about the local
\r
54 * language and country conventions multiple times.
\r
56 * DateFormat df = DateFormat.getDateInstance();
\r
57 * for (int i = 0; i < a.length; ++i) {
\r
58 * output.println(df.format(myDate[i]) + "; ");
\r
61 * <p>To format a number for a different Locale, specify it in the
\r
62 * call to getDateInstance().
\r
64 * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
\r
66 * <p>You can use a DateFormat to parse also.
\r
68 * myDate = df.parse(myString);
\r
70 * <p>Use getDateInstance to get the normal date format for that country.
\r
71 * There are other static factory methods available.
\r
72 * Use getTimeInstance to get the time format for that country.
\r
73 * Use getDateTimeInstance to get a date and time format. You can pass in
\r
74 * different options to these factory methods to control the length of the
\r
75 * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends
\r
76 * on the locale, but generally:
\r
77 * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm
\r
78 * <li>MEDIUM is longer, such as Jan 12, 1952
\r
79 * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm
\r
80 * <li>FULL is pretty completely specified, such as
\r
81 * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
\r
84 * <p>You can also set the time zone on the format if you wish.
\r
85 * If you want even more control over the format or parsing,
\r
86 * (or want to give your users more control),
\r
87 * you can try casting the DateFormat you get from the factory methods
\r
88 * to a SimpleDateFormat. This will work for the majority
\r
89 * of countries; just remember to put it in a try block in case you
\r
90 * encounter an unusual one.
\r
92 * <p>You can also use forms of the parse and format methods with
\r
93 * ParsePosition and FieldPosition to
\r
95 * <ul><li>progressively parse through pieces of a string.
\r
96 * <li>align any particular field, or find out where it is for selection
\r
100 * <h4>Synchronization</h4>
\r
102 * Date formats are not synchronized. It is recommended to create separate
\r
103 * format instances for each thread. If multiple threads access a format
\r
104 * concurrently, it must be synchronized externally.
\r
107 * @see NumberFormat
\r
108 * @see SimpleDateFormat
\r
109 * @see com.ibm.icu.util.Calendar
\r
110 * @see com.ibm.icu.util.GregorianCalendar
\r
111 * @see com.ibm.icu.util.TimeZone
\r
112 * @author Mark Davis, Chen-Lieh Huang, Alan Liu
\r
115 public abstract class DateFormat extends UFormat {
\r
118 * The calendar that <code>DateFormat</code> uses to produce the time field
\r
119 * values needed to implement date and time formatting. Subclasses should
\r
120 * initialize this to a calendar appropriate for the locale associated with
\r
121 * this <code>DateFormat</code>.
\r
125 protected Calendar calendar;
\r
128 * The number formatter that <code>DateFormat</code> uses to format numbers
\r
129 * in dates and times. Subclasses should initialize this to a number format
\r
130 * appropriate for the locale associated with this <code>DateFormat</code>.
\r
134 protected NumberFormat numberFormat;
\r
137 * FieldPosition selector for 'G' field alignment,
\r
138 * corresponding to the {@link Calendar#ERA} field.
\r
141 public final static int ERA_FIELD = 0;
\r
144 * FieldPosition selector for 'y' field alignment,
\r
145 * corresponding to the {@link Calendar#YEAR} field.
\r
148 public final static int YEAR_FIELD = 1;
\r
151 * FieldPosition selector for 'M' field alignment,
\r
152 * corresponding to the {@link Calendar#MONTH} field.
\r
155 public final static int MONTH_FIELD = 2;
\r
158 * FieldPosition selector for 'd' field alignment,
\r
159 * corresponding to the {@link Calendar#DATE} field.
\r
162 public final static int DATE_FIELD = 3;
\r
165 * FieldPosition selector for 'k' field alignment,
\r
166 * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
\r
167 * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
\r
168 * For example, 23:59 + 01:00 results in 24:59.
\r
171 public final static int HOUR_OF_DAY1_FIELD = 4;
\r
174 * FieldPosition selector for 'H' field alignment,
\r
175 * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
\r
176 * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
\r
177 * For example, 23:59 + 01:00 results in 00:59.
\r
180 public final static int HOUR_OF_DAY0_FIELD = 5;
\r
183 * FieldPosition selector for 'm' field alignment,
\r
184 * corresponding to the {@link Calendar#MINUTE} field.
\r
187 public final static int MINUTE_FIELD = 6;
\r
190 * FieldPosition selector for 's' field alignment,
\r
191 * corresponding to the {@link Calendar#SECOND} field.
\r
194 public final static int SECOND_FIELD = 7;
\r
197 * {@icu} FieldPosition selector for 'S' field alignment,
\r
198 * corresponding to the {@link Calendar#MILLISECOND} field.
\r
201 public final static int FRACTIONAL_SECOND_FIELD = 8;
\r
204 * Alias for FRACTIONAL_SECOND_FIELD.
\r
205 * @deprecated ICU 3.0 use FRACTIONAL_SECOND_FIELD.
\r
207 public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;
\r
210 * FieldPosition selector for 'E' field alignment,
\r
211 * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
\r
214 public final static int DAY_OF_WEEK_FIELD = 9;
\r
217 * FieldPosition selector for 'D' field alignment,
\r
218 * corresponding to the {@link Calendar#DAY_OF_YEAR} field.
\r
221 public final static int DAY_OF_YEAR_FIELD = 10;
\r
224 * FieldPosition selector for 'F' field alignment,
\r
225 * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.
\r
228 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
\r
231 * FieldPosition selector for 'w' field alignment,
\r
232 * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.
\r
235 public final static int WEEK_OF_YEAR_FIELD = 12;
\r
238 * FieldPosition selector for 'W' field alignment,
\r
239 * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.
\r
242 public final static int WEEK_OF_MONTH_FIELD = 13;
\r
245 * FieldPosition selector for 'a' field alignment,
\r
246 * corresponding to the {@link Calendar#AM_PM} field.
\r
249 public final static int AM_PM_FIELD = 14;
\r
252 * FieldPosition selector for 'h' field alignment,
\r
253 * corresponding to the {@link Calendar#HOUR} field.
\r
254 * HOUR1_FIELD is used for the one-based 12-hour clock.
\r
255 * For example, 11:30 PM + 1 hour results in 12:30 AM.
\r
258 public final static int HOUR1_FIELD = 15;
\r
261 * FieldPosition selector for 'K' field alignment,
\r
262 * corresponding to the {@link Calendar#HOUR} field.
\r
263 * HOUR0_FIELD is used for the zero-based 12-hour clock.
\r
264 * For example, 11:30 PM + 1 hour results in 00:30 AM.
\r
267 public final static int HOUR0_FIELD = 16;
\r
270 * FieldPosition selector for 'z' field alignment,
\r
271 * corresponding to the {@link Calendar#ZONE_OFFSET} and
\r
272 * {@link Calendar#DST_OFFSET} fields.
\r
275 public final static int TIMEZONE_FIELD = 17;
\r
278 * {@icu} FieldPosition selector for 'Y' field alignment,
\r
279 * corresponding to the {@link Calendar#YEAR_WOY} field.
\r
282 public final static int YEAR_WOY_FIELD = 18;
\r
285 * {@icu} FieldPosition selector for 'e' field alignment,
\r
286 * corresponding to the {@link Calendar#DOW_LOCAL} field.
\r
289 public final static int DOW_LOCAL_FIELD = 19;
\r
292 * {@icu} FieldPosition selector for 'u' field alignment,
\r
293 * corresponding to the {@link Calendar#EXTENDED_YEAR} field.
\r
296 public final static int EXTENDED_YEAR_FIELD = 20;
\r
299 * {@icu} FieldPosition selector for 'g' field alignment,
\r
300 * corresponding to the {@link Calendar#JULIAN_DAY} field.
\r
303 public final static int JULIAN_DAY_FIELD = 21;
\r
306 * {@icu} FieldPosition selector for 'A' field alignment,
\r
307 * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field.
\r
310 public final static int MILLISECONDS_IN_DAY_FIELD = 22;
\r
313 * {@icu} FieldPosition selector for 'Z' field alignment,
\r
314 * corresponding to the {@link Calendar#ZONE_OFFSET} and
\r
315 * {@link Calendar#DST_OFFSET} fields.
\r
318 public final static int TIMEZONE_RFC_FIELD = 23;
\r
321 * {@icu} FieldPosition selector for 'v' field alignment,
\r
322 * corresponding to the {@link Calendar#ZONE_OFFSET} and
\r
323 * {@link Calendar#DST_OFFSET} fields. This displays the generic zone
\r
324 * name, if available.
\r
327 public final static int TIMEZONE_GENERIC_FIELD = 24;
\r
330 * {@icu} FieldPosition selector for 'c' field alignment,
\r
331 * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
\r
332 * This displays the stand alone day name, if available.
\r
335 public final static int STANDALONE_DAY_FIELD = 25;
\r
338 * {@icu} FieldPosition selector for 'L' field alignment,
\r
339 * corresponding to the {@link Calendar#MONTH} field.
\r
340 * This displays the stand alone month name, if available.
\r
343 public final static int STANDALONE_MONTH_FIELD = 26;
\r
346 * {@icu} FieldPosition selector for 'Q' field alignment,
\r
347 * corresponding to the {@link Calendar#MONTH} field.
\r
348 * This displays the quarter.
\r
351 public final static int QUARTER_FIELD = 27;
\r
354 * {@icu} FieldPosition selector for 'q' field alignment,
\r
355 * corresponding to the {@link Calendar#MONTH} field.
\r
356 * This displays the stand alone quarter, if available.
\r
359 public final static int STANDALONE_QUARTER_FIELD = 28;
\r
362 * {@icu} FieldPosition selector for 'V' field alignment,
\r
363 * corresponding to the {@link Calendar#ZONE_OFFSET} and
\r
364 * {@link Calendar#DST_OFFSET} fields. This displays the fallback timezone
\r
365 * name when VVVV is specified, and the short standard or daylight
\r
366 * timezone name ignoring commonlyUsed when a single V is specified.
\r
369 public final static int TIMEZONE_SPECIAL_FIELD = 29;
\r
372 * {@icu} Number of FieldPosition selectors for DateFormat.
\r
373 * Valid selectors range from 0 to FIELD_COUNT-1.
\r
376 public final static int FIELD_COUNT = 30; // must == DateFormatSymbols.patternChars.length()
\r
378 // Proclaim serial compatibility with 1.1 FCS
\r
379 private static final long serialVersionUID = 7218322306649953788L;
\r
382 * Formats a time object into a time string. Examples of time objects
\r
383 * are a time value expressed in milliseconds and a Date object.
\r
384 * @param obj must be a Number or a Date or a Calendar.
\r
385 * @param toAppendTo the string buffer for the returning time string.
\r
386 * @return the formatted time string.
\r
387 * @param fieldPosition keeps track of the position of the field
\r
388 * within the returned string.
\r
389 * On input: an alignment field,
\r
390 * if desired. On output: the offsets of the alignment field. For
\r
391 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
\r
392 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
\r
393 * begin index and end index of fieldPosition will be set to
\r
394 * 0 and 4, respectively.
\r
395 * Notice that if the same time field appears
\r
396 * more than once in a pattern, the fieldPosition will be set for the first
\r
397 * occurrence of that time field. For instance, formatting a Date to
\r
398 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
\r
399 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
\r
400 * the begin index and end index of fieldPosition will be set to
\r
401 * 5 and 8, respectively, for the first occurrence of the timezone
\r
402 * pattern character 'z'.
\r
403 * @see java.text.Format
\r
406 public final StringBuffer format(Object obj, StringBuffer toAppendTo,
\r
407 FieldPosition fieldPosition)
\r
409 if (obj instanceof Calendar)
\r
410 return format( (Calendar)obj, toAppendTo, fieldPosition );
\r
411 else if (obj instanceof Date)
\r
412 return format( (Date)obj, toAppendTo, fieldPosition );
\r
413 else if (obj instanceof Number)
\r
414 return format( new Date(((Number)obj).longValue()),
\r
415 toAppendTo, fieldPosition );
\r
417 throw new IllegalArgumentException("Cannot format given Object (" +
\r
418 obj.getClass().getName() + ") as a Date");
\r
422 * Formats a date into a date/time string.
\r
423 * @param cal a Calendar set to the date and time to be formatted
\r
424 * into a date/time string. When the calendar type is different from
\r
425 * the internal calendar held by this DateFormat instance, the date
\r
426 * and the time zone will be inherited from the input calendar, but
\r
427 * other calendar field values will be calculated by the internal calendar.
\r
428 * @param toAppendTo the string buffer for the returning date/time string.
\r
429 * @param fieldPosition keeps track of the position of the field
\r
430 * within the returned string.
\r
431 * On input: an alignment field,
\r
432 * if desired. On output: the offsets of the alignment field. For
\r
433 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
\r
434 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
\r
435 * begin index and end index of fieldPosition will be set to
\r
436 * 0 and 4, respectively.
\r
437 * Notice that if the same time field appears
\r
438 * more than once in a pattern, the fieldPosition will be set for the first
\r
439 * occurrence of that time field. For instance, formatting a Date to
\r
440 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
\r
441 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
\r
442 * the begin index and end index of fieldPosition will be set to
\r
443 * 5 and 8, respectively, for the first occurrence of the timezone
\r
444 * pattern character 'z'.
\r
445 * @return the formatted date/time string.
\r
448 public abstract StringBuffer format(Calendar cal, StringBuffer toAppendTo,
\r
449 FieldPosition fieldPosition);
\r
452 * Formats a Date into a date/time string.
\r
453 * @param date a Date to be formatted into a date/time string.
\r
454 * @param toAppendTo the string buffer for the returning date/time string.
\r
455 * @param fieldPosition keeps track of the position of the field
\r
456 * within the returned string.
\r
457 * On input: an alignment field,
\r
458 * if desired. On output: the offsets of the alignment field. For
\r
459 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
\r
460 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
\r
461 * begin index and end index of fieldPosition will be set to
\r
462 * 0 and 4, respectively.
\r
463 * Notice that if the same time field appears
\r
464 * more than once in a pattern, the fieldPosition will be set for the first
\r
465 * occurrence of that time field. For instance, formatting a Date to
\r
466 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
\r
467 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
\r
468 * the begin index and end index of fieldPosition will be set to
\r
469 * 5 and 8, respectively, for the first occurrence of the timezone
\r
470 * pattern character 'z'.
\r
471 * @return the formatted date/time string.
\r
474 public StringBuffer format(Date date, StringBuffer toAppendTo,
\r
475 FieldPosition fieldPosition) {
\r
476 // Use our Calendar object
\r
477 calendar.setTime(date);
\r
478 return format(calendar, toAppendTo, fieldPosition);
\r
482 * Formats a Date into a date/time string.
\r
483 * @param date the time value to be formatted into a time string.
\r
484 * @return the formatted time string.
\r
487 public final String format(Date date)
\r
489 return format(date, new StringBuffer(64),new FieldPosition(0)).toString();
\r
493 * Parses a date/time string.
\r
495 * @param text The date/time string to be parsed
\r
497 * @return A Date, or null if the input could not be parsed
\r
499 * @exception ParseException If the given string cannot be parsed as a date.
\r
501 * @see #parse(String, ParsePosition)
\r
504 public Date parse(String text) throws ParseException
\r
506 ParsePosition pos = new ParsePosition(0);
\r
507 Date result = parse(text, pos);
\r
508 if (pos.getIndex() == 0) // ICU4J
\r
509 throw new ParseException("Unparseable date: \"" + text + "\"" ,
\r
510 pos.getErrorIndex()); // ICU4J
\r
515 * Parses a date/time string according to the given parse position.
\r
516 * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed
\r
517 * into a Calendar that is equivalent to Date(837039928046). The
\r
518 * caller should clear the calendar before calling this method,
\r
519 * unless existing field information is to be kept.
\r
521 * <p> By default, parsing is lenient: If the input is not in the form used
\r
522 * by this object's format method but can still be parsed as a date, then
\r
523 * the parse succeeds. Clients may insist on strict adherence to the
\r
524 * format by calling setLenient(false).
\r
526 * @see #setLenient(boolean)
\r
528 * @param text The date/time string to be parsed
\r
530 * @param cal The calendar into which parsed data will be stored.
\r
531 * In general, this should be cleared before calling this
\r
532 * method. If this parse fails, the calendar may still
\r
533 * have been modified. When the calendar type is different
\r
534 * from the internal calendar held by this DateFormat
\r
535 * instance, calendar field values will be parsed based
\r
536 * on the internal calendar initialized with the time and
\r
537 * the time zone taken from this calendar, then the
\r
538 * parse result (time in milliseconds and time zone) will
\r
539 * be set back to this calendar.
\r
541 * @param pos On input, the position at which to start parsing; on
\r
542 * output, the position at which parsing terminated, or the
\r
543 * start position if the parse failed.
\r
546 public abstract void parse(String text, Calendar cal, ParsePosition pos);
\r
549 * Parses a date/time string according to the given parse position. For
\r
550 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
\r
551 * that is equivalent to Date(837039928046).
\r
553 * <p> By default, parsing is lenient: If the input is not in the form used
\r
554 * by this object's format method but can still be parsed as a date, then
\r
555 * the parse succeeds. Clients may insist on strict adherence to the
\r
556 * format by calling setLenient(false).
\r
558 * @see #setLenient(boolean)
\r
560 * @param text The date/time string to be parsed
\r
562 * @param pos On input, the position at which to start parsing; on
\r
563 * output, the position at which parsing terminated, or the
\r
564 * start position if the parse failed.
\r
566 * @return A Date, or null if the input could not be parsed
\r
569 public Date parse(String text, ParsePosition pos) {
\r
570 Date result = null;
\r
571 int start = pos.getIndex();
\r
572 TimeZone tzsav = calendar.getTimeZone();
\r
574 parse(text, calendar, pos);
\r
575 if (pos.getIndex() != start) {
\r
577 result = calendar.getTime();
\r
578 } catch (IllegalArgumentException e) {
\r
579 // This occurs if the calendar is non-lenient and there is
\r
580 // an out-of-range field. We don't know which field was
\r
581 // illegal so we set the error index to the start.
\r
582 pos.setIndex(start);
\r
583 pos.setErrorIndex(start);
\r
586 // Restore TimeZone
\r
587 calendar.setTimeZone(tzsav);
\r
592 * Parses a date/time string into an Object. This convenience method simply
\r
593 * calls parse(String, ParsePosition).
\r
595 * @see #parse(String, ParsePosition)
\r
598 public Object parseObject (String source, ParsePosition pos)
\r
600 return parse(source, pos);
\r
604 * {@icu} Constant for empty style pattern.
\r
607 public static final int NONE = -1;
\r
610 * Constant for full style pattern.
\r
613 public static final int FULL = 0;
\r
616 * Constant for long style pattern.
\r
619 public static final int LONG = 1;
\r
622 * Constant for medium style pattern.
\r
625 public static final int MEDIUM = 2;
\r
628 * Constant for short style pattern.
\r
631 public static final int SHORT = 3;
\r
634 * Constant for default style pattern. Its value is MEDIUM.
\r
637 public static final int DEFAULT = MEDIUM;
\r
640 * {@icu} Constant for relative style mask.
\r
643 public static final int RELATIVE = (1 << 7);
\r
646 * {@icu} Constant for relative full style pattern.
\r
649 public static final int RELATIVE_FULL = RELATIVE | FULL;
\r
652 * {@icu} Constant for relative style pattern.
\r
655 public static final int RELATIVE_LONG = RELATIVE | LONG;
\r
658 * {@icu} Constant for relative style pattern.
\r
661 public static final int RELATIVE_MEDIUM = RELATIVE | MEDIUM;
\r
664 * {@icu} Constant for relative style pattern.
\r
667 public static final int RELATIVE_SHORT = RELATIVE | SHORT;
\r
670 * {@icu} Constant for relative default style pattern.
\r
673 public static final int RELATIVE_DEFAULT = RELATIVE | DEFAULT;
\r
675 /* Below are pre-defined skeletons
\r
681 * 1. only keeps the field pattern letter and ignores all other parts
\r
682 * in a pattern, such as space, punctuations, and string literals.
\r
684 * 2. hides the order of fields.
\r
686 * 3. might hide a field's pattern letter length.
\r
688 * For those non-digit calendar fields, the pattern letter length is
\r
689 * important, such as MMM, MMMM, and MMMMM; E and EEEE,
\r
690 * and the field's pattern letter length is honored.
\r
692 * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
\r
693 * the field pattern length is ignored and the best match, which is
\r
694 * defined in date time patterns, will be returned without honor
\r
695 * the field pattern letter length in skeleton.
\r
699 * {@icu} Constant for date pattern with minute and second.
\r
702 public static final String MINUTE_SECOND = "ms";
\r
705 * {@icu} Constant for date pattern with hour and minute in 24-hour presentation.
\r
708 public static final String HOUR24_MINUTE = "Hm";
\r
711 * {@icu} Constant for date pattern with hour, minute, and second in
\r
712 * 24-hour presentation.
\r
715 public static final String HOUR24_MINUTE_SECOND = "Hms";
\r
718 * {@icu} Constant for date pattern with hour, minute, and second.
\r
721 public static final String HOUR_MINUTE_SECOND = "hms";
\r
724 * {@icu} Constant for date pattern with standalone month.
\r
727 public static final String STANDALONE_MONTH = "LLLL";
\r
730 * {@icu} Constant for date pattern with standalone abbreviated month.
\r
733 public static final String ABBR_STANDALONE_MONTH = "LLL";
\r
736 * {@icu} Constant for date pattern with year and quarter.
\r
739 public static final String YEAR_QUARTER = "yQQQ";
\r
742 * {@icu} Constant for date pattern with year and abbreviated quarter.
\r
745 public static final String YEAR_ABBR_QUARTER = "yQ";
\r
748 /* Below are skeletons that date interval pre-defined in resource file.
\r
749 * Users are encouraged to use them in date interval format factory methods.
\r
752 * {@icu} Constant for date pattern with hour and minute.
\r
755 public static final String HOUR_MINUTE = "hm";
\r
758 * {@icu} Constant for date pattern with year.
\r
761 public static final String YEAR = "y";
\r
764 * {@icu} Constant for date pattern with day.
\r
767 public static final String DAY = "d";
\r
770 * {@icu} Constant for date pattern with numeric month, weekday, and day.
\r
773 public static final String NUM_MONTH_WEEKDAY_DAY = "MEd";
\r
776 * {@icu} Constant for date pattern with year and numeric month.
\r
779 public static final String YEAR_NUM_MONTH = "yM";
\r
782 * {@icu} Constant for date pattern with numeric month and day.
\r
785 public static final String NUM_MONTH_DAY = "Md";
\r
788 * {@icu} Constant for date pattern with year, numeric month, weekday, and day.
\r
791 public static final String YEAR_NUM_MONTH_WEEKDAY_DAY = "yMEd";
\r
794 * {@icu} Constant for date pattern with abbreviated month, weekday, and day.
\r
797 public static final String ABBR_MONTH_WEEKDAY_DAY = "MMMEd";
\r
800 * {@icu} Constant for date pattern with year and month.
\r
803 public static final String YEAR_MONTH = "yMMMM";
\r
806 * {@icu} Constant for date pattern with year and abbreviated month.
\r
809 public static final String YEAR_ABBR_MONTH = "yMMM";
\r
812 * {@icu} Constant for date pattern having month and day.
\r
815 public static final String MONTH_DAY = "MMMMd";
\r
818 * {@icu} Constant for date pattern with abbreviated month and day.
\r
821 public static final String ABBR_MONTH_DAY = "MMMd";
\r
824 * {@icu} Constant for date pattern with month, weekday, and day.
\r
827 public static final String MONTH_WEEKDAY_DAY = "MMMMEEEEd";
\r
830 * {@icu} Constant for date pattern with year, abbreviated month, weekday,
\r
834 public static final String YEAR_ABBR_MONTH_WEEKDAY_DAY = "yMMMEd";
\r
837 * {@icu} Constant for date pattern with year, month, weekday, and day.
\r
840 public static final String YEAR_MONTH_WEEKDAY_DAY = "yMMMMEEEEd";
\r
843 * {@icu} Constant for date pattern with year, month, and day.
\r
846 public static final String YEAR_MONTH_DAY = "yMMMMd";
\r
849 * {@icu} Constant for date pattern with year, abbreviated month, and day.
\r
852 public static final String YEAR_ABBR_MONTH_DAY = "yMMMd";
\r
855 * {@icu} Constant for date pattern with year, numeric month, and day.
\r
858 public static final String YEAR_NUM_MONTH_DAY = "yMd";
\r
861 * {@icu} Constant for date pattern with numeric month.
\r
864 public static final String NUM_MONTH = "M";
\r
867 * {@icu} Constant for date pattern with abbreviated month.
\r
870 public static final String ABBR_MONTH = "MMM";
\r
873 * {@icu} Constant for date pattern with month.
\r
876 public static final String MONTH = "MMMM";
\r
879 * {@icu} Constant for date pattern with hour, minute, and generic timezone.
\r
882 public static final String HOUR_MINUTE_GENERIC_TZ = "hmv";
\r
885 * {@icu} Constant for date pattern with hour, minute, and timezone.
\r
888 public static final String HOUR_MINUTE_TZ = "hmz";
\r
891 * {@icu} Constant for date pattern with hour.
\r
894 public static final String HOUR = "h";
\r
897 * {@icu} Constant for date pattern with hour and generic timezone.
\r
900 public static final String HOUR_GENERIC_TZ = "hv";
\r
903 * {@icu} Constant for date pattern with hour and timezone.
\r
906 public static final String HOUR_TZ = "hz";
\r
909 * Gets the time formatter with the default formatting style
\r
910 * for the default locale.
\r
911 * @return a time formatter.
\r
914 public final static DateFormat getTimeInstance()
\r
916 return get(-1, DEFAULT, ULocale.getDefault());
\r
920 * Returns the time formatter with the given formatting style
\r
921 * for the default locale.
\r
922 * @param style the given formatting style. For example,
\r
923 * SHORT for "h:mm a" in the US locale.
\r
924 * @return a time formatter.
\r
927 public final static DateFormat getTimeInstance(int style)
\r
929 return get(-1, style, ULocale.getDefault());
\r
933 * Returns the time formatter with the given formatting style
\r
934 * for the given locale.
\r
935 * @param style the given formatting style. For example,
\r
936 * SHORT for "h:mm a" in the US locale.
\r
937 * @param aLocale the given locale.
\r
938 * @return a time formatter.
\r
941 public final static DateFormat getTimeInstance(int style,
\r
944 return get(-1, style, ULocale.forLocale(aLocale));
\r
948 * Returns the time formatter with the given formatting style
\r
949 * for the given locale.
\r
950 * @param style the given formatting style. For example,
\r
951 * SHORT for "h:mm a" in the US locale.
\r
952 * @param locale the given ulocale.
\r
953 * @return a time formatter.
\r
956 public final static DateFormat getTimeInstance(int style,
\r
959 return get(-1, style, locale);
\r
963 * Returns the date formatter with the default formatting style
\r
964 * for the default locale.
\r
965 * @return a date formatter.
\r
968 public final static DateFormat getDateInstance()
\r
970 return get(DEFAULT, -1, ULocale.getDefault());
\r
974 * Returns the date formatter with the given formatting style
\r
975 * for the default locale.
\r
976 * @param style the given formatting style. For example,
\r
977 * SHORT for "M/d/yy" in the US locale.
\r
978 * @return a date formatter.
\r
981 public final static DateFormat getDateInstance(int style)
\r
983 return get(style, -1, ULocale.getDefault());
\r
987 * Returns the date formatter with the given formatting style
\r
988 * for the given locale.
\r
989 * @param style the given formatting style. For example,
\r
990 * SHORT for "M/d/yy" in the US locale.
\r
991 * @param aLocale the given locale.
\r
992 * @return a date formatter.
\r
995 public final static DateFormat getDateInstance(int style,
\r
998 return get(style, -1, ULocale.forLocale(aLocale));
\r
1002 * Returns the date formatter with the given formatting style
\r
1003 * for the given locale.
\r
1004 * @param style the given formatting style. For example,
\r
1005 * SHORT for "M/d/yy" in the US locale.
\r
1006 * @param locale the given ulocale.
\r
1007 * @return a date formatter.
\r
1010 public final static DateFormat getDateInstance(int style,
\r
1013 return get(style, -1, locale);
\r
1017 * Returns the date/time formatter with the default formatting style
\r
1018 * for the default locale.
\r
1019 * @return a date/time formatter.
\r
1022 public final static DateFormat getDateTimeInstance()
\r
1024 return get(DEFAULT, DEFAULT, ULocale.getDefault());
\r
1028 * Returns the date/time formatter with the given date and time
\r
1029 * formatting styles for the default locale.
\r
1030 * @param dateStyle the given date formatting style. For example,
\r
1031 * SHORT for "M/d/yy" in the US locale.
\r
1032 * @param timeStyle the given time formatting style. For example,
\r
1033 * SHORT for "h:mm a" in the US locale.
\r
1034 * @return a date/time formatter.
\r
1037 public final static DateFormat getDateTimeInstance(int dateStyle,
\r
1040 return get(dateStyle, timeStyle, ULocale.getDefault());
\r
1044 * Returns the date/time formatter with the given formatting styles
\r
1045 * for the given locale.
\r
1046 * @param dateStyle the given date formatting style.
\r
1047 * @param timeStyle the given time formatting style.
\r
1048 * @param aLocale the given locale.
\r
1049 * @return a date/time formatter.
\r
1052 public final static DateFormat getDateTimeInstance(
\r
1053 int dateStyle, int timeStyle, Locale aLocale)
\r
1055 return get(dateStyle, timeStyle, ULocale.forLocale(aLocale));
\r
1059 * Returns the date/time formatter with the given formatting styles
\r
1060 * for the given locale.
\r
1061 * @param dateStyle the given date formatting style.
\r
1062 * @param timeStyle the given time formatting style.
\r
1063 * @param locale the given ulocale.
\r
1064 * @return a date/time formatter.
\r
1067 public final static DateFormat getDateTimeInstance(
\r
1068 int dateStyle, int timeStyle, ULocale locale)
\r
1070 return get(dateStyle, timeStyle, locale);
\r
1074 * Returns a default date/time formatter that uses the SHORT style for both the
\r
1075 * date and the time.
\r
1078 public final static DateFormat getInstance() {
\r
1079 return getDateTimeInstance(SHORT, SHORT);
\r
1083 * Returns the set of locales for which DateFormats are installed.
\r
1084 * @return the set of locales for which DateFormats are installed.
\r
1087 public static Locale[] getAvailableLocales()
\r
1089 return ICUResourceBundle.getAvailableLocales();
\r
1093 * {@icu} Returns the set of locales for which DateFormats are installed.
\r
1094 * @return the set of locales for which DateFormats are installed.
\r
1095 * @draft ICU 3.2 (retain)
\r
1096 * @provisional This API might change or be removed in a future release.
\r
1098 public static ULocale[] getAvailableULocales()
\r
1100 return ICUResourceBundle.getAvailableULocales();
\r
1104 * Sets the calendar to be used by this date format. Initially, the default
\r
1105 * calendar for the specified or default locale is used.
\r
1106 * @param newCalendar the new Calendar to be used by the date format
\r
1109 public void setCalendar(Calendar newCalendar)
\r
1111 this.calendar = newCalendar;
\r
1115 * Returns the calendar associated with this date/time formatter.
\r
1116 * @return the calendar associated with this date/time formatter.
\r
1119 public Calendar getCalendar()
\r
1125 * Sets the number formatter.
\r
1126 * @param newNumberFormat the given new NumberFormat.
\r
1129 public void setNumberFormat(NumberFormat newNumberFormat)
\r
1131 this.numberFormat = newNumberFormat;
\r
1132 /*In order to parse String like "11.10.2001" to DateTime correctly
\r
1133 in Locale("fr","CH") [Richard/GCL]
\r
1135 this.numberFormat.setParseIntegerOnly(true);
\r
1139 * Returns the number formatter which this date/time formatter uses to
\r
1140 * format and parse a time.
\r
1141 * @return the number formatter which this date/time formatter uses.
\r
1144 public NumberFormat getNumberFormat()
\r
1146 return numberFormat;
\r
1150 * Sets the time zone for the calendar of this DateFormat object.
\r
1151 * @param zone the given new time zone.
\r
1154 public void setTimeZone(TimeZone zone)
\r
1156 calendar.setTimeZone(zone);
\r
1160 * Returns the time zone.
\r
1161 * @return the time zone associated with the calendar of DateFormat.
\r
1164 public TimeZone getTimeZone()
\r
1166 return calendar.getTimeZone();
\r
1170 * Specifies whether date/time parsing is to be lenient. With
\r
1171 * lenient parsing, the parser may use heuristics to interpret inputs that
\r
1172 * do not precisely match this object's format. With strict parsing,
\r
1173 * inputs must match this object's format.
\r
1174 * @param lenient when true, parsing is lenient
\r
1175 * @see com.ibm.icu.util.Calendar#setLenient
\r
1178 public void setLenient(boolean lenient)
\r
1180 calendar.setLenient(lenient);
\r
1184 * Returns whether date/time parsing is lenient.
\r
1187 public boolean isLenient()
\r
1189 return calendar.isLenient();
\r
1193 * Overrides hashCode.
\r
1197 // turn off code coverage since all subclasses override this
\r
1198 public int hashCode() {
\r
1199 return numberFormat.hashCode();
\r
1200 // just enough fields for a reasonable distribution
\r
1205 * Overrides equals.
\r
1208 public boolean equals(Object obj) {
\r
1209 if (this == obj) return true;
\r
1210 if (obj == null || getClass() != obj.getClass()) return false;
\r
1211 DateFormat other = (DateFormat) obj;
\r
1212 return (calendar.isEquivalentTo(other.calendar) &&
\r
1213 numberFormat.equals(other.numberFormat));
\r
1217 * Overrides clone.
\r
1220 public Object clone()
\r
1222 DateFormat other = (DateFormat) super.clone();
\r
1223 other.calendar = (Calendar) calendar.clone();
\r
1224 other.numberFormat = (NumberFormat) numberFormat.clone();
\r
1229 * Creates a DateFormat with the given time and/or date style in the given
\r
1231 * @param dateStyle a value from 0 to 3 indicating the time format,
\r
1232 * or -1 to indicate no date
\r
1233 * @param timeStyle a value from 0 to 3 indicating the time format,
\r
1234 * or -1 to indicate no time
\r
1235 * @param loc the locale for the format
\r
1237 private static DateFormat get(int dateStyle, int timeStyle, ULocale loc) {
\r
1238 if((timeStyle != -1 && (timeStyle & RELATIVE)>0) ||
\r
1239 (dateStyle != -1 && (dateStyle & RELATIVE)>0)) {
\r
1240 RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc);
\r
1244 if (timeStyle < -1 || timeStyle > 3) {
\r
1245 throw new IllegalArgumentException("Illegal time style " + timeStyle);
\r
1247 if (dateStyle < -1 || dateStyle > 3) {
\r
1248 throw new IllegalArgumentException("Illegal date style " + dateStyle);
\r
1251 Calendar cal = Calendar.getInstance(loc);
\r
1252 DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc);
\r
1253 result.setLocale(cal.getLocale(ULocale.VALID_LOCALE),
\r
1254 cal.getLocale(ULocale.ACTUAL_LOCALE));
\r
1256 } catch (MissingResourceException e) {
\r
1258 // coverage requires separate run with no data, so skip
\r
1259 return new SimpleDateFormat("M/d/yy h:mm a");
\r
1265 * Creates a new date format.
\r
1268 protected DateFormat() {}
\r
1270 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\r
1272 //-------------------------------------------------------------------------
\r
1273 // Public static interface for creating custon DateFormats for different
\r
1274 // types of Calendars.
\r
1275 //-------------------------------------------------------------------------
\r
1278 * Creates a {@link DateFormat} object that can be used to format dates in
\r
1279 * the calendar system specified by <code>cal</code>.
\r
1281 * @param cal The calendar system for which a date format is desired.
\r
1283 * @param dateStyle The type of date format desired. This can be
\r
1284 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1287 * @param locale The locale for which the date format is desired.
\r
1290 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale)
\r
1292 return getDateTimeInstance(cal, dateStyle, -1, ULocale.forLocale(locale));
\r
1296 * Creates a {@link DateFormat} object that can be used to format dates in
\r
1297 * the calendar system specified by <code>cal</code>.
\r
1299 * @param cal The calendar system for which a date format is desired.
\r
1301 * @param dateStyle The type of date format desired. This can be
\r
1302 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1305 * @param locale The locale for which the date format is desired.
\r
1308 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale)
\r
1310 return getDateTimeInstance(cal, dateStyle, -1, locale);
\r
1314 * Creates a {@link DateFormat} object that can be used to format times in
\r
1315 * the calendar system specified by <code>cal</code>.
\r
1317 * <b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1318 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1320 * @param cal The calendar system for which a time format is desired.
\r
1322 * @param timeStyle The type of time format desired. This can be
\r
1323 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1326 * @param locale The locale for which the time format is desired.
\r
1328 * @see DateFormat#getTimeInstance
\r
1331 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale)
\r
1333 return getDateTimeInstance(cal, -1, timeStyle, ULocale.forLocale(locale));
\r
1337 * Creates a {@link DateFormat} object that can be used to format times in
\r
1338 * the calendar system specified by <code>cal</code>.
\r
1340 * <b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1341 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1343 * @param cal The calendar system for which a time format is desired.
\r
1345 * @param timeStyle The type of time format desired. This can be
\r
1346 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1349 * @param locale The locale for which the time format is desired.
\r
1351 * @see DateFormat#getTimeInstance
\r
1354 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale)
\r
1356 return getDateTimeInstance(cal, -1, timeStyle, locale);
\r
1360 * Creates a {@link DateFormat} object that can be used to format dates and times in
\r
1361 * the calendar system specified by <code>cal</code>.
\r
1363 * <b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1364 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1366 * @param cal The calendar system for which a date/time format is desired.
\r
1368 * @param dateStyle The type of date format desired. This can be
\r
1369 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1372 * @param timeStyle The type of time format desired. This can be
\r
1373 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1376 * @param locale The locale for which the date/time format is desired.
\r
1378 * @see DateFormat#getDateTimeInstance
\r
1381 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
\r
1382 int timeStyle, Locale locale)
\r
1384 return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale.forLocale(locale));
\r
1388 * Creates a {@link DateFormat} object that can be used to format dates and times in
\r
1389 * the calendar system specified by <code>cal</code>.
\r
1391 * <b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1392 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1394 * @param cal The calendar system for which a date/time format is desired.
\r
1396 * @param dateStyle The type of date format desired. This can be
\r
1397 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1400 * @param timeStyle The type of time format desired. This can be
\r
1401 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
\r
1404 * @param locale The locale for which the date/time format is desired.
\r
1406 * @see DateFormat#getDateTimeInstance
\r
1409 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
\r
1410 int timeStyle, ULocale locale)
\r
1412 return cal.getDateTimeFormat(dateStyle, timeStyle, locale);
\r
1416 * Convenience overload.
\r
1419 static final public DateFormat getInstance(Calendar cal, Locale locale) {
\r
1420 return getDateTimeInstance(cal, SHORT, SHORT, ULocale.forLocale(locale));
\r
1424 * Convenience overload.
\r
1426 * @provisional This API might change or be removed in a future release.
\r
1428 static final public DateFormat getInstance(Calendar cal, ULocale locale) {
\r
1429 return getDateTimeInstance(cal, SHORT, SHORT, locale);
\r
1433 * Convenience overload.
\r
1436 static final public DateFormat getInstance(Calendar cal) {
\r
1437 return getInstance(cal, ULocale.getDefault());
\r
1441 * Convenience overload.
\r
1444 static final public DateFormat getDateInstance(Calendar cal, int dateStyle) {
\r
1445 return getDateInstance(cal, dateStyle, ULocale.getDefault());
\r
1449 * Convenience overload.
\r
1452 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) {
\r
1453 return getTimeInstance(cal, timeStyle, ULocale.getDefault());
\r
1457 * Convenience overload.
\r
1460 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) {
\r
1461 return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault());
\r
1465 * {@icu} Convenience overload.
\r
1468 public final static DateFormat getPatternInstance(String pattern) {
\r
1469 return getPatternInstance(pattern, ULocale.getDefault());
\r
1473 * {@icu} Convenience overload.
\r
1476 public final static DateFormat getPatternInstance(String pattern, Locale locale) {
\r
1477 return getPatternInstance(pattern, ULocale.forLocale(locale));
\r
1481 * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in
\r
1482 * the given locale.
\r
1484 * <b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1485 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1488 * @param pattern The pattern that selects the fields to be formatted. (Uses the
\r
1489 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH},
\r
1490 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc.
\r
1492 * @param locale The locale for which the date/time format is desired.
\r
1496 public final static DateFormat getPatternInstance(String pattern, ULocale locale) {
\r
1497 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
\r
1498 final String bestPattern = generator.getBestPattern(pattern);
\r
1499 return new SimpleDateFormat(bestPattern, locale);
\r
1503 * {@icu} Convenience overload.
\r
1506 public final static DateFormat getPatternInstance(Calendar cal, String pattern, Locale locale) {
\r
1507 return getPatternInstance(cal, pattern, ULocale.forLocale(locale));
\r
1511 * {@icu} Creates a {@link DateFormat} object that can be used to format dates and
\r
1512 * times in the calendar system specified by <code>cal</code>.
\r
1514 * <p><b>Note:</b> When this functionality is moved into the core JDK, this method
\r
1515 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
\r
1517 * @param cal The calendar system for which a date/time format is desired.
\r
1519 * @param pattern The pattern that selects the fields to be formatted. (Uses the
\r
1520 * {@link DateTimePatternGenerator}.) This can be
\r
1521 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY},
\r
1524 * @param locale The locale for which the date/time format is desired.
\r
1528 public final static DateFormat getPatternInstance(
\r
1529 Calendar cal, String pattern, ULocale locale) {
\r
1530 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
\r
1531 final String bestPattern = generator.getBestPattern(pattern);
\r
1532 SimpleDateFormat format = new SimpleDateFormat(bestPattern, locale);
\r
1533 format.setCalendar(cal);
\r
1538 * The instances of this inner class are used as attribute keys and values
\r
1539 * in AttributedCharacterIterator that
\r
1540 * DateFormat.formatToCharacterIterator() method returns.
\r
1542 * <p>There is no public constructor to this class, the only instances are the
\r
1543 * constants defined here.
\r
1547 public static class Field extends Format.Field {
\r
1549 private static final long serialVersionUID = -3627456821000730829L;
\r
1551 // Max number of calendar fields
\r
1552 private static final int CAL_FIELD_COUNT;
\r
1554 // Table for mapping calendar field number to DateFormat.Field
\r
1555 private static final Field[] CAL_FIELDS;
\r
1557 // Map for resolving DateFormat.Field by name
\r
1558 private static final Map<String, Field> FIELD_NAME_MAP;
\r
1561 GregorianCalendar cal = new GregorianCalendar();
\r
1562 CAL_FIELD_COUNT = cal.getFieldCount();
\r
1563 CAL_FIELDS = new Field[CAL_FIELD_COUNT];
\r
1564 FIELD_NAME_MAP = new HashMap<String, Field>(CAL_FIELD_COUNT);
\r
1567 // Java fields -------------------
\r
1570 * Constant identifying the time of day indicator(am/pm).
\r
1573 public static final Field AM_PM = new Field("am pm", Calendar.AM_PM);
\r
1576 * Constant identifying the day of month field.
\r
1579 public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH);
\r
1582 * Constant identifying the day of week field.
\r
1585 public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK);
\r
1588 * Constant identifying the day of week in month field.
\r
1591 public static final Field DAY_OF_WEEK_IN_MONTH =
\r
1592 new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
\r
1595 * Constant identifying the day of year field.
\r
1598 public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR);
\r
1601 * Constant identifying the era field.
\r
1604 public static final Field ERA = new Field("era", Calendar.ERA);
\r
1607 * Constant identifying the hour(0-23) of day field.
\r
1610 public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY);
\r
1613 * Constant identifying the hour(1-24) of day field.
\r
1616 public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1);
\r
1619 * Constant identifying the hour(0-11) field.
\r
1622 public static final Field HOUR0 = new Field("hour", Calendar.HOUR);
\r
1625 * Constant identifying the hour(1-12) field.
\r
1628 public static final Field HOUR1 = new Field("hour 1", -1);
\r
1631 * Constant identifying the millisecond field.
\r
1634 public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND);
\r
1637 * Constant identifying the minute field.
\r
1640 public static final Field MINUTE = new Field("minute", Calendar.MINUTE);
\r
1643 * Constant identifying the month field.
\r
1646 public static final Field MONTH = new Field("month", Calendar.MONTH);
\r
1649 * Constant identifying the second field.
\r
1652 public static final Field SECOND = new Field("second", Calendar.SECOND);
\r
1655 * Constant identifying the time zone field.
\r
1658 public static final Field TIME_ZONE = new Field("time zone", -1);
\r
1661 * Constant identifying the week of month field.
\r
1664 public static final Field WEEK_OF_MONTH =
\r
1665 new Field("week of month", Calendar.WEEK_OF_MONTH);
\r
1668 * Constant identifying the week of year field.
\r
1671 public static final Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR);
\r
1674 * Constant identifying the year field.
\r
1677 public static final Field YEAR = new Field("year", Calendar.YEAR);
\r
1680 // ICU only fields -------------------
\r
1683 * Constant identifying the local day of week field.
\r
1686 public static final Field DOW_LOCAL = new Field("local day of week", Calendar.DOW_LOCAL);
\r
1689 * Constant identifying the extended year field.
\r
1692 public static final Field EXTENDED_YEAR = new Field("extended year",
\r
1693 Calendar.EXTENDED_YEAR);
\r
1696 * Constant identifying the Julian day field.
\r
1699 public static final Field JULIAN_DAY = new Field("Julian day", Calendar.JULIAN_DAY);
\r
1702 * Constant identifying the milliseconds in day field.
\r
1705 public static final Field MILLISECONDS_IN_DAY =
\r
1706 new Field("milliseconds in day", Calendar.MILLISECONDS_IN_DAY);
\r
1709 * Constant identifying the year used with week of year field.
\r
1712 public static final Field YEAR_WOY = new Field("year for week of year", Calendar.YEAR_WOY);
\r
1715 * Constant identifying the quarter field.
\r
1718 public static final Field QUARTER = new Field("quarter", -1);
\r
1720 // Stand alone types are variants for its base types. So we do not define Field for
\r
1723 public static final Field STANDALONE_DAY =
\r
1724 new Field("stand alone day of week", Calendar.DAY_OF_WEEK);
\r
1725 public static final Field STANDALONE_MONTH = new Field("stand alone month", Calendar.MONTH);
\r
1726 public static final Field STANDALONE_QUARTER = new Field("stand alone quarter", -1);
\r
1729 // Corresponding calendar field
\r
1730 private final int calendarField;
\r
1733 * Constructs a <code>DateFormat.Field</code> with the given name and
\r
1734 * the <code>Calendar</code> field which this attribute represents. Use -1 for
\r
1735 * <code>calendarField</code> if this field does not have a corresponding
\r
1736 * <code>Calendar</code> field.
\r
1738 * @param name Name of the attribute
\r
1739 * @param calendarField <code>Calendar</code> field constant
\r
1743 protected Field(String name, int calendarField) {
\r
1745 this.calendarField = calendarField;
\r
1746 if (this.getClass() == DateFormat.Field.class) {
\r
1747 FIELD_NAME_MAP.put(name, this);
\r
1748 if (calendarField >= 0 && calendarField < CAL_FIELD_COUNT) {
\r
1749 CAL_FIELDS[calendarField] = this;
\r
1755 * Returns the <code>Field</code> constant that corresponds to the <code>
\r
1756 * Calendar</code> field <code>calendarField</code>. If there is no
\r
1757 * corresponding <code>Field</code> is available, null is returned.
\r
1759 * @param calendarField <code>Calendar</code> field constant
\r
1760 * @return <code>Field</code> associated with the <code>calendarField</code>,
\r
1761 * or null if no associated <code>Field</code> is available.
\r
1762 * @throws IllegalArgumentException if <code>calendarField</code> is not
\r
1763 * a valid <code>Calendar</code> field constant.
\r
1767 public static DateFormat.Field ofCalendarField(int calendarField) {
\r
1768 if (calendarField < 0 || calendarField >= CAL_FIELD_COUNT) {
\r
1769 throw new IllegalArgumentException("Calendar field number is out of range");
\r
1771 return CAL_FIELDS[calendarField];
\r
1775 * Returns the <code>Calendar</code> field associated with this attribute.
\r
1776 * If there is no corresponding <code>Calendar</code> available, this will
\r
1779 * @return <code>Calendar</code> constant for this attribute.
\r
1783 public int getCalendarField() {
\r
1784 return calendarField;
\r
1788 * Resolves instances being deserialized to the predefined constants.
\r
1790 * @throws InvalidObjectException if the constant could not be resolved.
\r
1794 protected Object readResolve() throws InvalidObjectException {
\r
1796 if (this.getClass() != DateFormat.Field.class) {
\r
1797 throw new InvalidObjectException(
\r
1798 "A subclass of DateFormat.Field must implement readResolve.");
\r
1801 Object o = FIELD_NAME_MAP.get(this.getName());
\r
1804 throw new InvalidObjectException("Unknown attribute name.");
\r