]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/text/DateFormat.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / text / DateFormat.java
1 //##header J2SE15
2 /*
3 *   Copyright (C) 1996-2009, International Business Machines
4 *   Corporation and others.  All Rights Reserved.
5 */
6
7 package com.ibm.icu.text;
8
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;
15
16 //#if defined(FOUNDATION10) || defined(J2SE13)
17 //#else
18 import java.io.InvalidObjectException;
19 import java.text.Format;
20 import java.util.HashMap;
21 import java.util.Map;
22 import com.ibm.icu.util.GregorianCalendar;
23 //#endif
24
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;
30
31 /**
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.
38  *
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
43  * descriptions.
44  *
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.
48  *
49  * <p>To format a date for the current Locale, use one of the
50  * static factory methods:
51  * <pre>
52  *  myString = DateFormat.getDateInstance().format(myDate);
53  * </pre>
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.
58  * <pre>
59  *  DateFormat df = DateFormat.getDateInstance();
60  *  for (int i = 0; i < a.length; ++i) {
61  *    output.println(df.format(myDate[i]) + "; ");
62  *  }
63  * </pre>
64  * <p>To format a number for a different Locale, specify it in the
65  * call to getDateInstance().
66  * <pre>
67  *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
68  * </pre>
69  * <p>You can use a DateFormat to parse also.
70  * <pre>
71  *  myDate = df.parse(myString);
72  * </pre>
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.
85  * </ul>
86  *
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.
94  *
95  * <p>You can also use forms of the parse and format methods with
96  * ParsePosition and FieldPosition to
97  * allow you 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
100  * on the screen.
101  * </ul>
102  *
103  * <h4>Synchronization</h4>
104  *
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. 
108  *
109  * @see          UFormat
110  * @see          NumberFormat
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
116  * @stable ICU 2.0
117  */
118 public abstract class DateFormat extends UFormat {
119
120     /**
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>.
125      * @serial
126      * @stable ICU 2.0
127      */
128     protected Calendar calendar;
129
130     /**
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>.
134      * @serial
135      * @stable ICU 2.0
136      */
137     protected NumberFormat numberFormat;
138
139     /**
140      * FieldPosition selector for 'G' field alignment,
141      * corresponding to the {@link Calendar#ERA} field.
142      * @stable ICU 2.0
143      */
144     public final static int ERA_FIELD = 0;
145
146     /**
147      * FieldPosition selector for 'y' field alignment,
148      * corresponding to the {@link Calendar#YEAR} field.
149      * @stable ICU 2.0
150      */
151     public final static int YEAR_FIELD = 1;
152
153     /**
154      * FieldPosition selector for 'M' field alignment,
155      * corresponding to the {@link Calendar#MONTH} field.
156      * @stable ICU 2.0
157      */
158     public final static int MONTH_FIELD = 2;
159
160     /**
161      * FieldPosition selector for 'd' field alignment,
162      * corresponding to the {@link Calendar#DATE} field.
163      * @stable ICU 2.0
164      */
165     public final static int DATE_FIELD = 3;
166
167     /**
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.
172      * @stable ICU 2.0
173      */
174     public final static int HOUR_OF_DAY1_FIELD = 4;
175
176     /**
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.
181      * @stable ICU 2.0
182      */
183     public final static int HOUR_OF_DAY0_FIELD = 5;
184
185     /**
186      * FieldPosition selector for 'm' field alignment,
187      * corresponding to the {@link Calendar#MINUTE} field.
188      * @stable ICU 2.0
189      */
190     public final static int MINUTE_FIELD = 6;
191
192     /**
193      * FieldPosition selector for 's' field alignment,
194      * corresponding to the {@link Calendar#SECOND} field.
195      * @stable ICU 2.0
196      */
197     public final static int SECOND_FIELD = 7;
198
199     /**
200      * FieldPosition selector for 'S' field alignment,
201      * corresponding to the {@link Calendar#MILLISECOND} field.
202      * @stable ICU 3.0
203      */
204     public final static int FRACTIONAL_SECOND_FIELD = 8;
205
206     /**
207      * Alias for FRACTIONAL_SECOND_FIELD.
208      * @deprecated ICU 3.0 use FRACTIONAL_SECOND_FIELD.
209      */
210     public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;
211
212     /**
213      * FieldPosition selector for 'E' field alignment,
214      * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
215      * @stable ICU 2.0
216      */
217     public final static int DAY_OF_WEEK_FIELD = 9;
218
219     /**
220      * FieldPosition selector for 'D' field alignment,
221      * corresponding to the {@link Calendar#DAY_OF_YEAR} field.
222      * @stable ICU 2.0
223      */
224     public final static int DAY_OF_YEAR_FIELD = 10;
225
226     /**
227      * FieldPosition selector for 'F' field alignment,
228      * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.
229      * @stable ICU 2.0
230      */
231     public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
232
233     /**
234      * FieldPosition selector for 'w' field alignment,
235      * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.
236      * @stable ICU 2.0
237      */
238     public final static int WEEK_OF_YEAR_FIELD = 12;
239
240     /**
241      * FieldPosition selector for 'W' field alignment,
242      * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.
243      * @stable ICU 2.0
244      */
245     public final static int WEEK_OF_MONTH_FIELD = 13;
246
247     /**
248      * FieldPosition selector for 'a' field alignment,
249      * corresponding to the {@link Calendar#AM_PM} field.
250      * @stable ICU 2.0
251      */
252     public final static int AM_PM_FIELD = 14;
253
254     /**
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.
259      * @stable ICU 2.0
260      */
261     public final static int HOUR1_FIELD = 15;
262
263     /**
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.
268      * @stable ICU 2.0
269      */
270     public final static int HOUR0_FIELD = 16;
271
272     /**
273      * FieldPosition selector for 'z' field alignment,
274      * corresponding to the {@link Calendar#ZONE_OFFSET} and
275      * {@link Calendar#DST_OFFSET} fields.
276      * @stable ICU 2.0
277      */
278     public final static int TIMEZONE_FIELD = 17;
279
280     /**
281      * FieldPosition selector for 'Y' field alignment,
282      * corresponding to the {@link Calendar#YEAR_WOY} field.
283      * @stable ICU 3.0
284      */
285     public final static int YEAR_WOY_FIELD = 18;
286
287     /**
288      * FieldPosition selector for 'e' field alignment,
289      * corresponding to the {@link Calendar#DOW_LOCAL} field.
290      * @stable ICU 3.0
291      */
292     public final static int DOW_LOCAL_FIELD = 19;
293
294     /**
295      * FieldPosition selector for 'u' field alignment,
296      * corresponding to the {@link Calendar#EXTENDED_YEAR} field.
297      * @stable ICU 3.0
298      */
299     public final static int EXTENDED_YEAR_FIELD = 20;
300
301     /**
302      * FieldPosition selector for 'g' field alignment,
303      * corresponding to the {@link Calendar#JULIAN_DAY} field.
304      * @stable ICU 3.0
305      */
306     public final static int JULIAN_DAY_FIELD = 21;
307
308     /**
309      * FieldPosition selector for 'A' field alignment,
310      * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field.
311      * @stable ICU 3.0
312      */
313     public final static int MILLISECONDS_IN_DAY_FIELD = 22;
314
315     /**
316      * FieldPosition selector for 'Z' field alignment,
317      * corresponding to the {@link Calendar#ZONE_OFFSET} and
318      * {@link Calendar#DST_OFFSET} fields.
319      * @stable ICU 3.0
320      */
321     public final static int TIMEZONE_RFC_FIELD = 23;
322
323     /**
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.
328      * @stable ICU 3.4
329      */
330     public final static int TIMEZONE_GENERIC_FIELD = 24;
331  
332
333     
334     /**
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.
338      * @stable ICU 3.4
339      */
340     public final static int STANDALONE_DAY_FIELD = 25;
341     
342     /**
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.
346      * @stable ICU 3.4
347      */
348     public final static int STANDALONE_MONTH_FIELD = 26;
349     
350     /**
351      * FieldPosition selector for 'Q' field alignment,
352      * corresponding to the {@link Calendar#MONTH} field.  
353      * This displays the quarter.
354      * @stable ICU 3.6
355      */
356     public final static int QUARTER_FIELD = 27;
357     
358     /**
359      * FieldPosition selector for 'q' field alignment,
360      * corresponding to the {@link Calendar#MONTH} field.  
361      * This displays the stand alone quarter, if available.
362      * @stable ICU 3.6
363      */
364     public final static int STANDALONE_QUARTER_FIELD = 28;
365     
366     /**
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.
372      * @stable ICU 3.8
373      */
374     public final static int TIMEZONE_SPECIAL_FIELD = 29;
375
376     /**
377      * Number of FieldPosition selectors for DateFormat.
378      * Valid selectors range from 0 to FIELD_COUNT-1.
379      * @stable ICU 3.0
380      */
381     public final static int FIELD_COUNT = 30; // must == DateFormatSymbols.patternChars.length()
382
383     // Proclaim serial compatibility with 1.1 FCS
384     private static final long serialVersionUID = 7218322306649953788L;
385
386     /**
387      * Overrides Format.
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
410      * @stable ICU 2.0
411      */
412     public final StringBuffer format(Object obj, StringBuffer toAppendTo,
413                                      FieldPosition fieldPosition)
414     {
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 );
422         else 
423             throw new IllegalArgumentException("Cannot format given Object (" + obj.getClass().getName() + ") as a Date");
424     }
425
426     /**
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.
451      * @stable ICU 2.0
452      */
453     public abstract StringBuffer format(Calendar cal, StringBuffer toAppendTo,
454                                         FieldPosition fieldPosition);
455
456     /**
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.
477      * @stable ICU 2.0
478      */
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);
484     }
485
486     /**
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.
490      * @stable ICU 2.0
491      */
492     public final String format(Date date)
493     {
494         return format(date, new StringBuffer(64),new FieldPosition(0)).toString();
495     }
496
497     /**
498      * Parse a date/time string.
499      *
500      * @param text  The date/time string to be parsed
501      *
502      * @return      A Date, or null if the input could not be parsed
503      *
504      * @exception  ParseException  If the given string cannot be parsed as a date.
505      *
506      * @see #parse(String, ParsePosition)
507      * @stable ICU 2.0
508      */
509     public Date parse(String text) throws ParseException
510     {
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
516         return result;
517     }
518
519     /**
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.
525      *
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).
530      *
531      * @see #setLenient(boolean)
532      *
533      * @param text  The date/time string to be parsed
534      *
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.
545      *
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.
549      * @stable ICU 2.0
550      */
551     public abstract void parse(String text, Calendar cal, ParsePosition pos);
552
553     /**
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).
557      *
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).
562      *
563      * @see #setLenient(boolean)
564      *
565      * @param text  The date/time string to be parsed
566      *
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.
570      *
571      * @return      A Date, or null if the input could not be parsed
572      * @stable ICU 2.0
573      */
574     public Date parse(String text, ParsePosition pos) {
575         Date result = null;
576         int start = pos.getIndex();
577         TimeZone tzsav = calendar.getTimeZone();
578         calendar.clear();
579         parse(text, calendar, pos);
580         if (pos.getIndex() != start) {
581             try {
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.
587                 pos.setIndex(start);
588                 pos.setErrorIndex(start);
589             }
590         }
591         // Restore TimeZone
592         calendar.setTimeZone(tzsav);
593         return result;
594     }
595
596     /**
597      * Parse a date/time string into an Object.  This convenience method simply
598      * calls parse(String, ParsePosition).
599      *
600      * @see #parse(String, ParsePosition)
601      * @stable ICU 2.0
602      */
603     public Object parseObject (String source, ParsePosition pos)
604     {
605         return parse(source, pos);
606     }
607
608     /**
609      * Constant for empty style pattern.
610      * @stable ICU 3.8
611      */
612     public static final int NONE = -1;
613     
614     /**
615      * Constant for full style pattern.
616      * @stable ICU 2.0
617      */
618     public static final int FULL = 0;
619
620     /**
621      * Constant for long style pattern.
622      * @stable ICU 2.0
623      */
624     public static final int LONG = 1;
625
626     /**
627      * Constant for medium style pattern.
628      * @stable ICU 2.0
629      */
630     public static final int MEDIUM = 2;
631
632     /**
633      * Constant for short style pattern.
634      * @stable ICU 2.0
635      */
636     public static final int SHORT = 3;
637
638     /**
639      * Constant for default style pattern.  Its value is MEDIUM.
640      * @stable ICU 2.0
641      */
642     public static final int DEFAULT = MEDIUM;
643     
644     /**
645      * Constant for relative style mask.
646      * @stable ICU 3.8
647      */
648     public static final int RELATIVE = (1 << 7);
649
650     /**
651      * Constant for relative full style pattern.
652      * @stable ICU 3.8
653      */
654     public static final int RELATIVE_FULL = RELATIVE | FULL;
655
656     /**
657      * Constant for relative style pattern.
658      * @stable ICU 3.8
659      */
660     public static final int RELATIVE_LONG = RELATIVE | LONG;
661
662     /**
663      * Constant for relative style pattern.
664      * @stable ICU 3.8
665      */
666     public static final int RELATIVE_MEDIUM = RELATIVE | MEDIUM;
667
668     /**
669      * Constant for relative style pattern.
670      * @stable ICU 3.8
671      */
672     public static final int RELATIVE_SHORT = RELATIVE | SHORT;
673
674     /**
675      * Constant for relative default style pattern.
676      * @stable ICU 3.8
677      */
678     public static final int RELATIVE_DEFAULT = RELATIVE | DEFAULT;
679
680     /* Below are pre-defined skeletons
681      *
682      * <P>
683      * A skeleton 
684      * <ul>
685      * <li>
686      * 1. only keeps the field pattern letter and ignores all other parts 
687      *    in a pattern, such as space, punctuations, and string literals.
688      * <li>
689      * 2. hides the order of fields. 
690      * <li>
691      * 3. might hide a field's pattern letter length.
692      *
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.
696      *    
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.
701      * </ul>
702      */
703     /** 
704      * Constant for date pattern with minute and second.
705      * @stable ICU 4.0
706      */
707     public static final String MINUTE_SECOND = "ms";
708
709     /** 
710      * Constant for date pattern with hour and minute in 24-hour presentation.
711      * @stable ICU 4.0
712      */
713     public static final String HOUR24_MINUTE = "Hm";
714
715     /** 
716      * Constant for date pattern with hour, minute, and second in
717      * 24-hour presentation.
718      * @stable ICU 4.0
719      */
720     public static final String HOUR24_MINUTE_SECOND = "Hms";      
721
722     /** 
723      * Constant for date pattern with hour, minute, and second.
724      * @stable ICU 4.0
725      */
726     public static final String HOUR_MINUTE_SECOND = "hms";
727
728     /** 
729      * Constant for date pattern with standalone month.
730      * @stable ICU 4.0
731      */
732     public static final String STANDALONE_MONTH = "LLLL";
733
734     /** 
735      * Constant for date pattern with standalone abbreviated month.
736      * @stable ICU 4.0
737      */
738     public static final String ABBR_STANDALONE_MONTH = "LLL";
739
740     /** 
741      * Constant for date pattern with year and quarter.
742      * @stable ICU 4.0
743      */
744     public static final String YEAR_QUARTER = "yQQQ";
745     
746     /** 
747      * Constant for date pattern with year and abbreviated quarter.
748      * @stable ICU 4.0
749      */
750     public static final String YEAR_ABBR_QUARTER = "yQ";
751
752     
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.
755      */
756     /** 
757      * Constant for date pattern with hour and minute.
758      * @stable ICU 4.0
759      */
760     public static final String HOUR_MINUTE = "hm";
761
762     /** 
763      * Constant for date pattern with year.
764      * @stable ICU 4.0
765      */
766     public static final String YEAR = "y";
767
768     /** 
769      * Constant for date pattern with day.
770      * @stable ICU 4.0
771      */
772     public static final String DAY = "d";
773
774     /** 
775      * Constant for date pattern with numeric month, weekday, and day.
776      * @stable ICU 4.0
777      */
778     public static final String NUM_MONTH_WEEKDAY_DAY = "MEd";
779
780     /** 
781      * Constant for date pattern with year and numeric month.
782      * @stable ICU 4.0
783      */
784     public static final String YEAR_NUM_MONTH = "yM";              
785
786     /** 
787      * Constant for date pattern with numeric month and day.
788      * @stable ICU 4.0
789      */
790     public static final String NUM_MONTH_DAY = "Md";
791
792     /** 
793      * Constant for date pattern with year, numeric month, weekday, and day.
794      * @stable ICU 4.0
795      */
796     public static final String YEAR_NUM_MONTH_WEEKDAY_DAY = "yMEd";
797
798     /** 
799      * Constant for date pattern with abbreviated month, weekday, and day.
800      * @stable ICU 4.0
801      */
802     public static final String ABBR_MONTH_WEEKDAY_DAY = "MMMEd";
803
804     /** 
805      * Constant for date pattern with year and month.
806      * @stable ICU 4.0
807      */
808     public static final String YEAR_MONTH = "yMMMM";
809
810     /** 
811      * Constant for date pattern with year and abbreviated month.
812      * @stable ICU 4.0
813      */
814     public static final String YEAR_ABBR_MONTH = "yMMM";
815
816     /** 
817      * Constant for date pattern having month and day.
818      * @stable ICU 4.0
819      */
820     public static final String MONTH_DAY = "MMMMd";
821
822     /** 
823      * Constant for date pattern with abbreviated month and day.
824      * @stable ICU 4.0
825      */
826     public static final String ABBR_MONTH_DAY = "MMMd"; 
827
828     /** 
829      * Constant for date pattern with month, weekday, and day.
830      * @stable ICU 4.0
831      */
832     public static final String MONTH_WEEKDAY_DAY = "MMMMEEEEd";
833
834     /** 
835      * Constant for date pattern with year, abbreviated month, weekday, 
836      * and day.
837      * @stable ICU 4.0
838      */
839     public static final String YEAR_ABBR_MONTH_WEEKDAY_DAY = "yMMMEd"; 
840
841     /** 
842      * Constant for date pattern with year, month, weekday, and day.
843      * @stable ICU 4.0
844      */
845     public static final String YEAR_MONTH_WEEKDAY_DAY = "yMMMMEEEEd";
846
847     /** 
848      * Constant for date pattern with year, month, and day.
849      * @stable ICU 4.0
850      */
851     public static final String YEAR_MONTH_DAY = "yMMMMd";
852
853     /** 
854      * Constant for date pattern with year, abbreviated month, and day.
855      * @stable ICU 4.0
856      */
857     public static final String YEAR_ABBR_MONTH_DAY = "yMMMd";
858
859     /** 
860      * Constant for date pattern with year, numeric month, and day.
861      * @stable ICU 4.0
862      */
863     public static final String YEAR_NUM_MONTH_DAY = "yMd";
864
865     /** 
866      * Constant for date pattern with numeric month.
867      * @stable ICU 4.0
868      */
869     public static final String NUM_MONTH = "M";
870
871     /** 
872      * Constant for date pattern with abbreviated month.
873      * @stable ICU 4.0
874      */
875     public static final String ABBR_MONTH = "MMM";
876
877     /** 
878      * Constant for date pattern with month.
879      * @stable ICU 4.0
880      */
881     public static final String MONTH = "MMMM";
882
883     /** 
884      * Constant for date pattern with hour, minute, and generic timezone.
885      * @stable ICU 4.0
886      */
887     public static final String HOUR_MINUTE_GENERIC_TZ = "hmv";
888
889     /** 
890      * Constant for date pattern with hour, minute, and timezone.
891      * @stable ICU 4.0
892      */
893     public static final String HOUR_MINUTE_TZ = "hmz";
894
895     /** 
896      * Constant for date pattern with hour.
897      * @stable ICU 4.0
898      */
899     public static final String HOUR = "h";
900
901     /** 
902      * Constant for date pattern with hour and generic timezone.
903      * @stable ICU 4.0
904      */
905     public static final String HOUR_GENERIC_TZ = "hv";
906
907     /** 
908      * Constant for date pattern with hour and timezone.
909      * @stable ICU 4.0
910      */
911     public static final String HOUR_TZ = "hz";
912
913     /**
914      * Gets the time formatter with the default formatting style
915      * for the default locale.
916      * @return a time formatter.
917      * @stable ICU 2.0
918      */
919     public final static DateFormat getTimeInstance()
920     {
921         return get(-1, DEFAULT, ULocale.getDefault());
922     }
923
924     /**
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.
930      * @stable ICU 2.0
931      */
932     public final static DateFormat getTimeInstance(int style)
933     {
934         return get(-1, style, ULocale.getDefault());
935     }
936
937     /**
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.
944      * @stable ICU 2.0
945      */
946     public final static DateFormat getTimeInstance(int style,
947                                                  Locale aLocale)
948     {
949         return get(-1, style, ULocale.forLocale(aLocale));
950     }
951
952     /**
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.
959      * @stable ICU 3.2
960      */
961     public final static DateFormat getTimeInstance(int style,
962                                                  ULocale locale)
963     {
964         return get(-1, style, locale);
965     }
966
967     /**
968      * Gets the date formatter with the default formatting style
969      * for the default locale.
970      * @return a date formatter.
971      * @stable ICU 2.0
972      */
973     public final static DateFormat getDateInstance()
974     {
975         return get(DEFAULT, -1, ULocale.getDefault());
976     }
977
978     /**
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.
984      * @stable ICU 2.0
985      */
986     public final static DateFormat getDateInstance(int style)
987     {
988         return get(style, -1, ULocale.getDefault());
989     }
990
991     /**
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.
998      * @stable ICU 2.0
999      */
1000     public final static DateFormat getDateInstance(int style,
1001                                                  Locale aLocale)
1002     {
1003         return get(style, -1, ULocale.forLocale(aLocale));
1004     }
1005
1006     /**
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.
1013      * @stable ICU 3.2
1014      */
1015     public final static DateFormat getDateInstance(int style,
1016                                                  ULocale locale)
1017     {
1018         return get(style, -1, locale);
1019     }
1020
1021     /**
1022      * Gets the date/time formatter with the default formatting style
1023      * for the default locale.
1024      * @return a date/time formatter.
1025      * @stable ICU 2.0
1026      */
1027     public final static DateFormat getDateTimeInstance()
1028     {
1029         return get(DEFAULT, DEFAULT, ULocale.getDefault());
1030     }
1031
1032     /**
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.
1040      * @stable ICU 2.0
1041      */
1042     public final static DateFormat getDateTimeInstance(int dateStyle,
1043                                                        int timeStyle)
1044     {
1045         return get(dateStyle, timeStyle, ULocale.getDefault());
1046     }
1047
1048     /**
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.
1055      * @stable ICU 2.0
1056      */
1057     public final static DateFormat
1058         getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
1059     {
1060         return get(dateStyle, timeStyle, ULocale.forLocale(aLocale));
1061     }
1062
1063     /**
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.
1070      * @stable ICU 3.2
1071      */
1072     public final static DateFormat
1073         getDateTimeInstance(int dateStyle, int timeStyle, ULocale locale)
1074     {
1075         return get(dateStyle, timeStyle, locale);
1076     }
1077
1078     /**
1079      * Get a default date/time formatter that uses the SHORT style for both the
1080      * date and the time.
1081      * @stable ICU 2.0
1082      */
1083     public final static DateFormat getInstance() {
1084         return getDateTimeInstance(SHORT, SHORT);
1085     }
1086
1087     /**
1088      * Gets the set of locales for which DateFormats are installed.
1089      * @return the set of locales for which DateFormats are installed.
1090      * @stable ICU 2.0
1091      */
1092     public static Locale[] getAvailableLocales()
1093     {
1094         return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME);
1095     }
1096
1097     /**
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.
1102      */
1103     public static ULocale[] getAvailableULocales()
1104     {
1105         return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME);
1106     }
1107
1108     /**
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
1112      * @stable ICU 2.0
1113      */
1114     public void setCalendar(Calendar newCalendar)
1115     {
1116         this.calendar = newCalendar;
1117     }
1118
1119     /**
1120      * Gets the calendar associated with this date/time formatter.
1121      * @return the calendar associated with this date/time formatter.
1122      * @stable ICU 2.0
1123      */
1124     public Calendar getCalendar()
1125     {
1126         return calendar;
1127     }
1128
1129     /**
1130      * Allows you to set the number formatter.
1131      * @param newNumberFormat the given new NumberFormat.
1132      * @stable ICU 2.0
1133      */
1134     public void setNumberFormat(NumberFormat newNumberFormat)
1135     {
1136         this.numberFormat = newNumberFormat;
1137         /*In order to parse String like "11.10.2001" to DateTime correctly 
1138           in Locale("fr","CH") [Richard/GCL]
1139         */
1140         this.numberFormat.setParseIntegerOnly(true);
1141     }
1142
1143     /**
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.
1147      * @stable ICU 2.0
1148      */
1149     public NumberFormat getNumberFormat()
1150     {
1151         return numberFormat;
1152     }
1153
1154     /**
1155      * Sets the time zone for the calendar of this DateFormat object.
1156      * @param zone the given new time zone.
1157      * @stable ICU 2.0
1158      */
1159     public void setTimeZone(TimeZone zone)
1160     {
1161         calendar.setTimeZone(zone);
1162     }
1163
1164     /**
1165      * Gets the time zone.
1166      * @return the time zone associated with the calendar of DateFormat.
1167      * @stable ICU 2.0
1168      */
1169     public TimeZone getTimeZone()
1170     {
1171         return calendar.getTimeZone();
1172     }
1173
1174     /**
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
1181      * @stable ICU 2.0
1182      */
1183     public void setLenient(boolean lenient)
1184     {
1185         calendar.setLenient(lenient);
1186     }
1187
1188     /**
1189      * Tell whether date/time parsing is to be lenient.
1190      * @stable ICU 2.0
1191      */
1192     public boolean isLenient()
1193     {
1194         return calendar.isLenient();
1195     }
1196
1197     /**
1198      * Overrides hashCode
1199      * @stable ICU 2.0
1200      */
1201     ///CLOVER:OFF
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
1206     }
1207     ///CLOVER:ON
1208
1209     /**
1210      * Overrides equals
1211      * @stable ICU 2.0
1212      */
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));
1219     }
1220
1221     /**
1222      * Overrides Cloneable
1223      * @stable ICU 2.0
1224      */
1225     public Object clone()
1226     {
1227         DateFormat other = (DateFormat) super.clone();
1228         other.calendar = (Calendar) calendar.clone();
1229         other.numberFormat = (NumberFormat) numberFormat.clone();
1230         return other;
1231     }
1232
1233     /**
1234      * Creates a DateFormat with the given time and/or date style in the given
1235      * locale.
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
1241      */
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);
1245             return r;
1246         }
1247     
1248         if (timeStyle < -1 || timeStyle > 3) {
1249             throw new IllegalArgumentException("Illegal time style " + timeStyle);
1250         }
1251         if (dateStyle < -1 || dateStyle > 3) {
1252             throw new IllegalArgumentException("Illegal date style " + dateStyle);
1253         }
1254         try {
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));
1259             return result;
1260         } catch (MissingResourceException e) {
1261             ///CLOVER:OFF
1262             // coverage requires separate run with no data, so skip
1263             return new SimpleDateFormat("M/d/yy h:mm a");
1264             ///CLOVER:ON
1265         }
1266     }
1267
1268     /**
1269      * Create a new date format.
1270      * @stable ICU 2.0
1271      */
1272     protected DateFormat() {}
1273
1274     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1275
1276     //-------------------------------------------------------------------------
1277     // Public static interface for creating custon DateFormats for different
1278     // types of Calendars.
1279     //-------------------------------------------------------------------------
1280     
1281     /**
1282      * Create a {@link DateFormat} object that can be used to format dates in
1283      * the calendar system specified by <code>cal</code>.
1284      * <p>
1285      * @param cal   The calendar system for which a date format is desired.
1286      *
1287      * @param dateStyle The type of date format desired.  This can be
1288      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1289      *              etc.
1290      *
1291      * @param locale The locale for which the date format is desired.
1292      * @stable ICU 2.0
1293      */
1294     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale)
1295     {
1296         return getDateTimeInstance(cal, dateStyle, -1, ULocale.forLocale(locale));
1297     }
1298     
1299     /**
1300      * Create a {@link DateFormat} object that can be used to format dates in
1301      * the calendar system specified by <code>cal</code>.
1302      * <p>
1303      * @param cal   The calendar system for which a date format is desired.
1304      *
1305      * @param dateStyle The type of date format desired.  This can be
1306      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1307      *              etc.
1308      *
1309      * @param locale The locale for which the date format is desired.
1310      * @stable ICU 3.2
1311      */
1312     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale)
1313     {
1314         return getDateTimeInstance(cal, dateStyle, -1, locale);
1315     }
1316     
1317     /**
1318      * Create a {@link DateFormat} object that can be used to format times in
1319      * the calendar system specified by <code>cal</code>.
1320      * <p>
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}.
1323      * <p>
1324      * @param cal   The calendar system for which a time format is desired.
1325      *
1326      * @param timeStyle The type of time format desired.  This can be
1327      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1328      *              etc.
1329      *
1330      * @param locale The locale for which the time format is desired.
1331      *
1332      * @see DateFormat#getTimeInstance
1333      * @stable ICU 2.0
1334      */
1335     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale)
1336     {
1337         return getDateTimeInstance(cal, -1, timeStyle, ULocale.forLocale(locale));
1338     }
1339     
1340     /**
1341      * Create a {@link DateFormat} object that can be used to format times in
1342      * the calendar system specified by <code>cal</code>.
1343      * <p>
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}.
1346      * <p>
1347      * @param cal   The calendar system for which a time format is desired.
1348      *
1349      * @param timeStyle The type of time format desired.  This can be
1350      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1351      *              etc.
1352      *
1353      * @param locale The locale for which the time format is desired.
1354      *
1355      * @see DateFormat#getTimeInstance
1356      * @stable ICU 3.2
1357      */
1358     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale)
1359     {
1360         return getDateTimeInstance(cal, -1, timeStyle, locale);
1361     }
1362     
1363     /**
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>.
1366      * <p>
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}.
1369      * <p>
1370      * @param cal   The calendar system for which a date/time format is desired.
1371      *
1372      * @param dateStyle The type of date format desired.  This can be
1373      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1374      *              etc.
1375      *
1376      * @param timeStyle The type of time format desired.  This can be
1377      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1378      *              etc.
1379      *
1380      * @param locale The locale for which the date/time format is desired.
1381      *
1382      * @see DateFormat#getDateTimeInstance
1383      * @stable ICU 2.0
1384      */
1385     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
1386                                                  int timeStyle, Locale locale)
1387     {
1388         return cal.getDateTimeFormat(dateStyle, timeStyle, ULocale.forLocale(locale));
1389     }
1390
1391     /**
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>.
1394      * <p>
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}.
1397      * <p>
1398      * @param cal   The calendar system for which a date/time format is desired.
1399      *
1400      * @param dateStyle The type of date format desired.  This can be
1401      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1402      *              etc.
1403      *
1404      * @param timeStyle The type of time format desired.  This can be
1405      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
1406      *              etc.
1407      *
1408      * @param locale The locale for which the date/time format is desired.
1409      *
1410      * @see DateFormat#getDateTimeInstance
1411      * @stable ICU 3.2
1412      */
1413     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
1414                                                  int timeStyle, ULocale locale)
1415     {
1416         return cal.getDateTimeFormat(dateStyle, timeStyle, locale);
1417     }
1418
1419     /**
1420      * Convenience overload
1421      * @stable ICU 2.0
1422      */
1423     static final public DateFormat getInstance(Calendar cal, Locale locale) {
1424         return getDateTimeInstance(cal, SHORT, SHORT, ULocale.forLocale(locale));
1425     }
1426
1427     /**
1428      * Convenience overload
1429      * @stable ICU 3.2
1430      * @provisional This API might change or be removed in a future release.
1431      */
1432     static final public DateFormat getInstance(Calendar cal, ULocale locale) {
1433         return getDateTimeInstance(cal, SHORT, SHORT, locale);
1434     }
1435
1436     /**
1437      * Convenience overload
1438      * @stable ICU 2.0
1439      */
1440     static final public DateFormat getInstance(Calendar cal) {
1441         return getInstance(cal, ULocale.getDefault());
1442     }
1443
1444     /**
1445      * Convenience overload
1446      * @stable ICU 2.0
1447      */
1448     static final public DateFormat getDateInstance(Calendar cal, int dateStyle) {
1449         return getDateInstance(cal, dateStyle, ULocale.getDefault());
1450     }
1451
1452     /**
1453      * Convenience overload
1454      * @stable ICU 2.0
1455      */
1456     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) {
1457         return getTimeInstance(cal, timeStyle, ULocale.getDefault());
1458     }
1459
1460     /**
1461      * Convenience overload
1462      * @stable ICU 2.0
1463      */
1464     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) {
1465         return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault());
1466     }
1467
1468     /**
1469      * Convenience overload
1470      * @stable ICU 4.0
1471      */
1472     public final static DateFormat getPatternInstance(String pattern) {
1473         return getPatternInstance(pattern, ULocale.getDefault());
1474     }
1475
1476     /**
1477      * Convenience overload
1478      * @stable ICU 4.0
1479      */
1480     public final static DateFormat getPatternInstance(String pattern, Locale locale) {
1481         return getPatternInstance(pattern, ULocale.forLocale(locale));
1482     }
1483
1484     /**
1485      * Create a {@link DateFormat} object that can be used to format dates and times in
1486      * the given locale.
1487      * <p>
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}.
1490      * <p>
1491      *
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.
1495      *
1496      * @param locale The locale for which the date/time format is desired.
1497      *
1498      * @stable ICU 4.0
1499      */
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);
1504     }
1505
1506     /**
1507      * Convenience overload
1508      * @stable ICU 4.0
1509      */
1510     public final static DateFormat getPatternInstance(Calendar cal, String pattern, Locale locale) {
1511         return getPatternInstance(cal, pattern, ULocale.forLocale(locale));
1512     }
1513
1514     /**
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>.
1517      * <p>
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}.
1520      * <p>
1521      * @param cal   The calendar system for which a date/time format is desired.
1522      *
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},
1526      *              etc.
1527      *
1528      * @param locale The locale for which the date/time format is desired.
1529      *
1530      * @stable ICU 4.0
1531      */
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);
1537         return format;
1538     }
1539
1540 //#if defined(FOUNDATION10) || defined(J2SE13)
1541 //#else
1542     /**
1543      * The instances of this inner class are used as attribute keys and values
1544      * in AttributedCharacterIterator that
1545      * DateFormat.formatToCharacterIterator() method returns.
1546      * <p>
1547      * There is no public constructor to this class, the only instances are the
1548      * constants defined here.
1549      * <p>
1550      * @stable ICU 3.8
1551      */
1552     public static class Field extends Format.Field {
1553
1554         private static final long serialVersionUID = -3627456821000730829L;
1555
1556         // Max number of calendar fields
1557         private static final int CAL_FIELD_COUNT;
1558
1559         // Table for mapping calendar field number to DateFormat.Field
1560         private static final Field[] CAL_FIELDS;
1561  
1562         // Map for resolving DateFormat.Field by name
1563         private static final Map FIELD_NAME_MAP;
1564
1565         static {
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);
1570         }
1571
1572         // Java fields -------------------
1573
1574         /**
1575          * Constant identifying the time of day indicator(am/pm).
1576          * @stable ICU 3.8
1577          */
1578         public static final Field AM_PM = new Field("am pm", Calendar.AM_PM);
1579
1580         /**
1581          * Constant identifying the day of month field.
1582          * @stable ICU 3.8
1583          */
1584         public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH);
1585
1586         /**
1587          * Constant identifying the day of week field.
1588          * @stable ICU 3.8
1589          */
1590         public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK);
1591
1592         /**
1593          * Constant identifying the day of week in month field.
1594          * @stable ICU 3.8
1595          */
1596         public static final Field DAY_OF_WEEK_IN_MONTH = new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
1597
1598         /**
1599          * Constant identifying the day of year field.
1600          * @stable ICU 3.8
1601          */
1602         public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR);
1603
1604         /**
1605          * Constant identifying the era field.
1606          * @stable ICU 3.8
1607          */
1608         public static final Field ERA = new Field("era", Calendar.ERA);
1609
1610         /**
1611          * Constant identifying the hour(0-23) of day field.
1612          * @stable ICU 3.8
1613          */
1614         public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY);
1615
1616         /**
1617          * Constant identifying the hour(1-24) of day field.
1618          * @stable ICU 3.8
1619          */
1620         public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1);
1621
1622         /**
1623          * Constant identifying the hour(0-11) field.
1624          * @stable ICU 3.8
1625          */
1626         public static final Field HOUR0 = new Field("hour", Calendar.HOUR);
1627
1628         /**
1629          * Constant identifying the hour(1-12) field.
1630          * @stable ICU 3.8
1631          */
1632         public static final Field HOUR1 = new Field("hour 1", -1);
1633
1634         /**
1635          * Constant identifying the millisecond field.
1636          * @stable ICU 3.8
1637          */
1638         public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND);
1639
1640         /**
1641          * Constant identifying the minute field.
1642          * @stable ICU 3.8
1643          */
1644         public static final Field MINUTE = new Field("minute", Calendar.MINUTE);
1645
1646         /**
1647          * Constant identifying the month field.
1648          * @stable ICU 3.8
1649          */
1650         public static final Field MONTH = new Field("month", Calendar.MONTH);
1651
1652         /**
1653          * Constant identifying the second field.
1654          * @stable ICU 3.8
1655          */
1656         public static final Field SECOND = new Field("second", Calendar.SECOND);
1657
1658         /**
1659          * Constant identifying the time zone field.
1660          * @stable ICU 3.8
1661          */
1662         public static final Field TIME_ZONE = new Field("time zone", -1);
1663
1664         /**
1665          * Constant identifying the week of month field.
1666          * @stable ICU 3.8
1667          */
1668         public static final Field WEEK_OF_MONTH = new Field("week of month", Calendar.WEEK_OF_MONTH);
1669
1670         /**
1671          * Constant identifying the week of year field.
1672          * @stable ICU 3.8
1673          */
1674         public static final Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR);
1675
1676         /**
1677          * Constant identifying the year field.
1678          * @stable ICU 3.8
1679          */
1680         public static final Field YEAR = new Field("year", Calendar.YEAR);
1681
1682
1683         // ICU only fields -------------------
1684
1685         /**
1686          * Constant identifying the local day of week field.
1687          * @stable ICU 3.8
1688          */
1689         public static final Field DOW_LOCAL = new Field("local day of week", Calendar.DOW_LOCAL);
1690
1691         /**
1692          * Constant identifying the extended year field.
1693          * @stable ICU 3.8
1694          */
1695         public static final Field EXTENDED_YEAR = new Field("extended year", Calendar.EXTENDED_YEAR);
1696
1697         /**
1698          * Constant identifying the Julian day field.
1699          * @stable ICU 3.8
1700          */
1701         public static final Field JULIAN_DAY = new Field("Julian day", Calendar.JULIAN_DAY);
1702
1703         /**
1704          * Constant identifying the milliseconds in day field.
1705          * @stable ICU 3.8
1706          */
1707         public static final Field MILLISECONDS_IN_DAY = new Field("milliseconds in day", Calendar.MILLISECONDS_IN_DAY);
1708
1709         /**
1710          * Constant identifying the year used with week of year field.
1711          * @stable ICU 3.8
1712          */
1713         public static final Field YEAR_WOY = new Field("year for week of year", Calendar.YEAR_WOY);
1714
1715         /**
1716          * Constant identifying the quarter field.
1717          * @stable ICU 3.8
1718          */
1719         public static final Field QUARTER = new Field("quarter", -1);
1720
1721         // Stand alone types are variants for its base types.  So we do not define Field for
1722         // them.
1723         /*
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);
1727         */
1728
1729         // Corresponding calendar field
1730         private final int calendarField;
1731
1732         /**
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.
1737          * 
1738          * @param name          Name of the attribute
1739          * @param calendarField <code>Calendar</code> field constant
1740          * 
1741          * @stable ICU 3.8
1742          */
1743         protected Field(String name, int calendarField) {
1744             super(name);
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;
1750                 }
1751             }
1752         }
1753
1754         /**
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.
1758          * 
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.
1764          * 
1765          * @stable ICU 3.8
1766          */
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");
1770             }
1771             return CAL_FIELDS[calendarField];
1772         }
1773         
1774         /**
1775          * Returns the <code>Calendar</code> field associated with this attribute.
1776          * If there is no corresponding <code>Calendar</code> available, this will
1777          * return -1.
1778          * 
1779          * @return <code>Calendar</code> constant for this attribute.
1780          * 
1781          * @stable ICU 3.8
1782          */
1783         public int getCalendarField() {
1784             return calendarField;
1785         }
1786         
1787         /**
1788          * Resolves instances being deserialized to the predefined constants.
1789          * 
1790          * @throws InvalidObjectException if the constant could not be resolved.
1791          * 
1792          * @stable ICU 3.8
1793          */
1794         protected Object readResolve() throws InvalidObjectException {
1795             ///CLOVER:OFF
1796             if (this.getClass() != DateFormat.Field.class) {
1797                 throw new InvalidObjectException("A subclass of DateFormat.Field must implement readResolve.");
1798             }
1799             ///CLOVER:ON
1800             Object o = FIELD_NAME_MAP.get(this.getName());
1801             ///CLOVER:OFF
1802             if (o == null) {
1803                 throw new InvalidObjectException("Unknown attribute name.");
1804             }
1805             ///CLOVER:ON
1806             return o;
1807         }
1808     }
1809 //#endif
1810     
1811 }