3 * Copyright (C) 1996-2009, International Business Machines
4 * Corporation and others. All Rights Reserved.
7 package com.ibm.icu.text;
9 import java.text.FieldPosition;
10 import java.text.ParseException;
11 import java.text.ParsePosition;
12 import java.util.Date;
13 import java.util.Locale;
14 import java.util.MissingResourceException;
16 //#if defined(FOUNDATION10) || defined(J2SE13)
18 import java.io.InvalidObjectException;
19 import java.text.Format;
20 import java.util.HashMap;
22 import com.ibm.icu.util.GregorianCalendar;
25 import com.ibm.icu.impl.ICUResourceBundle;
26 import com.ibm.icu.impl.RelativeDateFormat;
27 import com.ibm.icu.util.Calendar;
28 import com.ibm.icu.util.TimeZone;
29 import com.ibm.icu.util.ULocale;
32 * DateFormat is an abstract class for date/time formatting subclasses which
33 * formats and parses dates or time in a language-independent manner.
34 * The date/time formatting subclass, such as SimpleDateFormat, allows for
35 * formatting (i.e., date -> text), parsing (text -> date), and
36 * normalization. The date is represented as a <code>Date</code> object or
37 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
39 * <p>DateFormat provides many class methods for obtaining default date/time
40 * formatters based on the default or a given locale and a number of formatting
41 * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
42 * detail and examples of using these styles are provided in the method
45 * <p>DateFormat helps you to format and parse dates for any locale.
46 * Your code can be completely independent of the locale conventions for
47 * months, days of the week, or even the calendar format: lunar vs. solar.
49 * <p>To format a date for the current Locale, use one of the
50 * static factory methods:
52 * myString = DateFormat.getDateInstance().format(myDate);
54 * <p>If you are formatting multiple numbers, it is
55 * more efficient to get the format and use it multiple times so that
56 * the system doesn't have to fetch the information about the local
57 * language and country conventions multiple times.
59 * DateFormat df = DateFormat.getDateInstance();
60 * for (int i = 0; i < a.length; ++i) {
61 * output.println(df.format(myDate[i]) + "; ");
64 * <p>To format a number for a different Locale, specify it in the
65 * call to getDateInstance().
67 * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
69 * <p>You can use a DateFormat to parse also.
71 * myDate = df.parse(myString);
73 * <p>Use getDateInstance to get the normal date format for that country.
74 * There are other static factory methods available.
75 * Use getTimeInstance to get the time format for that country.
76 * Use getDateTimeInstance to get a date and time format. You can pass in
77 * different options to these factory methods to control the length of the
78 * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends
79 * on the locale, but generally:
80 * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm
81 * <li>MEDIUM is longer, such as Jan 12, 1952
82 * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm
83 * <li>FULL is pretty completely specified, such as
84 * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
87 * <p>You can also set the time zone on the format if you wish.
88 * If you want even more control over the format or parsing,
89 * (or want to give your users more control),
90 * you can try casting the DateFormat you get from the factory methods
91 * to a SimpleDateFormat. This will work for the majority
92 * of countries; just remember to put it in a try block in case you
93 * encounter an unusual one.
95 * <p>You can also use forms of the parse and format methods with
96 * ParsePosition and FieldPosition to
98 * <ul><li>progressively parse through pieces of a string.
99 * <li>align any particular field, or find out where it is for selection
103 * <h4>Synchronization</h4>
105 * Date formats are not synchronized. It is recommended to create separate
106 * format instances for each thread. If multiple threads access a format
107 * concurrently, it must be synchronized externally.
111 * @see SimpleDateFormat
112 * @see com.ibm.icu.util.Calendar
113 * @see com.ibm.icu.util.GregorianCalendar
114 * @see com.ibm.icu.util.TimeZone
115 * @author Mark Davis, Chen-Lieh Huang, Alan Liu
118 public abstract class DateFormat extends UFormat {
121 * The calendar that <code>DateFormat</code> uses to produce the time field
122 * values needed to implement date and time formatting. Subclasses should
123 * initialize this to a calendar appropriate for the locale associated with
124 * this <code>DateFormat</code>.
128 protected Calendar calendar;
131 * The number formatter that <code>DateFormat</code> uses to format numbers
132 * in dates and times. Subclasses should initialize this to a number format
133 * appropriate for the locale associated with this <code>DateFormat</code>.
137 protected NumberFormat numberFormat;
140 * FieldPosition selector for 'G' field alignment,
141 * corresponding to the {@link Calendar#ERA} field.
144 public final static int ERA_FIELD = 0;
147 * FieldPosition selector for 'y' field alignment,
148 * corresponding to the {@link Calendar#YEAR} field.
151 public final static int YEAR_FIELD = 1;
154 * FieldPosition selector for 'M' field alignment,
155 * corresponding to the {@link Calendar#MONTH} field.
158 public final static int MONTH_FIELD = 2;
161 * FieldPosition selector for 'd' field alignment,
162 * corresponding to the {@link Calendar#DATE} field.
165 public final static int DATE_FIELD = 3;
168 * FieldPosition selector for 'k' field alignment,
169 * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
170 * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
171 * For example, 23:59 + 01:00 results in 24:59.
174 public final static int HOUR_OF_DAY1_FIELD = 4;
177 * FieldPosition selector for 'H' field alignment,
178 * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
179 * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
180 * For example, 23:59 + 01:00 results in 00:59.
183 public final static int HOUR_OF_DAY0_FIELD = 5;
186 * FieldPosition selector for 'm' field alignment,
187 * corresponding to the {@link Calendar#MINUTE} field.
190 public final static int MINUTE_FIELD = 6;
193 * FieldPosition selector for 's' field alignment,
194 * corresponding to the {@link Calendar#SECOND} field.
197 public final static int SECOND_FIELD = 7;
200 * FieldPosition selector for 'S' field alignment,
201 * corresponding to the {@link Calendar#MILLISECOND} field.
204 public final static int FRACTIONAL_SECOND_FIELD = 8;
207 * Alias for FRACTIONAL_SECOND_FIELD.
208 * @deprecated ICU 3.0 use FRACTIONAL_SECOND_FIELD.
210 public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;
213 * FieldPosition selector for 'E' field alignment,
214 * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
217 public final static int DAY_OF_WEEK_FIELD = 9;
220 * FieldPosition selector for 'D' field alignment,
221 * corresponding to the {@link Calendar#DAY_OF_YEAR} field.
224 public final static int DAY_OF_YEAR_FIELD = 10;
227 * FieldPosition selector for 'F' field alignment,
228 * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.
231 public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
234 * FieldPosition selector for 'w' field alignment,
235 * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.
238 public final static int WEEK_OF_YEAR_FIELD = 12;
241 * FieldPosition selector for 'W' field alignment,
242 * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.
245 public final static int WEEK_OF_MONTH_FIELD = 13;
248 * FieldPosition selector for 'a' field alignment,
249 * corresponding to the {@link Calendar#AM_PM} field.
252 public final static int AM_PM_FIELD = 14;
255 * FieldPosition selector for 'h' field alignment,
256 * corresponding to the {@link Calendar#HOUR} field.
257 * HOUR1_FIELD is used for the one-based 12-hour clock.
258 * For example, 11:30 PM + 1 hour results in 12:30 AM.
261 public final static int HOUR1_FIELD = 15;
264 * FieldPosition selector for 'K' field alignment,
265 * corresponding to the {@link Calendar#HOUR} field.
266 * HOUR0_FIELD is used for the zero-based 12-hour clock.
267 * For example, 11:30 PM + 1 hour results in 00:30 AM.
270 public final static int HOUR0_FIELD = 16;
273 * FieldPosition selector for 'z' field alignment,
274 * corresponding to the {@link Calendar#ZONE_OFFSET} and
275 * {@link Calendar#DST_OFFSET} fields.
278 public final static int TIMEZONE_FIELD = 17;
281 * FieldPosition selector for 'Y' field alignment,
282 * corresponding to the {@link Calendar#YEAR_WOY} field.
285 public final static int YEAR_WOY_FIELD = 18;
288 * FieldPosition selector for 'e' field alignment,
289 * corresponding to the {@link Calendar#DOW_LOCAL} field.
292 public final static int DOW_LOCAL_FIELD = 19;
295 * FieldPosition selector for 'u' field alignment,
296 * corresponding to the {@link Calendar#EXTENDED_YEAR} field.
299 public final static int EXTENDED_YEAR_FIELD = 20;
302 * FieldPosition selector for 'g' field alignment,
303 * corresponding to the {@link Calendar#JULIAN_DAY} field.
306 public final static int JULIAN_DAY_FIELD = 21;
309 * FieldPosition selector for 'A' field alignment,
310 * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field.
313 public final static int MILLISECONDS_IN_DAY_FIELD = 22;
316 * FieldPosition selector for 'Z' field alignment,
317 * corresponding to the {@link Calendar#ZONE_OFFSET} and
318 * {@link Calendar#DST_OFFSET} fields.
321 public final static int TIMEZONE_RFC_FIELD = 23;
324 * FieldPosition selector for 'v' field alignment,
325 * corresponding to the {@link Calendar#ZONE_OFFSET} and
326 * {@link Calendar#DST_OFFSET} fields. This displays the generic zone
327 * name, if available.
330 public final static int TIMEZONE_GENERIC_FIELD = 24;
335 * FieldPosition selector for 'c' field alignment,
336 * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
337 * This displays the stand alone day name, if available.
340 public final static int STANDALONE_DAY_FIELD = 25;
343 * FieldPosition selector for 'L' field alignment,
344 * corresponding to the {@link Calendar#MONTH} field.
345 * This displays the stand alone month name, if available.
348 public final static int STANDALONE_MONTH_FIELD = 26;
351 * FieldPosition selector for 'Q' field alignment,
352 * corresponding to the {@link Calendar#MONTH} field.
353 * This displays the quarter.
356 public final static int QUARTER_FIELD = 27;
359 * FieldPosition selector for 'q' field alignment,
360 * corresponding to the {@link Calendar#MONTH} field.
361 * This displays the stand alone quarter, if available.
364 public final static int STANDALONE_QUARTER_FIELD = 28;
367 * FieldPosition selector for 'V' field alignment,
368 * corresponding to the {@link Calendar#ZONE_OFFSET} and
369 * {@link Calendar#DST_OFFSET} fields. This displays the fallback timezone
370 * name when VVVV is specified, and the short standard or daylight
371 * timezone name ignoring commonlyUsed when a single V is specified.
374 public final static int TIMEZONE_SPECIAL_FIELD = 29;
377 * Number of FieldPosition selectors for DateFormat.
378 * Valid selectors range from 0 to FIELD_COUNT-1.
381 public final static int FIELD_COUNT = 30; // must == DateFormatSymbols.patternChars.length()
383 // Proclaim serial compatibility with 1.1 FCS
384 private static final long serialVersionUID = 7218322306649953788L;
388 * Formats a time object into a time string. Examples of time objects
389 * are a time value expressed in milliseconds and a Date object.
390 * @param obj must be a Number or a Date or a Calendar.
391 * @param toAppendTo the string buffer for the returning time string.
392 * @return the formatted time string.
393 * @param fieldPosition keeps track of the position of the field
394 * within the returned string.
395 * On input: an alignment field,
396 * if desired. On output: the offsets of the alignment field. For
397 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
398 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
399 * begin index and end index of fieldPosition will be set to
400 * 0 and 4, respectively.
401 * Notice that if the same time field appears
402 * more than once in a pattern, the fieldPosition will be set for the first
403 * occurrence of that time field. For instance, formatting a Date to
404 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
405 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
406 * the begin index and end index of fieldPosition will be set to
407 * 5 and 8, respectively, for the first occurrence of the timezone
408 * pattern character 'z'.
409 * @see java.text.Format
412 public final StringBuffer format(Object obj, StringBuffer toAppendTo,
413 FieldPosition fieldPosition)
415 if (obj instanceof Calendar)
416 return format( (Calendar)obj, toAppendTo, fieldPosition );
417 else if (obj instanceof Date)
418 return format( (Date)obj, toAppendTo, fieldPosition );
419 else if (obj instanceof Number)
420 return format( new Date(((Number)obj).longValue()),
421 toAppendTo, fieldPosition );
423 throw new IllegalArgumentException("Cannot format given Object (" + obj.getClass().getName() + ") as a Date");
427 * Formats a date into a date/time string.
428 * @param cal a Calendar set to the date and time to be formatted
429 * into a date/time string. When the calendar type is different from
430 * the internal calendar held by this DateFormat instance, the date
431 * and the time zone will be inherited from the input calendar, but
432 * other calendar field values will be calculated by the internal calendar.
433 * @param toAppendTo the string buffer for the returning date/time string.
434 * @param fieldPosition keeps track of the position of the field
435 * within the returned string.
436 * On input: an alignment field,
437 * if desired. On output: the offsets of the alignment field. For
438 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
439 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
440 * begin index and end index of fieldPosition will be set to
441 * 0 and 4, respectively.
442 * Notice that if the same time field appears
443 * more than once in a pattern, the fieldPosition will be set for the first
444 * occurrence of that time field. For instance, formatting a Date to
445 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
446 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
447 * the begin index and end index of fieldPosition will be set to
448 * 5 and 8, respectively, for the first occurrence of the timezone
449 * pattern character 'z'.
450 * @return the formatted date/time string.
453 public abstract StringBuffer format(Calendar cal, StringBuffer toAppendTo,
454 FieldPosition fieldPosition);
457 * Formats a Date into a date/time string.
458 * @param date a Date to be formatted into a date/time string.
459 * @param toAppendTo the string buffer for the returning date/time string.
460 * @param fieldPosition keeps track of the position of the field
461 * within the returned string.
462 * On input: an alignment field,
463 * if desired. On output: the offsets of the alignment field. For
464 * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
465 * if the given fieldPosition is DateFormat.YEAR_FIELD, the
466 * begin index and end index of fieldPosition will be set to
467 * 0 and 4, respectively.
468 * Notice that if the same time field appears
469 * more than once in a pattern, the fieldPosition will be set for the first
470 * occurrence of that time field. For instance, formatting a Date to
471 * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
472 * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
473 * the begin index and end index of fieldPosition will be set to
474 * 5 and 8, respectively, for the first occurrence of the timezone
475 * pattern character 'z'.
476 * @return the formatted date/time string.
479 public StringBuffer format(Date date, StringBuffer toAppendTo,
480 FieldPosition fieldPosition) {
481 // Use our Calendar object
482 calendar.setTime(date);
483 return format(calendar, toAppendTo, fieldPosition);
487 * Formats a Date into a date/time string.
488 * @param date the time value to be formatted into a time string.
489 * @return the formatted time string.
492 public final String format(Date date)
494 return format(date, new StringBuffer(64),new FieldPosition(0)).toString();
498 * Parse a date/time string.
500 * @param text The date/time string to be parsed
502 * @return A Date, or null if the input could not be parsed
504 * @exception ParseException If the given string cannot be parsed as a date.
506 * @see #parse(String, ParsePosition)
509 public Date parse(String text) throws ParseException
511 ParsePosition pos = new ParsePosition(0);
512 Date result = parse(text, pos);
513 if (pos.getIndex() == 0) // ICU4J
514 throw new ParseException("Unparseable date: \"" + text + "\"" ,
515 pos.getErrorIndex()); // ICU4J
520 * Parse a date/time string according to the given parse position.
521 * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed
522 * into a Calendar that is equivalent to Date(837039928046). The
523 * caller should clear the calendar before calling this method,
524 * unless existing field information is to be kept.
526 * <p> By default, parsing is lenient: If the input is not in the form used
527 * by this object's format method but can still be parsed as a date, then
528 * the parse succeeds. Clients may insist on strict adherence to the
529 * format by calling setLenient(false).
531 * @see #setLenient(boolean)
533 * @param text The date/time string to be parsed
535 * @param cal The calendar into which parsed data will be stored.
536 * In general, this should be cleared before calling this
537 * method. If this parse fails, the calendar may still
538 * have been modified. When the calendar type is different
539 * from the internal calendar held by this DateFormat
540 * instance, calendar field values will be parsed based
541 * on the internal calendar initialized with the time and
542 * the time zone taken from this calendar, then the
543 * parse result (time in milliseconds and time zone) will
544 * be set back to this calendar.
546 * @param pos On input, the position at which to start parsing; on
547 * output, the position at which parsing terminated, or the
548 * start position if the parse failed.
551 public abstract void parse(String text, Calendar cal, ParsePosition pos);
554 * Parse a date/time string according to the given parse position. For
555 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
556 * that is equivalent to Date(837039928046).
558 * <p> By default, parsing is lenient: If the input is not in the form used
559 * by this object's format method but can still be parsed as a date, then
560 * the parse succeeds. Clients may insist on strict adherence to the
561 * format by calling setLenient(false).
563 * @see #setLenient(boolean)
565 * @param text The date/time string to be parsed
567 * @param pos On input, the position at which to start parsing; on
568 * output, the position at which parsing terminated, or the
569 * start position if the parse failed.
571 * @return A Date, or null if the input could not be parsed
574 public Date parse(String text, ParsePosition pos) {
576 int start = pos.getIndex();
577 TimeZone tzsav = calendar.getTimeZone();
579 parse(text, calendar, pos);
580 if (pos.getIndex() != start) {
582 result = calendar.getTime();
583 } catch (IllegalArgumentException e) {
584 // This occurs if the calendar is non-lenient and there is
585 // an out-of-range field. We don't know which field was
586 // illegal so we set the error index to the start.
588 pos.setErrorIndex(start);
592 calendar.setTimeZone(tzsav);
597 * Parse a date/time string into an Object. This convenience method simply
598 * calls parse(String, ParsePosition).
600 * @see #parse(String, ParsePosition)
603 public Object parseObject (String source, ParsePosition pos)
605 return parse(source, pos);
609 * Constant for empty style pattern.
612 public static final int NONE = -1;
615 * Constant for full style pattern.
618 public static final int FULL = 0;
621 * Constant for long style pattern.
624 public static final int LONG = 1;
627 * Constant for medium style pattern.
630 public static final int MEDIUM = 2;
633 * Constant for short style pattern.
636 public static final int SHORT = 3;
639 * Constant for default style pattern. Its value is MEDIUM.
642 public static final int DEFAULT = MEDIUM;
645 * Constant for relative style mask.
648 public static final int RELATIVE = (1 << 7);
651 * Constant for relative full style pattern.
654 public static final int RELATIVE_FULL = RELATIVE | FULL;
657 * Constant for relative style pattern.
660 public static final int RELATIVE_LONG = RELATIVE | LONG;
663 * Constant for relative style pattern.
666 public static final int RELATIVE_MEDIUM = RELATIVE | MEDIUM;
669 * Constant for relative style pattern.
672 public static final int RELATIVE_SHORT = RELATIVE | SHORT;
675 * Constant for relative default style pattern.
678 public static final int RELATIVE_DEFAULT = RELATIVE | DEFAULT;
680 /* Below are pre-defined skeletons
686 * 1. only keeps the field pattern letter and ignores all other parts
687 * in a pattern, such as space, punctuations, and string literals.
689 * 2. hides the order of fields.
691 * 3. might hide a field's pattern letter length.
693 * For those non-digit calendar fields, the pattern letter length is
694 * important, such as MMM, MMMM, and MMMMM; E and EEEE,
695 * and the field's pattern letter length is honored.
697 * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
698 * the field pattern length is ignored and the best match, which is
699 * defined in date time patterns, will be returned without honor
700 * the field pattern letter length in skeleton.
704 * Constant for date pattern with minute and second.
707 public static final String MINUTE_SECOND = "ms";
710 * Constant for date pattern with hour and minute in 24-hour presentation.
713 public static final String HOUR24_MINUTE = "Hm";
716 * Constant for date pattern with hour, minute, and second in
717 * 24-hour presentation.
720 public static final String HOUR24_MINUTE_SECOND = "Hms";
723 * Constant for date pattern with hour, minute, and second.
726 public static final String HOUR_MINUTE_SECOND = "hms";
729 * Constant for date pattern with standalone month.
732 public static final String STANDALONE_MONTH = "LLLL";
735 * Constant for date pattern with standalone abbreviated month.
738 public static final String ABBR_STANDALONE_MONTH = "LLL";
741 * Constant for date pattern with year and quarter.
744 public static final String YEAR_QUARTER = "yQQQ";
747 * Constant for date pattern with year and abbreviated quarter.
750 public static final String YEAR_ABBR_QUARTER = "yQ";
753 /* Below are skeletons that date interval pre-defined in resource file.
754 * Users are encouraged to use them in date interval format factory methods.
757 * Constant for date pattern with hour and minute.
760 public static final String HOUR_MINUTE = "hm";
763 * Constant for date pattern with year.
766 public static final String YEAR = "y";
769 * Constant for date pattern with day.
772 public static final String DAY = "d";
775 * Constant for date pattern with numeric month, weekday, and day.
778 public static final String NUM_MONTH_WEEKDAY_DAY = "MEd";
781 * Constant for date pattern with year and numeric month.
784 public static final String YEAR_NUM_MONTH = "yM";
787 * Constant for date pattern with numeric month and day.
790 public static final String NUM_MONTH_DAY = "Md";
793 * Constant for date pattern with year, numeric month, weekday, and day.
796 public static final String YEAR_NUM_MONTH_WEEKDAY_DAY = "yMEd";
799 * Constant for date pattern with abbreviated month, weekday, and day.
802 public static final String ABBR_MONTH_WEEKDAY_DAY = "MMMEd";
805 * Constant for date pattern with year and month.
808 public static final String YEAR_MONTH = "yMMMM";
811 * Constant for date pattern with year and abbreviated month.
814 public static final String YEAR_ABBR_MONTH = "yMMM";
817 * Constant for date pattern having month and day.
820 public static final String MONTH_DAY = "MMMMd";
823 * Constant for date pattern with abbreviated month and day.
826 public static final String ABBR_MONTH_DAY = "MMMd";
829 * Constant for date pattern with month, weekday, and day.
832 public static final String MONTH_WEEKDAY_DAY = "MMMMEEEEd";
835 * Constant for date pattern with year, abbreviated month, weekday,
839 public static final String YEAR_ABBR_MONTH_WEEKDAY_DAY = "yMMMEd";
842 * Constant for date pattern with year, month, weekday, and day.
845 public static final String YEAR_MONTH_WEEKDAY_DAY = "yMMMMEEEEd";
848 * Constant for date pattern with year, month, and day.
851 public static final String YEAR_MONTH_DAY = "yMMMMd";
854 * Constant for date pattern with year, abbreviated month, and day.
857 public static final String YEAR_ABBR_MONTH_DAY = "yMMMd";
860 * Constant for date pattern with year, numeric month, and day.
863 public static final String YEAR_NUM_MONTH_DAY = "yMd";
866 * Constant for date pattern with numeric month.
869 public static final String NUM_MONTH = "M";
872 * Constant for date pattern with abbreviated month.
875 public static final String ABBR_MONTH = "MMM";
878 * Constant for date pattern with month.
881 public static final String MONTH = "MMMM";
884 * Constant for date pattern with hour, minute, and generic timezone.
887 public static final String HOUR_MINUTE_GENERIC_TZ = "hmv";
890 * Constant for date pattern with hour, minute, and timezone.
893 public static final String HOUR_MINUTE_TZ = "hmz";
896 * Constant for date pattern with hour.
899 public static final String HOUR = "h";
902 * Constant for date pattern with hour and generic timezone.
905 public static final String HOUR_GENERIC_TZ = "hv";
908 * Constant for date pattern with hour and timezone.
911 public static final String HOUR_TZ = "hz";
914 * Gets the time formatter with the default formatting style
915 * for the default locale.
916 * @return a time formatter.
919 public final static DateFormat getTimeInstance()
921 return get(-1, DEFAULT, ULocale.getDefault());
925 * Gets the time formatter with the given formatting style
926 * for the default locale.
927 * @param style the given formatting style. For example,
928 * SHORT for "h:mm a" in the US locale.
929 * @return a time formatter.
932 public final static DateFormat getTimeInstance(int style)
934 return get(-1, style, ULocale.getDefault());
938 * Gets the time formatter with the given formatting style
939 * for the given locale.
940 * @param style the given formatting style. For example,
941 * SHORT for "h:mm a" in the US locale.
942 * @param aLocale the given locale.
943 * @return a time formatter.
946 public final static DateFormat getTimeInstance(int style,
949 return get(-1, style, ULocale.forLocale(aLocale));
953 * Gets the time formatter with the given formatting style
954 * for the given locale.
955 * @param style the given formatting style. For example,
956 * SHORT for "h:mm a" in the US locale.
957 * @param locale the given ulocale.
958 * @return a time formatter.
961 public final static DateFormat getTimeInstance(int style,
964 return get(-1, style, locale);
968 * Gets the date formatter with the default formatting style
969 * for the default locale.
970 * @return a date formatter.
973 public final static DateFormat getDateInstance()
975 return get(DEFAULT, -1, ULocale.getDefault());
979 * Gets the date formatter with the given formatting style
980 * for the default locale.
981 * @param style the given formatting style. For example,
982 * SHORT for "M/d/yy" in the US locale.
983 * @return a date formatter.
986 public final static DateFormat getDateInstance(int style)
988 return get(style, -1, ULocale.getDefault());
992 * Gets the date formatter with the given formatting style
993 * for the given locale.
994 * @param style the given formatting style. For example,
995 * SHORT for "M/d/yy" in the US locale.
996 * @param aLocale the given locale.
997 * @return a date formatter.
1000 public final static DateFormat getDateInstance(int style,
1003 return get(style, -1, ULocale.forLocale(aLocale));
1007 * Gets the date formatter with the given formatting style
1008 * for the given locale.
1009 * @param style the given formatting style. For example,
1010 * SHORT for "M/d/yy" in the US locale.
1011 * @param locale the given ulocale.
1012 * @return a date formatter.
1015 public final static DateFormat getDateInstance(int style,
1018 return get(style, -1, locale);
1022 * Gets the date/time formatter with the default formatting style
1023 * for the default locale.
1024 * @return a date/time formatter.
1027 public final static DateFormat getDateTimeInstance()
1029 return get(DEFAULT, DEFAULT, ULocale.getDefault());
1033 * Gets the date/time formatter with the given date and time
1034 * formatting styles for the default locale.
1035 * @param dateStyle the given date formatting style. For example,
1036 * SHORT for "M/d/yy" in the US locale.
1037 * @param timeStyle the given time formatting style. For example,
1038 * SHORT for "h:mm a" in the US locale.
1039 * @return a date/time formatter.
1042 public final static DateFormat getDateTimeInstance(int dateStyle,
1045 return get(dateStyle, timeStyle, ULocale.getDefault());
1049 * Gets the date/time formatter with the given formatting styles
1050 * for the given locale.
1051 * @param dateStyle the given date formatting style.
1052 * @param timeStyle the given time formatting style.
1053 * @param aLocale the given locale.
1054 * @return a date/time formatter.
1057 public final static DateFormat
1058 getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
1060 return get(dateStyle, timeStyle, ULocale.forLocale(aLocale));
1064 * Gets the date/time formatter with the given formatting styles
1065 * for the given locale.
1066 * @param dateStyle the given date formatting style.
1067 * @param timeStyle the given time formatting style.
1068 * @param locale the given ulocale.
1069 * @return a date/time formatter.
1072 public final static DateFormat
1073 getDateTimeInstance(int dateStyle, int timeStyle, ULocale locale)
1075 return get(dateStyle, timeStyle, locale);
1079 * Get a default date/time formatter that uses the SHORT style for both the
1080 * date and the time.
1083 public final static DateFormat getInstance() {
1084 return getDateTimeInstance(SHORT, SHORT);
1088 * Gets the set of locales for which DateFormats are installed.
1089 * @return the set of locales for which DateFormats are installed.
1092 public static Locale[] getAvailableLocales()
1094 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
1098 * Gets the set of locales for which DateFormats are installed.
1099 * @return the set of locales for which DateFormats are installed.
1100 * @draft ICU 3.2 (retain)
1101 * @provisional This API might change or be removed in a future release.
1103 public static ULocale[] getAvailableULocales()
1105 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
1109 * Set the calendar to be used by this date format. Initially, the default
1110 * calendar for the specified or default locale is used.
1111 * @param newCalendar the new Calendar to be used by the date format
1114 public void setCalendar(Calendar newCalendar)
1116 this.calendar = newCalendar;
1120 * Gets the calendar associated with this date/time formatter.
1121 * @return the calendar associated with this date/time formatter.
1124 public Calendar getCalendar()
1130 * Allows you to set the number formatter.
1131 * @param newNumberFormat the given new NumberFormat.
1134 public void setNumberFormat(NumberFormat newNumberFormat)
1136 this.numberFormat = newNumberFormat;
1137 /*In order to parse String like "11.10.2001" to DateTime correctly
1138 in Locale("fr","CH") [Richard/GCL]
1140 this.numberFormat.setParseIntegerOnly(true);
1144 * Gets the number formatter which this date/time formatter uses to
1145 * format and parse a time.
1146 * @return the number formatter which this date/time formatter uses.
1149 public NumberFormat getNumberFormat()
1151 return numberFormat;
1155 * Sets the time zone for the calendar of this DateFormat object.
1156 * @param zone the given new time zone.
1159 public void setTimeZone(TimeZone zone)
1161 calendar.setTimeZone(zone);
1165 * Gets the time zone.
1166 * @return the time zone associated with the calendar of DateFormat.
1169 public TimeZone getTimeZone()
1171 return calendar.getTimeZone();
1175 * Specify whether or not date/time parsing is to be lenient. With
1176 * lenient parsing, the parser may use heuristics to interpret inputs that
1177 * do not precisely match this object's format. With strict parsing,
1178 * inputs must match this object's format.
1179 * @param lenient when true, parsing is lenient
1180 * @see com.ibm.icu.util.Calendar#setLenient
1183 public void setLenient(boolean lenient)
1185 calendar.setLenient(lenient);
1189 * Tell whether date/time parsing is to be lenient.
1192 public boolean isLenient()
1194 return calendar.isLenient();
1198 * Overrides hashCode
1202 // turn off code coverage since all subclasses override this
1203 public int hashCode() {
1204 return numberFormat.hashCode();
1205 // just enough fields for a reasonable distribution
1213 public boolean equals(Object obj) {
1214 if (this == obj) return true;
1215 if (obj == null || getClass() != obj.getClass()) return false;
1216 DateFormat other = (DateFormat) obj;
1217 return (calendar.isEquivalentTo(other.calendar) &&
1218 numberFormat.equals(other.numberFormat));
1222 * Overrides Cloneable
1225 public Object clone()
1227 DateFormat other = (DateFormat) super.clone();
1228 other.calendar = (Calendar) calendar.clone();
1229 other.numberFormat = (NumberFormat) numberFormat.clone();
1234 * Creates a DateFormat with the given time and/or date style in the given
1236 * @param dateStyle a value from 0 to 3 indicating the time format,
1237 * or -1 to indicate no date
1238 * @param timeStyle a value from 0 to 3 indicating the time format,
1239 * or -1 to indicate no time
1240 * @param loc the locale for the format
1242 private static DateFormat get(int dateStyle, int timeStyle, ULocale loc) {
1243 if((timeStyle != -1 && (timeStyle & RELATIVE)>0) || (dateStyle != -1 && (dateStyle & RELATIVE)>0)) {
1244 RelativeDateFormat r = new RelativeDateFormat(timeStyle, dateStyle /* offset? */, loc);
1248 if (timeStyle < -1 || timeStyle > 3) {
1249 throw new IllegalArgumentException("Illegal time style " + timeStyle);
1251 if (dateStyle < -1 || dateStyle > 3) {
1252 throw new IllegalArgumentException("Illegal date style " + dateStyle);
1255 Calendar cal = Calendar.getInstance(loc);
1256 DateFormat result = cal.getDateTimeFormat(dateStyle, timeStyle, loc);
1257 result.setLocale(cal.getLocale(ULocale.VALID_LOCALE),
1258 cal.getLocale(ULocale.ACTUAL_LOCALE));
1260 } catch (MissingResourceException e) {
1262 // coverage requires separate run with no data, so skip
1263 return new SimpleDateFormat("M/d/yy h:mm a");
1269 * Create a new date format.
1272 protected DateFormat() {}
1274 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1276 //-------------------------------------------------------------------------
1277 // Public static interface for creating custon DateFormats for different
1278 // types of Calendars.
1279 //-------------------------------------------------------------------------
1282 * Create a {@link DateFormat} object that can be used to format dates in
1283 * the calendar system specified by <code>cal</code>.
1285 * @param cal The calendar system for which a date format is desired.
1287 * @param dateStyle The type of date format desired. This can be
1288 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1291 * @param locale The locale for which the date format is desired.
1294 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale)
1296 return getDateTimeInstance(cal, dateStyle, -1, ULocale.forLocale(locale));
1300 * Create a {@link DateFormat} object that can be used to format dates in
1301 * the calendar system specified by <code>cal</code>.
1303 * @param cal The calendar system for which a date format is desired.
1305 * @param dateStyle The type of date format desired. This can be
1306 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1309 * @param locale The locale for which the date format is desired.
1312 static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale)
1314 return getDateTimeInstance(cal, dateStyle, -1, locale);
1318 * Create a {@link DateFormat} object that can be used to format times in
1319 * the calendar system specified by <code>cal</code>.
1321 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1322 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1324 * @param cal The calendar system for which a time format is desired.
1326 * @param timeStyle The type of time format desired. This can be
1327 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1330 * @param locale The locale for which the time format is desired.
1332 * @see DateFormat#getTimeInstance
1335 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale)
1337 return getDateTimeInstance(cal, -1, timeStyle, ULocale.forLocale(locale));
1341 * Create a {@link DateFormat} object that can be used to format times in
1342 * the calendar system specified by <code>cal</code>.
1344 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1345 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1347 * @param cal The calendar system for which a time format is desired.
1349 * @param timeStyle The type of time format desired. This can be
1350 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1353 * @param locale The locale for which the time format is desired.
1355 * @see DateFormat#getTimeInstance
1358 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale)
1360 return getDateTimeInstance(cal, -1, timeStyle, locale);
1364 * Create a {@link DateFormat} object that can be used to format dates and times in
1365 * the calendar system specified by <code>cal</code>.
1367 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1368 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1370 * @param cal The calendar system for which a date/time format is desired.
1372 * @param dateStyle The type of date format desired. This can be
1373 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1376 * @param timeStyle The type of time format desired. This can be
1377 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1380 * @param locale The locale for which the date/time format is desired.
1382 * @see DateFormat#getDateTimeInstance
1385 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
1386 int timeStyle, Locale locale)
1388 return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale.forLocale(locale));
1392 * Create a {@link DateFormat} object that can be used to format dates and times in
1393 * the calendar system specified by <code>cal</code>.
1395 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1396 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1398 * @param cal The calendar system for which a date/time format is desired.
1400 * @param dateStyle The type of date format desired. This can be
1401 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1404 * @param timeStyle The type of time format desired. This can be
1405 * {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1408 * @param locale The locale for which the date/time format is desired.
1410 * @see DateFormat#getDateTimeInstance
1413 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
1414 int timeStyle, ULocale locale)
1416 return cal.getDateTimeFormat(dateStyle, timeStyle, locale);
1420 * Convenience overload
1423 static final public DateFormat getInstance(Calendar cal, Locale locale) {
1424 return getDateTimeInstance(cal, SHORT, SHORT, ULocale.forLocale(locale));
1428 * Convenience overload
1430 * @provisional This API might change or be removed in a future release.
1432 static final public DateFormat getInstance(Calendar cal, ULocale locale) {
1433 return getDateTimeInstance(cal, SHORT, SHORT, locale);
1437 * Convenience overload
1440 static final public DateFormat getInstance(Calendar cal) {
1441 return getInstance(cal, ULocale.getDefault());
1445 * Convenience overload
1448 static final public DateFormat getDateInstance(Calendar cal, int dateStyle) {
1449 return getDateInstance(cal, dateStyle, ULocale.getDefault());
1453 * Convenience overload
1456 static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) {
1457 return getTimeInstance(cal, timeStyle, ULocale.getDefault());
1461 * Convenience overload
1464 static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) {
1465 return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault());
1469 * Convenience overload
1472 public final static DateFormat getPatternInstance(String pattern) {
1473 return getPatternInstance(pattern, ULocale.getDefault());
1477 * Convenience overload
1480 public final static DateFormat getPatternInstance(String pattern, Locale locale) {
1481 return getPatternInstance(pattern, ULocale.forLocale(locale));
1485 * Create a {@link DateFormat} object that can be used to format dates and times in
1488 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1489 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1492 * @param pattern The pattern that selects the fields to be formatted. (Uses the
1493 * {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH},
1494 * {@link DateFormat#MONTH_WEEKDAY_DAY}, etc.
1496 * @param locale The locale for which the date/time format is desired.
1500 public final static DateFormat getPatternInstance(String pattern, ULocale locale) {
1501 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
1502 final String bestPattern = generator.getBestPattern(pattern);
1503 return new SimpleDateFormat(bestPattern, locale);
1507 * Convenience overload
1510 public final static DateFormat getPatternInstance(Calendar cal, String pattern, Locale locale) {
1511 return getPatternInstance(cal, pattern, ULocale.forLocale(locale));
1515 * Create a {@link DateFormat} object that can be used to format dates and times in
1516 * the calendar system specified by <code>cal</code>.
1518 * <b>Note:</b> When this functionality is moved into the core JDK, this method
1519 * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
1521 * @param cal The calendar system for which a date/time format is desired.
1523 * @param pattern The pattern that selects the fields to be formatted. (Uses the
1524 * {@link DateTimePatternGenerator}.) This can be
1525 * {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY},
1528 * @param locale The locale for which the date/time format is desired.
1532 public final static DateFormat getPatternInstance(Calendar cal, String pattern, ULocale locale) {
1533 DateTimePatternGenerator generator = DateTimePatternGenerator.getInstance(locale);
1534 final String bestPattern = generator.getBestPattern(pattern);
1535 SimpleDateFormat format = new SimpleDateFormat(bestPattern, locale);
1536 format.setCalendar(cal);
1540 //#if defined(FOUNDATION10) || defined(J2SE13)
1543 * The instances of this inner class are used as attribute keys and values
1544 * in AttributedCharacterIterator that
1545 * DateFormat.formatToCharacterIterator() method returns.
1547 * There is no public constructor to this class, the only instances are the
1548 * constants defined here.
1552 public static class Field extends Format.Field {
1554 private static final long serialVersionUID = -3627456821000730829L;
1556 // Max number of calendar fields
1557 private static final int CAL_FIELD_COUNT;
1559 // Table for mapping calendar field number to DateFormat.Field
1560 private static final Field[] CAL_FIELDS;
1562 // Map for resolving DateFormat.Field by name
1563 private static final Map FIELD_NAME_MAP;
1566 GregorianCalendar cal = new GregorianCalendar();
1567 CAL_FIELD_COUNT = cal.getFieldCount();
1568 CAL_FIELDS = new Field[CAL_FIELD_COUNT];
1569 FIELD_NAME_MAP = new HashMap(CAL_FIELD_COUNT);
1572 // Java fields -------------------
1575 * Constant identifying the time of day indicator(am/pm).
1578 public static final Field AM_PM = new Field("am pm", Calendar.AM_PM);
1581 * Constant identifying the day of month field.
1584 public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH);
1587 * Constant identifying the day of week field.
1590 public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK);
1593 * Constant identifying the day of week in month field.
1596 public static final Field DAY_OF_WEEK_IN_MONTH = new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
1599 * Constant identifying the day of year field.
1602 public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR);
1605 * Constant identifying the era field.
1608 public static final Field ERA = new Field("era", Calendar.ERA);
1611 * Constant identifying the hour(0-23) of day field.
1614 public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY);
1617 * Constant identifying the hour(1-24) of day field.
1620 public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1);
1623 * Constant identifying the hour(0-11) field.
1626 public static final Field HOUR0 = new Field("hour", Calendar.HOUR);
1629 * Constant identifying the hour(1-12) field.
1632 public static final Field HOUR1 = new Field("hour 1", -1);
1635 * Constant identifying the millisecond field.
1638 public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND);
1641 * Constant identifying the minute field.
1644 public static final Field MINUTE = new Field("minute", Calendar.MINUTE);
1647 * Constant identifying the month field.
1650 public static final Field MONTH = new Field("month", Calendar.MONTH);
1653 * Constant identifying the second field.
1656 public static final Field SECOND = new Field("second", Calendar.SECOND);
1659 * Constant identifying the time zone field.
1662 public static final Field TIME_ZONE = new Field("time zone", -1);
1665 * Constant identifying the week of month field.
1668 public static final Field WEEK_OF_MONTH = new Field("week of month", Calendar.WEEK_OF_MONTH);
1671 * Constant identifying the week of year field.
1674 public static final Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR);
1677 * Constant identifying the year field.
1680 public static final Field YEAR = new Field("year", Calendar.YEAR);
1683 // ICU only fields -------------------
1686 * Constant identifying the local day of week field.
1689 public static final Field DOW_LOCAL = new Field("local day of week", Calendar.DOW_LOCAL);
1692 * Constant identifying the extended year field.
1695 public static final Field EXTENDED_YEAR = new Field("extended year", Calendar.EXTENDED_YEAR);
1698 * Constant identifying the Julian day field.
1701 public static final Field JULIAN_DAY = new Field("Julian day", Calendar.JULIAN_DAY);
1704 * Constant identifying the milliseconds in day field.
1707 public static final Field MILLISECONDS_IN_DAY = new Field("milliseconds in day", Calendar.MILLISECONDS_IN_DAY);
1710 * Constant identifying the year used with week of year field.
1713 public static final Field YEAR_WOY = new Field("year for week of year", Calendar.YEAR_WOY);
1716 * Constant identifying the quarter field.
1719 public static final Field QUARTER = new Field("quarter", -1);
1721 // Stand alone types are variants for its base types. So we do not define Field for
1724 public static final Field STANDALONE_DAY = new Field("stand alone day of week", Calendar.DAY_OF_WEEK);
1725 public static final Field STANDALONE_MONTH = new Field("stand alone month", Calendar.MONTH);
1726 public static final Field STANDALONE_QUARTER = new Field("stand alone quarter", -1);
1729 // Corresponding calendar field
1730 private final int calendarField;
1733 * Constructs a <code>DateFormat.Field</code> with the given name and
1734 * the <code>Calendar</code> field which this attribute represents. Use -1 for
1735 * <code>calendarField</code> if this field does not have a corresponding
1736 * <code>Calendar</code> field.
1738 * @param name Name of the attribute
1739 * @param calendarField <code>Calendar</code> field constant
1743 protected Field(String name, int calendarField) {
1745 this.calendarField = calendarField;
1746 if (this.getClass() == DateFormat.Field.class) {
1747 FIELD_NAME_MAP.put(name, this);
1748 if (calendarField >= 0 && calendarField < CAL_FIELD_COUNT) {
1749 CAL_FIELDS[calendarField] = this;
1755 * Returns the <code>Field</code> constant that corresponds to the <code>
1756 * Calendar</code> field <code>calendarField</code>. If there is no
1757 * corresponding <code>Field</code> is available, null is returned.
1759 * @param calendarField <code>Calendar</code> field constant
1760 * @return <code>Field</code> associated with the <code>calendarField</code>,
1761 * or null if no associated <code>Field</code> is available.
1762 * @throws IllegalArgumentException if <code>calendarField</code> is not
1763 * a valid <code>Calendar</code> field constant.
1767 public static DateFormat.Field ofCalendarField(int calendarField) {
1768 if (calendarField < 0 || calendarField >= CAL_FIELD_COUNT) {
1769 throw new IllegalArgumentException("Calendar field number is out of range");
1771 return CAL_FIELDS[calendarField];
1775 * Returns the <code>Calendar</code> field associated with this attribute.
1776 * If there is no corresponding <code>Calendar</code> available, this will
1779 * @return <code>Calendar</code> constant for this attribute.
1783 public int getCalendarField() {
1784 return calendarField;
1788 * Resolves instances being deserialized to the predefined constants.
1790 * @throws InvalidObjectException if the constant could not be resolved.
1794 protected Object readResolve() throws InvalidObjectException {
1796 if (this.getClass() != DateFormat.Field.class) {
1797 throw new InvalidObjectException("A subclass of DateFormat.Field must implement readResolve.");
1800 Object o = FIELD_NAME_MAP.get(this.getName());
1803 throw new InvalidObjectException("Unknown attribute name.");