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