Home | History | Annotate | Download | only in text
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *   Copyright (C) 1996-2012, International Business Machines
      5  *   Corporation and others.  All Rights Reserved.
      6  */
      7 
      8 package com.ibm.icu.text;
      9 
     10 import java.io.InvalidObjectException;
     11 import java.text.FieldPosition;
     12 import java.text.Format;
     13 import java.text.ParseException;
     14 import java.text.ParsePosition;
     15 import java.util.Date;
     16 import java.util.HashMap;
     17 import java.util.Locale;
     18 import java.util.Map;
     19 
     20 import com.ibm.icu.util.Calendar;
     21 import com.ibm.icu.util.TimeZone;
     22 import com.ibm.icu.util.ULocale;
     23 import com.ibm.icu.util.ULocale.Category;
     24 
     25 /**
     26  * {@icuenhanced java.text.DateFormat}.{@icu _usage_}
     27  *
     28  * <p>DateFormat is an abstract class for date/time formatting subclasses which
     29  * formats and parses dates or time in a language-independent manner.
     30  * The date/time formatting subclass, such as SimpleDateFormat, allows for
     31  * formatting (i.e., date -> text), parsing (text -> date), and
     32  * normalization.  The date is represented as a <code>Date</code> object or
     33  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
     34  *
     35  * <p>DateFormat provides many class methods for obtaining default date/time
     36  * formatters based on the default or a given locale and a number of formatting
     37  * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More
     38  * detail and examples of using these styles are provided in the method
     39  * descriptions.
     40  *
     41  * <p>DateFormat helps you to format and parse dates for any locale.
     42  * Your code can be completely independent of the locale conventions for
     43  * months, days of the week, or even the calendar format: lunar vs. solar.
     44  *
     45  * <p>To format a date for the current Locale, use one of the
     46  * static factory methods:
     47  * <pre>
     48  *  myString = DateFormat.getDateInstance().format(myDate);
     49  * </pre>
     50  * <p>If you are formatting multiple numbers, it is
     51  * more efficient to get the format and use it multiple times so that
     52  * the system doesn't have to fetch the information about the local
     53  * language and country conventions multiple times.
     54  * <pre>
     55  *  DateFormat df = DateFormat.getDateInstance();
     56  *  for (int i = 0; i < a.length; ++i) {
     57  *    output.println(df.format(myDate[i]) + "; ");
     58  *  }
     59  * </pre>
     60  * <p>To format a number for a different Locale, specify it in the
     61  * call to getDateInstance().
     62  * <pre>
     63  *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
     64  * </pre>
     65  * <p>You can use a DateFormat to parse also.
     66  * <pre>
     67  *  myDate = df.parse(myString);
     68  * </pre>
     69  * <p>Use getDateInstance to get the normal date format for that country.
     70  * There are other static factory methods available.
     71  * Use getTimeInstance to get the time format for that country.
     72  * Use getDateTimeInstance to get a date and time format. You can pass in
     73  * different options to these factory methods to control the length of the
     74  * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends
     75  * on the locale, but generally:
     76  * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm
     77  * <li>MEDIUM is longer, such as Jan 12, 1952
     78  * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm
     79  * <li>FULL is pretty completely specified, such as
     80  * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
     81  * </ul>
     82  *
     83  * <p>You can also set the time zone on the format if you wish.
     84  * If you want even more control over the format or parsing,
     85  * (or want to give your users more control),
     86  * you can try casting the DateFormat you get from the factory methods
     87  * to a SimpleDateFormat. This will work for the majority
     88  * of countries; just remember to put it in a try block in case you
     89  * encounter an unusual one.
     90  *
     91  * <p>You can also use forms of the parse and format methods with
     92  * ParsePosition and FieldPosition to
     93  * allow you to
     94  * <ul><li>progressively parse through pieces of a string.
     95  * <li>align any particular field, or find out where it is for selection
     96  * on the screen.
     97  * </ul>
     98  *
     99  * <h4>Synchronization</h4>
    100  *
    101  * Date formats are not synchronized. It is recommended to create separate
    102  * format instances for each thread. If multiple threads access a format
    103  * concurrently, it must be synchronized externally.
    104  *
    105  * @see          UFormat
    106  * @see          NumberFormat
    107  * @see          SimpleDateFormat
    108  * @see          com.ibm.icu.util.Calendar
    109  * @see          com.ibm.icu.util.GregorianCalendar
    110  * @see          com.ibm.icu.util.TimeZone
    111  * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
    112  * @stable ICU 2.0
    113  */
    114 public class DateFormat extends Format {
    115 
    116     private static final long serialVersionUID = 1L;
    117 
    118     /**
    119      * @internal
    120      */
    121     public final java.text.DateFormat dateFormat;
    122 
    123     /**
    124      * @internal
    125      * @param delegate the DateFormat to which to delegate
    126      */
    127     public DateFormat(java.text.DateFormat delegate) {
    128         this.dateFormat = delegate;
    129     }
    130 
    131     /**
    132      * For subclass use.  Subclasses will generally not
    133      * work correctly unless they manipulate the delegate.
    134      */
    135     protected DateFormat() {
    136         this.dateFormat = java.text.DateFormat.getDateTimeInstance(
    137                 java.text.DateFormat.SHORT,
    138                 java.text.DateFormat.SHORT,
    139                 ULocale.getDefault(Category.FORMAT).toLocale());
    140     }
    141 
    142     /**
    143      * FieldPosition selector for 'G' field alignment,
    144      * corresponding to the {@link Calendar#ERA} field.
    145      * @stable ICU 2.0
    146      */
    147     public final static int ERA_FIELD = 0;
    148 
    149     /**
    150      * FieldPosition selector for 'y' field alignment,
    151      * corresponding to the {@link Calendar#YEAR} field.
    152      * @stable ICU 2.0
    153      */
    154     public final static int YEAR_FIELD = 1;
    155 
    156     /**
    157      * FieldPosition selector for 'M' field alignment,
    158      * corresponding to the {@link Calendar#MONTH} field.
    159      * @stable ICU 2.0
    160      */
    161     public final static int MONTH_FIELD = 2;
    162 
    163     /**
    164      * FieldPosition selector for 'd' field alignment,
    165      * corresponding to the {@link Calendar#DATE} field.
    166      * @stable ICU 2.0
    167      */
    168     public final static int DATE_FIELD = 3;
    169 
    170     /**
    171      * FieldPosition selector for 'k' field alignment,
    172      * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
    173      * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
    174      * For example, 23:59 + 01:00 results in 24:59.
    175      * @stable ICU 2.0
    176      */
    177     public final static int HOUR_OF_DAY1_FIELD = 4;
    178 
    179     /**
    180      * FieldPosition selector for 'H' field alignment,
    181      * corresponding to the {@link Calendar#HOUR_OF_DAY} field.
    182      * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
    183      * For example, 23:59 + 01:00 results in 00:59.
    184      * @stable ICU 2.0
    185      */
    186     public final static int HOUR_OF_DAY0_FIELD = 5;
    187 
    188     /**
    189      * FieldPosition selector for 'm' field alignment,
    190      * corresponding to the {@link Calendar#MINUTE} field.
    191      * @stable ICU 2.0
    192      */
    193     public final static int MINUTE_FIELD = 6;
    194 
    195     /**
    196      * FieldPosition selector for 's' field alignment,
    197      * corresponding to the {@link Calendar#SECOND} field.
    198      * @stable ICU 2.0
    199      */
    200     public final static int SECOND_FIELD = 7;
    201 
    202     /**
    203      * {@icu} FieldPosition selector for 'S' field alignment,
    204      * corresponding to the {@link Calendar#MILLISECOND} field.
    205      * @stable ICU 3.0
    206      */
    207     public final static int FRACTIONAL_SECOND_FIELD = 8;
    208 
    209     /**
    210      * Alias for FRACTIONAL_SECOND_FIELD.
    211      * @stable ICU 3.0 FRACTIONAL_SECOND_FIELD.
    212      */
    213     public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;
    214 
    215     /**
    216      * FieldPosition selector for 'E' field alignment,
    217      * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
    218      * @stable ICU 2.0
    219      */
    220     public final static int DAY_OF_WEEK_FIELD = 9;
    221 
    222     /**
    223      * FieldPosition selector for 'D' field alignment,
    224      * corresponding to the {@link Calendar#DAY_OF_YEAR} field.
    225      * @stable ICU 2.0
    226      */
    227     public final static int DAY_OF_YEAR_FIELD = 10;
    228 
    229     /**
    230      * FieldPosition selector for 'F' field alignment,
    231      * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.
    232      * @stable ICU 2.0
    233      */
    234     public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
    235 
    236     /**
    237      * FieldPosition selector for 'w' field alignment,
    238      * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.
    239      * @stable ICU 2.0
    240      */
    241     public final static int WEEK_OF_YEAR_FIELD = 12;
    242 
    243     /**
    244      * FieldPosition selector for 'W' field alignment,
    245      * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.
    246      * @stable ICU 2.0
    247      */
    248     public final static int WEEK_OF_MONTH_FIELD = 13;
    249 
    250     /**
    251      * FieldPosition selector for 'a' field alignment,
    252      * corresponding to the {@link Calendar#AM_PM} field.
    253      * @stable ICU 2.0
    254      */
    255     public final static int AM_PM_FIELD = 14;
    256 
    257     /**
    258      * FieldPosition selector for 'h' field alignment,
    259      * corresponding to the {@link Calendar#HOUR} field.
    260      * HOUR1_FIELD is used for the one-based 12-hour clock.
    261      * For example, 11:30 PM + 1 hour results in 12:30 AM.
    262      * @stable ICU 2.0
    263      */
    264     public final static int HOUR1_FIELD = 15;
    265 
    266     /**
    267      * FieldPosition selector for 'K' field alignment,
    268      * corresponding to the {@link Calendar#HOUR} field.
    269      * HOUR0_FIELD is used for the zero-based 12-hour clock.
    270      * For example, 11:30 PM + 1 hour results in 00:30 AM.
    271      * @stable ICU 2.0
    272      */
    273     public final static int HOUR0_FIELD = 16;
    274 
    275     /**
    276      * FieldPosition selector for 'z' field alignment,
    277      * corresponding to the {@link Calendar#ZONE_OFFSET} and
    278      * {@link Calendar#DST_OFFSET} fields.
    279      * @stable ICU 2.0
    280      */
    281     public final static int TIMEZONE_FIELD = 17;
    282 
    283     /**
    284      * {@icu} FieldPosition selector for 'Y' field alignment,
    285      * corresponding to the {@link Calendar#YEAR_WOY} field.
    286      * @stable ICU 3.0
    287      */
    288     public final static int YEAR_WOY_FIELD = 18;
    289 
    290     /**
    291      * {@icu} FieldPosition selector for 'e' field alignment,
    292      * corresponding to the {@link Calendar#DOW_LOCAL} field.
    293      * @stable ICU 3.0
    294      */
    295     public final static int DOW_LOCAL_FIELD = 19;
    296 
    297     /**
    298      * {@icu} FieldPosition selector for 'u' field alignment,
    299      * corresponding to the {@link Calendar#EXTENDED_YEAR} field.
    300      * @stable ICU 3.0
    301      */
    302     public final static int EXTENDED_YEAR_FIELD = 20;
    303 
    304     /**
    305      * {@icu} FieldPosition selector for 'g' field alignment,
    306      * corresponding to the {@link Calendar#JULIAN_DAY} field.
    307      * @stable ICU 3.0
    308      */
    309     public final static int JULIAN_DAY_FIELD = 21;
    310 
    311     /**
    312      * {@icu} FieldPosition selector for 'A' field alignment,
    313      * corresponding to the {@link Calendar#MILLISECONDS_IN_DAY} field.
    314      * @stable ICU 3.0
    315      */
    316     public final static int MILLISECONDS_IN_DAY_FIELD = 22;
    317 
    318     /**
    319      * {@icu} FieldPosition selector for 'Z' field alignment,
    320      * corresponding to the {@link Calendar#ZONE_OFFSET} and
    321      * {@link Calendar#DST_OFFSET} fields.
    322      * @stable ICU 3.0
    323      */
    324     public final static int TIMEZONE_RFC_FIELD = 23;
    325 
    326     /**
    327      * {@icu} FieldPosition selector for 'v' field alignment,
    328      * corresponding to the {@link Calendar#ZONE_OFFSET} and
    329      * {@link Calendar#DST_OFFSET} fields.  This displays the generic zone
    330      * name, if available.
    331      * @stable ICU 3.4
    332      */
    333     public final static int TIMEZONE_GENERIC_FIELD = 24;
    334 
    335     /**
    336      * {@icu} FieldPosition selector for 'c' field alignment,
    337      * corresponding to the {@link Calendar#DAY_OF_WEEK} field.
    338      * This displays the stand alone day name, if available.
    339      * @stable ICU 3.4
    340      */
    341     public final static int STANDALONE_DAY_FIELD = 25;
    342 
    343     /**
    344      * {@icu} FieldPosition selector for 'L' field alignment,
    345      * corresponding to the {@link Calendar#MONTH} field.
    346      * This displays the stand alone month name, if available.
    347      * @stable ICU 3.4
    348      */
    349     public final static int STANDALONE_MONTH_FIELD = 26;
    350 
    351     /**
    352      * {@icu} FieldPosition selector for 'Q' field alignment,
    353      * corresponding to the {@link Calendar#MONTH} field.
    354      * This displays the quarter.
    355      * @stable ICU 3.6
    356      */
    357     public final static int QUARTER_FIELD = 27;
    358 
    359     /**
    360      * {@icu} FieldPosition selector for 'q' field alignment,
    361      * corresponding to the {@link Calendar#MONTH} field.
    362      * This displays the stand alone quarter, if available.
    363      * @stable ICU 3.6
    364      */
    365     public final static int STANDALONE_QUARTER_FIELD = 28;
    366 
    367     /**
    368      * {@icu} FieldPosition selector for 'V' field alignment,
    369      * corresponding to the {@link Calendar#ZONE_OFFSET} and
    370      * {@link Calendar#DST_OFFSET} fields.  This displays the fallback timezone
    371      * name when VVVV is specified, and the short standard or daylight
    372      * timezone name ignoring commonlyUsed when a single V is specified.
    373      * @stable ICU 3.8
    374      */
    375     public final static int TIMEZONE_SPECIAL_FIELD = 29;
    376 
    377     /**
    378      * {@icu} Number of FieldPosition selectors for DateFormat.
    379      * Valid selectors range from 0 to FIELD_COUNT-1.
    380      * @stable ICU 3.0
    381      */
    382     public final static int FIELD_COUNT = 30; // must == DateFormatSymbols.patternChars.length()
    383 
    384     /**
    385      * Formats a time object into a time string. Examples of time objects
    386      * are a time value expressed in milliseconds and a Date object.
    387      * @param obj must be a Number or a Date or a Calendar.
    388      * @param toAppendTo the string buffer for the returning time string.
    389      * @return the formatted time string.
    390      * @param fieldPosition keeps track of the position of the field
    391      * within the returned string.
    392      * On input: an alignment field,
    393      * if desired. On output: the offsets of the alignment field. For
    394      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
    395      * if the given fieldPosition is DateFormat.YEAR_FIELD, the
    396      * begin index and end index of fieldPosition will be set to
    397      * 0 and 4, respectively.
    398      * Notice that if the same time field appears
    399      * more than once in a pattern, the fieldPosition will be set for the first
    400      * occurrence of that time field. For instance, formatting a Date to
    401      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
    402      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
    403      * the begin index and end index of fieldPosition will be set to
    404      * 5 and 8, respectively, for the first occurrence of the timezone
    405      * pattern character 'z'.
    406      * @see java.text.Format
    407      * @stable ICU 2.0
    408      */
    409     public final StringBuffer format(Object obj, StringBuffer toAppendTo,
    410                                      FieldPosition fieldPosition)
    411     {
    412         if (obj instanceof Calendar) {
    413             return format((Calendar)obj, toAppendTo, fieldPosition);
    414         } else if (obj instanceof Date) {
    415             return format((Date)obj, toAppendTo, fieldPosition);
    416         } else if (obj instanceof Number) {
    417             return format(new Date(((Number)obj).longValue()), toAppendTo, fieldPosition );
    418         }
    419 
    420         throw new IllegalArgumentException("Cannot format given Object (" +
    421                                                obj.getClass().getName() + ") as a Date");
    422     }
    423 
    424     /**
    425      * Formats a date into a date/time string.
    426      * @param cal a Calendar set to the date and time to be formatted
    427      * into a date/time string.  When the calendar type is different from
    428      * the internal calendar held by this DateFormat instance, the date
    429      * and the time zone will be inherited from the input calendar, but
    430      * other calendar field values will be calculated by the internal calendar.
    431      * @param toAppendTo the string buffer for the returning date/time string.
    432      * @param fieldPosition keeps track of the position of the field
    433      * within the returned string.
    434      * On input: an alignment field,
    435      * if desired. On output: the offsets of the alignment field. For
    436      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
    437      * if the given fieldPosition is DateFormat.YEAR_FIELD, the
    438      * begin index and end index of fieldPosition will be set to
    439      * 0 and 4, respectively.
    440      * Notice that if the same time field appears
    441      * more than once in a pattern, the fieldPosition will be set for the first
    442      * occurrence of that time field. For instance, formatting a Date to
    443      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
    444      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
    445      * the begin index and end index of fieldPosition will be set to
    446      * 5 and 8, respectively, for the first occurrence of the timezone
    447      * pattern character 'z'.
    448      * @return the formatted date/time string.
    449      * @stable ICU 2.0
    450      */
    451     public StringBuffer format(Calendar cal, StringBuffer toAppendTo,
    452                                         FieldPosition fieldPosition) {
    453         return format(cal.getTime(), toAppendTo, fieldPosition);
    454     }
    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         FieldPosition jdkPos = toJDKFieldPosition(fieldPosition);
    482         StringBuffer buf = dateFormat.format(date, toAppendTo, jdkPos);
    483         if (jdkPos != null) {
    484             fieldPosition.setBeginIndex(jdkPos.getBeginIndex());
    485             fieldPosition.setEndIndex(jdkPos.getEndIndex());
    486         }
    487         return buf;
    488     }
    489 
    490     /**
    491      * Formats a Date into a date/time string.
    492      * @param date the time value to be formatted into a time string.
    493      * @return the formatted time string.
    494      * @stable ICU 2.0
    495      */
    496     public final String format(Date date)
    497     {
    498         return dateFormat.format(date);
    499     }
    500 
    501     /**
    502      * Parses a date/time string.
    503      *
    504      * @param text  The date/time string to be parsed
    505      *
    506      * @return      A Date, or null if the input could not be parsed
    507      *
    508      * @exception  ParseException  If the given string cannot be parsed as a date.
    509      *
    510      * @see #parse(String, ParsePosition)
    511      * @stable ICU 2.0
    512      */
    513     public Date parse(String text) throws ParseException
    514     {
    515         return dateFormat.parse(text);
    516     }
    517 
    518     /**
    519      * Parses a date/time string according to the given parse position.
    520      * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed
    521      * into a Calendar that is equivalent to Date(837039928046).  The
    522      * caller should clear the calendar before calling this method,
    523      * unless existing field information is to be kept.
    524      *
    525      * <p> By default, parsing is lenient: If the input is not in the form used
    526      * by this object's format method but can still be parsed as a date, then
    527      * the parse succeeds.  Clients may insist on strict adherence to the
    528      * format by calling setLenient(false).
    529      *
    530      * @see #setLenient(boolean)
    531      *
    532      * @param text  The date/time string to be parsed
    533      *
    534      * @param cal   The calendar into which parsed data will be stored.
    535      *              In general, this should be cleared before calling this
    536      *              method.  If this parse fails, the calendar may still
    537      *              have been modified.  When the calendar type is different
    538      *              from the internal calendar held by this DateFormat
    539      *              instance, calendar field values will be parsed based
    540      *              on the internal calendar initialized with the time and
    541      *              the time zone taken from this calendar, then the
    542      *              parse result (time in milliseconds and time zone) will
    543      *              be set back to this calendar.
    544      *
    545      * @param pos   On input, the position at which to start parsing; on
    546      *              output, the position at which parsing terminated, or the
    547      *              start position if the parse failed.
    548      * @stable ICU 2.0
    549      */
    550     public void parse(String text, Calendar cal, ParsePosition pos) {
    551         Date result = dateFormat.parse(text, pos);
    552         cal.setTime(result);
    553     }
    554 
    555     /**
    556      * Parses a date/time string according to the given parse position.  For
    557      * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
    558      * that is equivalent to Date(837039928046).
    559      *
    560      * <p> By default, parsing is lenient: If the input is not in the form used
    561      * by this object's format method but can still be parsed as a date, then
    562      * the parse succeeds.  Clients may insist on strict adherence to the
    563      * format by calling setLenient(false).
    564      *
    565      * @see #setLenient(boolean)
    566      *
    567      * @param text  The date/time string to be parsed
    568      *
    569      * @param pos   On input, the position at which to start parsing; on
    570      *              output, the position at which parsing terminated, or the
    571      *              start position if the parse failed.
    572      *
    573      * @return      A Date, or null if the input could not be parsed
    574      * @stable ICU 2.0
    575      */
    576     public Date parse(String text, ParsePosition pos) {
    577         return dateFormat.parse(text, pos);
    578     }
    579 
    580     /**
    581      * Parses a date/time string into an Object.  This convenience method simply
    582      * calls parse(String, ParsePosition).
    583      *
    584      * @see #parse(String, ParsePosition)
    585      * @stable ICU 2.0
    586      */
    587     public Object parseObject (String source, ParsePosition pos)
    588     {
    589         return parse(source, pos);
    590     }
    591 
    592     /**
    593      * {@icu} Constant for empty style pattern.
    594      * @stable ICU 3.8
    595      */
    596     public static final int NONE = -1;
    597 
    598     /**
    599      * Constant for full style pattern.
    600      * @stable ICU 2.0
    601      */
    602     public static final int FULL = 0;
    603 
    604     /**
    605      * Constant for long style pattern.
    606      * @stable ICU 2.0
    607      */
    608     public static final int LONG = 1;
    609 
    610     /**
    611      * Constant for medium style pattern.
    612      * @stable ICU 2.0
    613      */
    614     public static final int MEDIUM = 2;
    615 
    616     /**
    617      * Constant for short style pattern.
    618      * @stable ICU 2.0
    619      */
    620     public static final int SHORT = 3;
    621 
    622     /**
    623      * Constant for default style pattern.  Its value is MEDIUM.
    624      * @stable ICU 2.0
    625      */
    626     public static final int DEFAULT = MEDIUM;
    627 
    628     /**
    629      * {@icu} Constant for relative style mask.
    630      * @stable ICU 3.8
    631      */
    632     public static final int RELATIVE = (1 << 7);
    633 
    634     /**
    635      * {@icu} Constant for relative full style pattern.
    636      * @stable ICU 3.8
    637      */
    638     public static final int RELATIVE_FULL = RELATIVE | FULL;
    639 
    640     /**
    641      * {@icu} Constant for relative style pattern.
    642      * @stable ICU 3.8
    643      */
    644     public static final int RELATIVE_LONG = RELATIVE | LONG;
    645 
    646     /**
    647      * {@icu} Constant for relative style pattern.
    648      * @stable ICU 3.8
    649      */
    650     public static final int RELATIVE_MEDIUM = RELATIVE | MEDIUM;
    651 
    652     /**
    653      * {@icu} Constant for relative style pattern.
    654      * @stable ICU 3.8
    655      */
    656     public static final int RELATIVE_SHORT = RELATIVE | SHORT;
    657 
    658     /**
    659      * {@icu} Constant for relative default style pattern.
    660      * @stable ICU 3.8
    661      */
    662     public static final int RELATIVE_DEFAULT = RELATIVE | DEFAULT;
    663 
    664     /*
    665      * DATES
    666      */
    667 
    668     /**
    669      * {@icu} Constant for date skeleton with year.
    670      * @stable ICU 4.0
    671      */
    672     public static final String YEAR = "y";
    673 
    674     /**
    675      * {@icu} Constant for date skeleton with quarter.
    676      * @draft ICU 50
    677      * @provisional This API might change or be removed in a future release.
    678      */
    679     public static final String QUARTER = "QQQQ";
    680 
    681     /**
    682      * {@icu} Constant for date skeleton with abbreviated quarter.
    683      * @draft ICU 50
    684      * @provisional This API might change or be removed in a future release.
    685      */
    686     public static final String ABBR_QUARTER = "QQQ";
    687 
    688     /**
    689      * {@icu} Constant for date skeleton with year and quarter.
    690      * @stable ICU 4.0
    691      */
    692     public static final String YEAR_QUARTER = "yQQQQ";
    693 
    694     /**
    695      * {@icu} Constant for date skeleton with year and abbreviated quarter.
    696      * @stable ICU 4.0
    697      */
    698     public static final String YEAR_ABBR_QUARTER = "yQQQ";
    699 
    700     /**
    701      * {@icu} Constant for date skeleton with month.
    702      * @stable ICU 4.0
    703      */
    704     public static final String MONTH = "MMMM";
    705 
    706     /**
    707      * {@icu} Constant for date skeleton with abbreviated month.
    708      * @stable ICU 4.0
    709      */
    710     public static final String ABBR_MONTH = "MMM";
    711 
    712     /**
    713      * {@icu} Constant for date skeleton with numeric month.
    714      * @stable ICU 4.0
    715      */
    716     public static final String NUM_MONTH = "M";
    717 
    718     /**
    719      * {@icu} Constant for date skeleton with year and month.
    720      * @stable ICU 4.0
    721      */
    722     public static final String YEAR_MONTH = "yMMMM";
    723 
    724     /**
    725      * {@icu} Constant for date skeleton with year and abbreviated month.
    726      * @stable ICU 4.0
    727      */
    728     public static final String YEAR_ABBR_MONTH = "yMMM";
    729 
    730     /**
    731      * {@icu} Constant for date skeleton with year and numeric month.
    732      * @stable ICU 4.0
    733      */
    734     public static final String YEAR_NUM_MONTH = "yM";
    735 
    736     /**
    737      * {@icu} Constant for date skeleton with day.
    738      * @stable ICU 4.0
    739      */
    740     public static final String DAY = "d";
    741 
    742     /**
    743      * {@icu} Constant for date skeleton with year, month, and day.
    744      * Used in combinations date + time, date + time + zone, or time + zone.
    745      * @stable ICU 4.0
    746      */
    747     public static final String YEAR_MONTH_DAY = "yMMMMd";
    748 
    749     /**
    750      * {@icu} Constant for date skeleton with year, abbreviated month, and day.
    751      * Used in combinations date + time, date + time + zone, or time + zone.
    752      * @stable ICU 4.0
    753      */
    754     public static final String YEAR_ABBR_MONTH_DAY = "yMMMd";
    755 
    756     /**
    757      * {@icu} Constant for date skeleton with year, numeric month, and day.
    758      * Used in combinations date + time, date + time + zone, or time + zone.
    759      * @stable ICU 4.0
    760      */
    761     public static final String YEAR_NUM_MONTH_DAY = "yMd";
    762 
    763     /**
    764      * {@icu} Constant for date skeleton with weekday.
    765      * @draft ICU 50
    766      * @provisional This API might change or be removed in a future release.
    767      */
    768     public static final String WEEKDAY = "EEEE";
    769 
    770     /**
    771      * {@icu} Constant for date skeleton with abbreviated weekday.
    772      * @draft ICU 50
    773      * @provisional This API might change or be removed in a future release.
    774      */
    775     public static final String ABBR_WEEKDAY = "E";
    776 
    777     /**
    778      * {@icu} Constant for date skeleton with year, month, weekday, and day.
    779      * Used in combinations date + time, date + time + zone, or time + zone.
    780      * @stable ICU 4.0
    781      */
    782     public static final String YEAR_MONTH_WEEKDAY_DAY = "yMMMMEEEEd";
    783 
    784     /**
    785      * {@icu} Constant for date skeleton with year, abbreviated month, weekday, and day.
    786      * Used in combinations date + time, date + time + zone, or time + zone.
    787      * @stable ICU 4.0
    788      */
    789     public static final String YEAR_ABBR_MONTH_WEEKDAY_DAY = "yMMMEd";
    790 
    791     /**
    792      * {@icu} Constant for date skeleton with year, numeric month, weekday, and day.
    793      * Used in combinations date + time, date + time + zone, or time + zone.
    794      * @stable ICU 4.0
    795      */
    796     public static final String YEAR_NUM_MONTH_WEEKDAY_DAY = "yMEd";
    797 
    798     /**
    799      * {@icu} Constant for date skeleton with long month and day.
    800      * Used in combinations date + time, date + time + zone, or time + zone.
    801      * @stable ICU 4.0
    802      */
    803     public static final String MONTH_DAY = "MMMMd";
    804 
    805     /**
    806      * {@icu} Constant for date skeleton with abbreviated month and day.
    807      * Used in combinations date + time, date + time + zone, or time + zone.
    808      * @stable ICU 4.0
    809      */
    810     public static final String ABBR_MONTH_DAY = "MMMd";
    811 
    812     /**
    813      * {@icu} Constant for date skeleton with numeric month and day.
    814      * Used in combinations date + time, date + time + zone, or time + zone.
    815      * @stable ICU 4.0
    816      */
    817     public static final String NUM_MONTH_DAY = "Md";
    818 
    819     /**
    820      * {@icu} Constant for date skeleton with month, weekday, and day.
    821      * Used in combinations date + time, date + time + zone, or time + zone.
    822      * @stable ICU 4.0
    823      */
    824     public static final String MONTH_WEEKDAY_DAY = "MMMMEEEEd";
    825 
    826     /**
    827      * {@icu} Constant for date skeleton with abbreviated month, weekday, and day.
    828      * Used in combinations date + time, date + time + zone, or time + zone.
    829      * @stable ICU 4.0
    830      */
    831     public static final String ABBR_MONTH_WEEKDAY_DAY = "MMMEd";
    832 
    833     /**
    834      * {@icu} Constant for date skeleton with numeric month, weekday, and day.
    835      * Used in combinations date + time, date + time + zone, or time + zone.
    836      * @stable ICU 4.0
    837      */
    838     public static final String NUM_MONTH_WEEKDAY_DAY = "MEd";
    839 
    840     /*
    841      * TIMES
    842      */
    843 
    844     /**
    845      * {@icu} Constant for date skeleton with hour, with the locale's preferred hour format (12 or 24).
    846      * @stable ICU 4.0
    847      */
    848     public static final String HOUR = "j";
    849 
    850     /**
    851      * {@icu} Constant for date skeleton with hour in 24-hour presentation.
    852      * @draft ICU 50
    853      * @provisional This API might change or be removed in a future release.
    854      */
    855     public static final String HOUR24 = "H";
    856 
    857     /**
    858      * {@icu} Constant for date skeleton with minute.
    859      * @draft ICU 50
    860      * @provisional This API might change or be removed in a future release.
    861      */
    862     public static final String MINUTE = "m";
    863 
    864     /**
    865      * {@icu} Constant for date skeleton with hour and minute, with the locale's preferred hour format (12 or 24).
    866      * Used in combinations date + time, date + time + zone, or time + zone.
    867      * @stable ICU 4.0
    868      */
    869     public static final String HOUR_MINUTE = "jm";
    870 
    871     /**
    872      * {@icu} Constant for date skeleton with hour and minute in 24-hour presentation.
    873      * Used in combinations date + time, date + time + zone, or time + zone.
    874      * @stable ICU 4.0
    875      */
    876     public static final String HOUR24_MINUTE = "Hm";
    877 
    878     /**
    879      * {@icu} Constant for date skeleton with second.
    880      * @draft ICU 50
    881      * @provisional This API might change or be removed in a future release.
    882      */
    883     public static final String SECOND = "s";
    884 
    885     /**
    886      * {@icu} Constant for date skeleton with hour, minute, and second,
    887      * with the locale's preferred hour format (12 or 24).
    888      * Used in combinations date + time, date + time + zone, or time + zone.
    889      * @stable ICU 4.0
    890      */
    891     public static final String HOUR_MINUTE_SECOND = "jms";
    892 
    893     /**
    894      * {@icu} Constant for date skeleton with hour, minute, and second in
    895      * 24-hour presentation.
    896      * Used in combinations date + time, date + time + zone, or time + zone.
    897      * @stable ICU 4.0
    898      */
    899     public static final String HOUR24_MINUTE_SECOND = "Hms";
    900 
    901     /**
    902      * {@icu} Constant for date skeleton with minute and second.
    903      * Used in combinations date + time, date + time + zone, or time + zone.
    904      * @stable ICU 4.0
    905      */
    906     public static final String MINUTE_SECOND = "ms";
    907 
    908     /*
    909      * TIMEZONES
    910      */
    911 
    912     /**
    913      * {@icu} Constant for <i>generic location format</i>, such as Los Angeles Time;
    914      * used in combinations date + time + zone, or time + zone.
    915      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    916      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    917      * @draft ICU 50
    918      * @provisional This API might change or be removed in a future release.
    919      */
    920     public static final String LOCATION_TZ = "VVVV";
    921 
    922     /**
    923      * {@icu} Constant for <i>generic non-location format</i>, such as Pacific Time;
    924      * used in combinations date + time + zone, or time + zone.
    925      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    926      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    927      * @draft ICU 50
    928      * @provisional This API might change or be removed in a future release.
    929      */
    930     public static final String GENERIC_TZ = "vvvv";
    931 
    932     /**
    933      * {@icu} Constant for <i>generic non-location format</i>, abbreviated if possible, such as PT;
    934      * used in combinations date + time + zone, or time + zone.
    935      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    936      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    937      * @draft ICU 50
    938      * @provisional This API might change or be removed in a future release.
    939      */
    940     public static final String ABBR_GENERIC_TZ = "v";
    941 
    942     /**
    943      * {@icu} Constant for <i>specific non-location format</i>, such as Pacific Daylight Time;
    944      * used in combinations date + time + zone, or time + zone.
    945      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    946      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    947      * @draft ICU 50
    948      * @provisional This API might change or be removed in a future release.
    949      */
    950     public static final String SPECIFIC_TZ = "zzzz";
    951 
    952     /**
    953      * {@icu} Constant for <i>specific non-location format</i>, abbreviated if possible, such as PDT;
    954      * used in combinations date + time + zone, or time + zone.
    955      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    956      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    957      * @draft ICU 50
    958      * @provisional This API might change or be removed in a future release.
    959      */
    960     public static final String ABBR_SPECIFIC_TZ = "z";
    961 
    962     /**
    963      * {@icu} Constant for <i>localized GMT/UTC format</i>, such as GMT+8:00 or HPG-8:00;
    964      * used in combinations date + time + zone, or time + zone.
    965      * @see <a href="http://unicode.org/reports/tr35/#Date_Format_Patterns">LDML Date Format Patterns</a>
    966      * @see <a href="http://unicode.org/reports/tr35/#Time_Zone_Fallback">LDML Time Zone Fallback</a>
    967      * @draft ICU 50
    968      * @provisional This API might change or be removed in a future release.
    969      */
    970     public static final String ABBR_UTC_TZ = "ZZZZ";
    971 
    972     /*
    973      * deprecated skeleton constants
    974      */
    975 
    976     /**
    977      * {@icu} Constant for date skeleton with standalone month.
    978      * @deprecated ICU 50 Use {@link #MONTH} instead.
    979      */
    980     public static final String STANDALONE_MONTH = "LLLL";
    981 
    982     /**
    983      * {@icu} Constant for date skeleton with standalone abbreviated month.
    984      * @deprecated ICU 50 Use {@link #ABBR_MONTH} instead.
    985      */
    986     public static final String ABBR_STANDALONE_MONTH = "LLL";
    987 
    988     /**
    989      * {@icu} Constant for date skeleton with hour, minute, and generic timezone.
    990      * @deprecated ICU 50 Use instead {@link #HOUR_MINUTE}+{@link #ABBR_GENERIC_TZ} or some other timezone presentation.
    991      */
    992     public static final String HOUR_MINUTE_GENERIC_TZ = "jmv";
    993 
    994     /**
    995      * {@icu} Constant for date skeleton with hour, minute, and timezone.
    996      * @deprecated ICU 50 Use instead {@link #HOUR_MINUTE}+{@link #ABBR_SPECIFIC_TZ} or some other timezone presentation.
    997      */
    998     public static final String HOUR_MINUTE_TZ = "jmz";
    999 
   1000     /**
   1001      * {@icu} Constant for date skeleton with hour and generic timezone.
   1002      * @deprecated ICU 50 Use instead {@link #HOUR}+{@link #ABBR_GENERIC_TZ} or some other timezone presentation.
   1003      */
   1004     public static final String HOUR_GENERIC_TZ = "jv";
   1005 
   1006     /**
   1007      * {@icu} Constant for date skeleton with hour and timezone.
   1008      * @deprecated ICU 50 Use instead {@link #HOUR}+{@link #ABBR_SPECIFIC_TZ} or some other timezone presentation.
   1009      */
   1010     public static final String HOUR_TZ = "jz";
   1011     /**
   1012      * Gets the time formatter with the default formatting style
   1013      * for the default locale.
   1014      * @return a time formatter.
   1015      * @stable ICU 2.0
   1016      */
   1017     public final static DateFormat getTimeInstance()
   1018     {
   1019         return new DateFormat(java.text.DateFormat.getTimeInstance(
   1020                 java.text.DateFormat.DEFAULT,
   1021                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1022     }
   1023 
   1024     /**
   1025      * Returns the time formatter with the given formatting style
   1026      * for the default locale.
   1027      * @param style the given formatting style. For example,
   1028      * SHORT for "h:mm a" in the US locale.
   1029      * @return a time formatter.
   1030      * @stable ICU 2.0
   1031      */
   1032     public final static DateFormat getTimeInstance(int style)
   1033     {
   1034         return new DateFormat(java.text.DateFormat.getTimeInstance(
   1035                 getJDKFormatStyle(style),
   1036                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1037     }
   1038 
   1039     /**
   1040      * Returns the time formatter with the given formatting style
   1041      * for the given locale.
   1042      * @param style the given formatting style. For example,
   1043      * SHORT for "h:mm a" in the US locale.
   1044      * @param aLocale the given locale.
   1045      * @return a time formatter.
   1046      * @stable ICU 2.0
   1047      */
   1048     public final static DateFormat getTimeInstance(int style,
   1049                                                  Locale aLocale)
   1050     {
   1051         return new DateFormat(java.text.DateFormat.getTimeInstance(getJDKFormatStyle(style), aLocale));
   1052     }
   1053 
   1054     /**
   1055      * Returns the time formatter with the given formatting style
   1056      * for the given locale.
   1057      * @param style the given formatting style. For example,
   1058      * SHORT for "h:mm a" in the US locale.
   1059      * @param locale the given ulocale.
   1060      * @return a time formatter.
   1061      * @stable ICU 3.2
   1062      */
   1063     public final static DateFormat getTimeInstance(int style,
   1064                                                  ULocale locale)
   1065     {
   1066         return new DateFormat(java.text.DateFormat.getTimeInstance(getJDKFormatStyle(style), locale.toLocale()));
   1067     }
   1068 
   1069     /**
   1070      * Returns the date formatter with the default formatting style
   1071      * for the default locale.
   1072      * @return a date formatter.
   1073      * @stable ICU 2.0
   1074      */
   1075     public final static DateFormat getDateInstance()
   1076     {
   1077         return new DateFormat(java.text.DateFormat.getDateInstance(
   1078                 java.text.DateFormat.DEFAULT,
   1079                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1080     }
   1081 
   1082     /**
   1083      * Returns the date formatter with the given formatting style
   1084      * for the default locale.
   1085      * @param style the given formatting style. For example,
   1086      * SHORT for "M/d/yy" in the US locale.
   1087      * @return a date formatter.
   1088      * @stable ICU 2.0
   1089      */
   1090     public final static DateFormat getDateInstance(int style)
   1091     {
   1092         return new DateFormat(java.text.DateFormat.getDateInstance(
   1093                 getJDKFormatStyle(style),
   1094                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1095     }
   1096 
   1097     /**
   1098      * Returns the date formatter with the given formatting style
   1099      * for the given locale.
   1100      * @param style the given formatting style. For example,
   1101      * SHORT for "M/d/yy" in the US locale.
   1102      * @param aLocale the given locale.
   1103      * @return a date formatter.
   1104      * @stable ICU 2.0
   1105      */
   1106     public final static DateFormat getDateInstance(int style,
   1107                                                  Locale aLocale)
   1108     {
   1109         return new DateFormat(java.text.DateFormat.getDateInstance(getJDKFormatStyle(style), aLocale));
   1110     }
   1111 
   1112     /**
   1113      * Returns the date formatter with the given formatting style
   1114      * for the given locale.
   1115      * @param style the given formatting style. For example,
   1116      * SHORT for "M/d/yy" in the US locale.
   1117      * @param locale the given ulocale.
   1118      * @return a date formatter.
   1119      * @stable ICU 3.2
   1120      */
   1121     public final static DateFormat getDateInstance(int style,
   1122                                                  ULocale locale)
   1123     {
   1124         return new DateFormat(java.text.DateFormat.getDateInstance(getJDKFormatStyle(style), locale.toLocale()));
   1125     }
   1126 
   1127     /**
   1128      * Returns the date/time formatter with the default formatting style
   1129      * for the default locale.
   1130      * @return a date/time formatter.
   1131      * @stable ICU 2.0
   1132      */
   1133     public final static DateFormat getDateTimeInstance()
   1134     {
   1135         return new DateFormat(java.text.DateFormat.getDateTimeInstance(
   1136                 java.text.DateFormat.DEFAULT,
   1137                 java.text.DateFormat.DEFAULT,
   1138                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1139     }
   1140 
   1141     /**
   1142      * Returns the date/time formatter with the given date and time
   1143      * formatting styles for the default locale.
   1144      * @param dateStyle the given date formatting style. For example,
   1145      * SHORT for "M/d/yy" in the US locale.
   1146      * @param timeStyle the given time formatting style. For example,
   1147      * SHORT for "h:mm a" in the US locale.
   1148      * @return a date/time formatter.
   1149      * @stable ICU 2.0
   1150      */
   1151     public final static DateFormat getDateTimeInstance(int dateStyle,
   1152                                                        int timeStyle)
   1153     {
   1154         if (dateStyle != NONE) {
   1155             if (timeStyle != NONE) {
   1156                 return new DateFormat(java.text.DateFormat.getDateTimeInstance(
   1157                         getJDKFormatStyle(dateStyle),
   1158                         getJDKFormatStyle(timeStyle),
   1159                         ULocale.getDefault(Category.FORMAT).toLocale()));
   1160             } else {
   1161                 return new DateFormat(java.text.DateFormat.getDateInstance(
   1162                         getJDKFormatStyle(dateStyle),
   1163                         ULocale.getDefault(Category.FORMAT).toLocale()));
   1164             }
   1165         }
   1166         if (timeStyle != NONE) {
   1167             return new DateFormat(java.text.DateFormat.getTimeInstance(
   1168                     getJDKFormatStyle(timeStyle),
   1169                     ULocale.getDefault(Category.FORMAT).toLocale()));
   1170         }
   1171         return null;
   1172     }
   1173 
   1174     /**
   1175      * Returns the date/time formatter with the given formatting styles
   1176      * for the given locale.
   1177      * @param dateStyle the given date formatting style.
   1178      * @param timeStyle the given time formatting style.
   1179      * @param aLocale the given locale.
   1180      * @return a date/time formatter.
   1181      * @stable ICU 2.0
   1182      */
   1183     public final static DateFormat getDateTimeInstance(
   1184         int dateStyle, int timeStyle, Locale aLocale)
   1185     {
   1186         if (dateStyle != NONE) {
   1187             if (timeStyle != NONE) {
   1188                 return new DateFormat(java.text.DateFormat.getDateTimeInstance(getJDKFormatStyle(dateStyle), getJDKFormatStyle(timeStyle), aLocale));
   1189             } else {
   1190                 return new DateFormat(java.text.DateFormat.getDateInstance(getJDKFormatStyle(dateStyle), aLocale));
   1191             }
   1192         }
   1193         if (timeStyle != NONE) {
   1194             return new DateFormat(java.text.DateFormat.getTimeInstance(getJDKFormatStyle(timeStyle), aLocale));
   1195         }
   1196         return null;
   1197     }
   1198 
   1199     /**
   1200      * Returns the date/time formatter with the given formatting styles
   1201      * for the given locale.
   1202      * @param dateStyle the given date formatting style.
   1203      * @param timeStyle the given time formatting style.
   1204      * @param locale the given ulocale.
   1205      * @return a date/time formatter.
   1206      * @stable ICU 3.2
   1207      */
   1208     public final static DateFormat getDateTimeInstance(
   1209         int dateStyle, int timeStyle, ULocale locale)
   1210     {
   1211         if (dateStyle != NONE) {
   1212             if (timeStyle != NONE) {
   1213                 return new DateFormat(java.text.DateFormat.getDateTimeInstance(getJDKFormatStyle(dateStyle), getJDKFormatStyle(timeStyle), locale.toLocale()));
   1214             } else {
   1215                 return new DateFormat(java.text.DateFormat.getDateInstance(getJDKFormatStyle(dateStyle), locale.toLocale()));
   1216             }
   1217         }
   1218         if (timeStyle != NONE) {
   1219             return new DateFormat(java.text.DateFormat.getTimeInstance(getJDKFormatStyle(timeStyle), locale.toLocale()));
   1220         }
   1221         return null;
   1222     }
   1223 
   1224     /**
   1225      * Returns a default date/time formatter that uses the SHORT style for both the
   1226      * date and the time.
   1227      * @stable ICU 2.0
   1228      */
   1229     public final static DateFormat getInstance() {
   1230         return new DateFormat(java.text.DateFormat.getDateTimeInstance(
   1231                 java.text.DateFormat.SHORT,
   1232                 java.text.DateFormat.SHORT,
   1233                 ULocale.getDefault(Category.FORMAT).toLocale()));
   1234     }
   1235 
   1236     /**
   1237      * Returns the set of locales for which DateFormats are installed.
   1238      * @return the set of locales for which DateFormats are installed.
   1239      * @stable ICU 2.0
   1240      */
   1241     public static Locale[] getAvailableLocales()
   1242     {
   1243         return java.text.DateFormat.getAvailableLocales();
   1244     }
   1245 
   1246     /**
   1247      * {@icu} Returns the set of locales for which DateFormats are installed.
   1248      * @return the set of locales for which DateFormats are installed.
   1249      * @draft ICU 3.2 (retain)
   1250      * @provisional This API might change or be removed in a future release.
   1251      */
   1252     public static ULocale[] getAvailableULocales()
   1253     {
   1254         if (availableULocales == null) {
   1255             synchronized(DateFormat.class) {
   1256                 if (availableULocales == null) {
   1257                     Locale[] locales = java.text.DateFormat.getAvailableLocales();
   1258                     availableULocales = new ULocale[locales.length];
   1259                     for (int i = 0; i < locales.length; ++i) {
   1260                         availableULocales[i] = ULocale.forLocale(locales[i]);
   1261                     }
   1262                 }
   1263             }
   1264         }
   1265         return availableULocales;
   1266     }
   1267     private static volatile ULocale[] availableULocales;
   1268 
   1269     /**
   1270      * Sets the calendar to be used by this date format.  Initially, the default
   1271      * calendar for the specified or default locale is used.
   1272      * @param newCalendar the new Calendar to be used by the date format
   1273      * @stable ICU 2.0
   1274      */
   1275     public void setCalendar(Calendar newCalendar)
   1276     {
   1277         dateFormat.setCalendar(newCalendar.calendar);
   1278     }
   1279 
   1280     /**
   1281      * Returns the calendar associated with this date/time formatter.
   1282      * @return the calendar associated with this date/time formatter.
   1283      * @stable ICU 2.0
   1284      */
   1285     public Calendar getCalendar()
   1286     {
   1287         return new Calendar(dateFormat.getCalendar());
   1288     }
   1289 
   1290     /**
   1291      * Sets the number formatter.
   1292      * @param newNumberFormat the given new NumberFormat.
   1293      * @stable ICU 2.0
   1294      */
   1295     public void setNumberFormat(NumberFormat newNumberFormat)
   1296     {
   1297         dateFormat.setNumberFormat(newNumberFormat.numberFormat);
   1298     }
   1299 
   1300     /**
   1301      * Returns the number formatter which this date/time formatter uses to
   1302      * format and parse a time.
   1303      * @return the number formatter which this date/time formatter uses.
   1304      * @stable ICU 2.0
   1305      */
   1306     public NumberFormat getNumberFormat()
   1307     {
   1308         return new NumberFormat(dateFormat.getNumberFormat());
   1309     }
   1310 
   1311     /**
   1312      * Sets the time zone for the calendar of this DateFormat object.
   1313      * @param zone the given new time zone.
   1314      * @stable ICU 2.0
   1315      */
   1316     public void setTimeZone(TimeZone zone)
   1317     {
   1318         dateFormat.setTimeZone(zone.timeZone);
   1319     }
   1320 
   1321     /**
   1322      * Returns the time zone.
   1323      * @return the time zone associated with the calendar of DateFormat.
   1324      * @stable ICU 2.0
   1325      */
   1326     public TimeZone getTimeZone()
   1327     {
   1328         return new TimeZone(dateFormat.getTimeZone());
   1329     }
   1330 
   1331     /**
   1332      * Specifies whether date/time parsing is to be lenient.  With
   1333      * lenient parsing, the parser may use heuristics to interpret inputs that
   1334      * do not precisely match this object's format.  With strict parsing,
   1335      * inputs must match this object's format.
   1336      * @param lenient when true, parsing is lenient
   1337      * @see com.ibm.icu.util.Calendar#setLenient
   1338      * @stable ICU 2.0
   1339      */
   1340     public void setLenient(boolean lenient)
   1341     {
   1342         dateFormat.setLenient(lenient);
   1343     }
   1344 
   1345     /**
   1346      * Returns whether date/time parsing is lenient.
   1347      * @stable ICU 2.0
   1348      */
   1349     public boolean isLenient()
   1350     {
   1351         return dateFormat.isLenient();
   1352     }
   1353 
   1354     /**
   1355      * Overrides hashCode.
   1356      * @stable ICU 2.0
   1357      */
   1358     public int hashCode() {
   1359         return dateFormat.hashCode();
   1360     }
   1361 
   1362     /**
   1363      * Overrides equals.
   1364      * @stable ICU 2.0
   1365      */
   1366     public boolean equals(Object obj) {
   1367         try {
   1368             return dateFormat.equals(((DateFormat)obj).dateFormat);
   1369         }
   1370         catch (Exception e) {
   1371             return false;
   1372         }
   1373     }
   1374 
   1375     /**
   1376      * Overrides clone.
   1377      * @stable ICU 2.0
   1378      */
   1379     public Object clone()
   1380     {
   1381         return new DateFormat((java.text.DateFormat)dateFormat.clone());
   1382     }
   1383 
   1384     //-------------------------------------------------------------------------
   1385     // Public static interface for creating custon DateFormats for different
   1386     // types of Calendars.
   1387     //-------------------------------------------------------------------------
   1388 
   1389     /**
   1390      * Creates a {@link DateFormat} object that can be used to format dates in
   1391      * the calendar system specified by <code>cal</code>.
   1392      * <p>
   1393      * @param cal   The calendar system for which a date format is desired.
   1394      *
   1395      * @param dateStyle The type of date format desired.  This can be
   1396      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1397      *              etc.
   1398      *
   1399      * @param locale The locale for which the date format is desired.
   1400      * @stable ICU 2.0
   1401      */
   1402     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale)
   1403     {
   1404         DateFormat df = getDateInstance(dateStyle, locale);
   1405         df.setCalendar(cal);
   1406         return df;
   1407     }
   1408 
   1409     /**
   1410      * Creates a {@link DateFormat} object that can be used to format dates in
   1411      * the calendar system specified by <code>cal</code>.
   1412      * <p>
   1413      * @param cal   The calendar system for which a date format is desired.
   1414      *
   1415      * @param dateStyle The type of date format desired.  This can be
   1416      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1417      *              etc.
   1418      *
   1419      * @param locale The locale for which the date format is desired.
   1420      * @stable ICU 3.2
   1421      */
   1422     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale)
   1423     {
   1424         DateFormat df = getDateInstance(dateStyle, locale);
   1425         df.setCalendar(cal);
   1426         return df;
   1427     }
   1428 
   1429     /**
   1430      * Creates a {@link DateFormat} object that can be used to format times in
   1431      * the calendar system specified by <code>cal</code>.
   1432      * <p>
   1433      * <b>Note:</b> When this functionality is moved into the core JDK, this method
   1434      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1435      * <p>
   1436      * @param cal   The calendar system for which a time format is desired.
   1437      *
   1438      * @param timeStyle The type of time format desired.  This can be
   1439      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1440      *              etc.
   1441      *
   1442      * @param locale The locale for which the time format is desired.
   1443      *
   1444      * @see DateFormat#getTimeInstance
   1445      * @stable ICU 2.0
   1446      */
   1447     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale)
   1448     {
   1449         DateFormat df = getTimeInstance(timeStyle, locale);
   1450         df.setCalendar(cal);
   1451         return df;
   1452     }
   1453 
   1454     /**
   1455      * Creates a {@link DateFormat} object that can be used to format times in
   1456      * the calendar system specified by <code>cal</code>.
   1457      * <p>
   1458      * <b>Note:</b> When this functionality is moved into the core JDK, this method
   1459      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1460      * <p>
   1461      * @param cal   The calendar system for which a time format is desired.
   1462      *
   1463      * @param timeStyle The type of time format desired.  This can be
   1464      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1465      *              etc.
   1466      *
   1467      * @param locale The locale for which the time format is desired.
   1468      *
   1469      * @see DateFormat#getTimeInstance
   1470      * @stable ICU 3.2
   1471      */
   1472     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale)
   1473     {
   1474         DateFormat df = getTimeInstance(timeStyle, locale);
   1475         df.setCalendar(cal);
   1476         return df;
   1477     }
   1478 
   1479     /**
   1480      * Creates a {@link DateFormat} object that can be used to format dates and times in
   1481      * the calendar system specified by <code>cal</code>.
   1482      * <p>
   1483      * <b>Note:</b> When this functionality is moved into the core JDK, this method
   1484      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1485      * <p>
   1486      * @param cal   The calendar system for which a date/time format is desired.
   1487      *
   1488      * @param dateStyle The type of date format desired.  This can be
   1489      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1490      *              etc.
   1491      *
   1492      * @param timeStyle The type of time format desired.  This can be
   1493      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1494      *              etc.
   1495      *
   1496      * @param locale The locale for which the date/time format is desired.
   1497      *
   1498      * @see DateFormat#getDateTimeInstance
   1499      * @stable ICU 2.0
   1500      */
   1501     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
   1502                                                  int timeStyle, Locale locale)
   1503     {
   1504         DateFormat df = getDateTimeInstance(dateStyle, timeStyle, locale);
   1505         df.setCalendar(cal);
   1506         return df;
   1507     }
   1508 
   1509     /**
   1510      * Creates a {@link DateFormat} object that can be used to format dates and times in
   1511      * the calendar system specified by <code>cal</code>.
   1512      * <p>
   1513      * <b>Note:</b> When this functionality is moved into the core JDK, this method
   1514      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1515      * <p>
   1516      * @param cal   The calendar system for which a date/time format is desired.
   1517      *
   1518      * @param dateStyle The type of date format desired.  This can be
   1519      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1520      *              etc.
   1521      *
   1522      * @param timeStyle The type of time format desired.  This can be
   1523      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},
   1524      *              etc.
   1525      *
   1526      * @param locale The locale for which the date/time format is desired.
   1527      *
   1528      * @see DateFormat#getDateTimeInstance
   1529      * @stable ICU 3.2
   1530      */
   1531     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,
   1532                                                  int timeStyle, ULocale locale)
   1533     {
   1534         DateFormat df = getDateTimeInstance(dateStyle, timeStyle, locale);
   1535         df.setCalendar(cal);
   1536         return df;
   1537     }
   1538 
   1539     /**
   1540      * Convenience overload.
   1541      * @stable ICU 2.0
   1542      */
   1543     static final public DateFormat getInstance(Calendar cal, Locale locale) {
   1544         return getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.SHORT, locale);
   1545     }
   1546 
   1547     /**
   1548      * Convenience overload.
   1549      * @stable ICU 3.2
   1550      * @provisional This API might change or be removed in a future release.
   1551      */
   1552     static final public DateFormat getInstance(Calendar cal, ULocale locale) {
   1553         return getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.SHORT, locale);
   1554     }
   1555 
   1556     /**
   1557      * Convenience overload.
   1558      * @stable ICU 2.0
   1559      */
   1560     static final public DateFormat getInstance(Calendar cal) {
   1561         return getInstance(cal, ULocale.getDefault(Category.FORMAT));
   1562     }
   1563 
   1564     /**
   1565      * Convenience overload.
   1566      * @stable ICU 2.0
   1567      */
   1568     static final public DateFormat getDateInstance(Calendar cal, int dateStyle) {
   1569         return getDateInstance(cal, dateStyle, ULocale.getDefault(Category.FORMAT));
   1570     }
   1571 
   1572     /**
   1573      * Convenience overload.
   1574      * @stable ICU 2.0
   1575      */
   1576     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) {
   1577         return getTimeInstance(cal, timeStyle, ULocale.getDefault(Category.FORMAT));
   1578     }
   1579 
   1580     /**
   1581      * Convenience overload.
   1582      * @stable ICU 2.0
   1583      */
   1584     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) {
   1585         return getDateTimeInstance(cal, dateStyle, timeStyle, ULocale.getDefault(Category.FORMAT));
   1586     }
   1587 
   1588 //    /**
   1589 //     * {@icu} Convenience overload.
   1590 //     * @stable ICU 4.0
   1591 //     */
   1592 //    public final static DateFormat getPatternInstance(String pattern) {
   1593 //        throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base");
   1594 //    }
   1595 
   1596 //    /**
   1597 //     * {@icu} Convenience overload.
   1598 //     * @stable ICU 4.0
   1599 //     */
   1600 //    public final static DateFormat getPatternInstance(String pattern, Locale locale) {
   1601 //        throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base");
   1602 //    }
   1603 
   1604 //    /**
   1605 //     * {@icu} Returns a {@link DateFormat} object that can be used to format dates and times in
   1606 //     * the given locale.
   1607 //     * <p>
   1608 //     * <b>Note:</b> When this functionality is moved into the core JDK, this method
   1609 //     * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1610 //     * <p>
   1611 //     *
   1612 //     * @param pattern The pattern that selects the fields to be formatted. (Uses the
   1613 //     *              {@link DateTimePatternGenerator}.) This can be {@link DateFormat#ABBR_MONTH},
   1614 //     *              {@link DateFormat#MONTH_WEEKDAY_DAY}, etc.
   1615 //     *
   1616 //     * @param locale The locale for which the date/time format is desired.
   1617 //     *
   1618 //     * @stable ICU 4.0
   1619 //     */
   1620 //    public final static DateFormat getPatternInstance(String pattern, ULocale locale) {
   1621 //        throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base");
   1622 //    }
   1623 
   1624 //    /**
   1625 //     * {@icu} Convenience overload.
   1626 //     * @stable ICU 4.0
   1627 //     */
   1628 //    public final static DateFormat getPatternInstance(Calendar cal, String pattern, Locale locale) {
   1629 //        throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base");
   1630 //    }
   1631 
   1632 //    /**
   1633 //     * {@icu} Creates a {@link DateFormat} object that can be used to format dates and
   1634 //     * times in the calendar system specified by <code>cal</code>.
   1635 //     *
   1636 //     * <p><b>Note:</b> When this functionality is moved into the core JDK, this method
   1637 //     * will probably be replaced by a new overload of {@link DateFormat#getInstance}.
   1638 //     *
   1639 //     * @param cal   The calendar system for which a date/time format is desired.
   1640 //     *
   1641 //     * @param pattern The pattern that selects the fields to be formatted. (Uses the
   1642 //     *              {@link DateTimePatternGenerator}.)  This can be
   1643 //     *              {@link DateFormat#ABBR_MONTH}, {@link DateFormat#MONTH_WEEKDAY_DAY},
   1644 //     *              etc.
   1645 //     *
   1646 //     * @param locale The locale for which the date/time format is desired.
   1647 //     *
   1648 //     * @stable ICU 4.0
   1649 //     */
   1650 //    public final static DateFormat getPatternInstance(
   1651 //        Calendar cal, String pattern, ULocale locale) {
   1652 //        throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base");
   1653 //    }
   1654 
   1655     /**
   1656      * The instances of this inner class are used as attribute keys and values
   1657      * in AttributedCharacterIterator that
   1658      * DateFormat.formatToCharacterIterator() method returns.
   1659      *
   1660      * <p>There is no public constructor to this class, the only instances are the
   1661      * constants defined here.
   1662      * <p>
   1663      * @stable ICU 3.8
   1664      */
   1665     public static class Field extends Format.Field {
   1666 
   1667         private static final long serialVersionUID = -3627456821000730829L;
   1668 
   1669         // Max number of calendar fields
   1670         private static final int CAL_FIELD_COUNT;
   1671 
   1672         // Table for mapping calendar field number to DateFormat.Field
   1673         private static final Field[] CAL_FIELDS;
   1674 
   1675         // Map for resolving DateFormat.Field by name
   1676         private static final Map<String, Field> FIELD_NAME_MAP;
   1677 
   1678         static {
   1679             Calendar cal = Calendar.getInstance();
   1680             CAL_FIELD_COUNT = cal.getFieldCount();
   1681             CAL_FIELDS = new Field[CAL_FIELD_COUNT];
   1682             FIELD_NAME_MAP = new HashMap<String, Field>(CAL_FIELD_COUNT);
   1683         }
   1684 
   1685         // Java fields -------------------
   1686 
   1687         /**
   1688          * Constant identifying the time of day indicator(am/pm).
   1689          * @stable ICU 3.8
   1690          */
   1691         public static final Field AM_PM = new Field("am pm", Calendar.AM_PM);
   1692 
   1693         /**
   1694          * Constant identifying the day of month field.
   1695          * @stable ICU 3.8
   1696          */
   1697         public static final Field DAY_OF_MONTH = new Field("day of month", Calendar.DAY_OF_MONTH);
   1698 
   1699         /**
   1700          * Constant identifying the day of week field.
   1701          * @stable ICU 3.8
   1702          */
   1703         public static final Field DAY_OF_WEEK = new Field("day of week", Calendar.DAY_OF_WEEK);
   1704 
   1705         /**
   1706          * Constant identifying the day of week in month field.
   1707          * @stable ICU 3.8
   1708          */
   1709         public static final Field DAY_OF_WEEK_IN_MONTH =
   1710             new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
   1711 
   1712         /**
   1713          * Constant identifying the day of year field.
   1714          * @stable ICU 3.8
   1715          */
   1716         public static final Field DAY_OF_YEAR = new Field("day of year", Calendar.DAY_OF_YEAR);
   1717 
   1718         /**
   1719          * Constant identifying the era field.
   1720          * @stable ICU 3.8
   1721          */
   1722         public static final Field ERA = new Field("era", Calendar.ERA);
   1723 
   1724         /**
   1725          * Constant identifying the hour(0-23) of day field.
   1726          * @stable ICU 3.8
   1727          */
   1728         public static final Field HOUR_OF_DAY0 = new Field("hour of day", Calendar.HOUR_OF_DAY);
   1729 
   1730         /**
   1731          * Constant identifying the hour(1-24) of day field.
   1732          * @stable ICU 3.8
   1733          */
   1734         public static final Field HOUR_OF_DAY1 = new Field("hour of day 1", -1);
   1735 
   1736         /**
   1737          * Constant identifying the hour(0-11) field.
   1738          * @stable ICU 3.8
   1739          */
   1740         public static final Field HOUR0 = new Field("hour", Calendar.HOUR);
   1741 
   1742         /**
   1743          * Constant identifying the hour(1-12) field.
   1744          * @stable ICU 3.8
   1745          */
   1746         public static final Field HOUR1 = new Field("hour 1", -1);
   1747 
   1748         /**
   1749          * Constant identifying the millisecond field.
   1750          * @stable ICU 3.8
   1751          */
   1752         public static final Field MILLISECOND = new Field("millisecond", Calendar.MILLISECOND);
   1753 
   1754         /**
   1755          * Constant identifying the minute field.
   1756          * @stable ICU 3.8
   1757          */
   1758         public static final Field MINUTE = new Field("minute", Calendar.MINUTE);
   1759 
   1760         /**
   1761          * Constant identifying the month field.
   1762          * @stable ICU 3.8
   1763          */
   1764         public static final Field MONTH = new Field("month", Calendar.MONTH);
   1765 
   1766         /**
   1767          * Constant identifying the second field.
   1768          * @stable ICU 3.8
   1769          */
   1770         public static final Field SECOND = new Field("second", Calendar.SECOND);
   1771 
   1772         /**
   1773          * Constant identifying the time zone field.
   1774          * @stable ICU 3.8
   1775          */
   1776         public static final Field TIME_ZONE = new Field("time zone", -1);
   1777 
   1778         /**
   1779          * Constant identifying the week of month field.
   1780          * @stable ICU 3.8
   1781          */
   1782         public static final Field WEEK_OF_MONTH =
   1783             new Field("week of month", Calendar.WEEK_OF_MONTH);
   1784 
   1785         /**
   1786          * Constant identifying the week of year field.
   1787          * @stable ICU 3.8
   1788          */
   1789         public static final Field WEEK_OF_YEAR = new Field("week of year", Calendar.WEEK_OF_YEAR);
   1790 
   1791         /**
   1792          * Constant identifying the year field.
   1793          * @stable ICU 3.8
   1794          */
   1795         public static final Field YEAR = new Field("year", Calendar.YEAR);
   1796 
   1797 
   1798         // ICU only fields -------------------
   1799 
   1800 //        /**
   1801 //         * Constant identifying the local day of week field.
   1802 //         * @stable ICU 3.8
   1803 //         */
   1804 //        public static final Field DOW_LOCAL = new Field("local day of week", Calendar.DOW_LOCAL);
   1805 
   1806 //        /**
   1807 //         * Constant identifying the extended year field.
   1808 //         * @stable ICU 3.8
   1809 //         */
   1810 //        public static final Field EXTENDED_YEAR = new Field("extended year",
   1811 //                                                            Calendar.EXTENDED_YEAR);
   1812 
   1813 //        /**
   1814 //         * Constant identifying the Julian day field.
   1815 //         * @stable ICU 3.8
   1816 //         */
   1817 //        public static final Field JULIAN_DAY = new Field("Julian day", Calendar.JULIAN_DAY);
   1818 
   1819 //        /**
   1820 //         * Constant identifying the milliseconds in day field.
   1821 //         * @stable ICU 3.8
   1822 //         */
   1823 //        public static final Field MILLISECONDS_IN_DAY =
   1824 //            new Field("milliseconds in day", Calendar.MILLISECONDS_IN_DAY);
   1825 
   1826 //        /**
   1827 //         * Constant identifying the year used with week of year field.
   1828 //         * @stable ICU 3.8
   1829 //         */
   1830 //        public static final Field YEAR_WOY = new Field("year for week of year", Calendar.YEAR_WOY);
   1831 
   1832 //        /**
   1833 //         * Constant identifying the quarter field.
   1834 //         * @stable ICU 3.8
   1835 //         */
   1836 //        public static final Field QUARTER = new Field("quarter", -1);
   1837 
   1838         // Stand alone types are variants for its base types.  So we do not define Field for
   1839         // them.
   1840         /*
   1841         public static final Field STANDALONE_DAY =
   1842             new Field("stand alone day of week", Calendar.DAY_OF_WEEK);
   1843         public static final Field STANDALONE_MONTH = new Field("stand alone month", Calendar.MONTH);
   1844         public static final Field STANDALONE_QUARTER = new Field("stand alone quarter", -1);
   1845         */
   1846 
   1847         // Corresponding calendar field
   1848         private final int calendarField;
   1849 
   1850         /**
   1851          * Constructs a <code>DateFormat.Field</code> with the given name and
   1852          * the <code>Calendar</code> field which this attribute represents.  Use -1 for
   1853          * <code>calendarField</code> if this field does not have a corresponding
   1854          * <code>Calendar</code> field.
   1855          *
   1856          * @param name          Name of the attribute
   1857          * @param calendarField <code>Calendar</code> field constant
   1858          *
   1859          * @stable ICU 3.8
   1860          */
   1861         protected Field(String name, int calendarField) {
   1862             super(name);
   1863             this.calendarField = calendarField;
   1864             if (this.getClass() == DateFormat.Field.class) {
   1865                 FIELD_NAME_MAP.put(name, this);
   1866                 if (calendarField >= 0 && calendarField < CAL_FIELD_COUNT) {
   1867                     CAL_FIELDS[calendarField] = this;
   1868                 }
   1869             }
   1870         }
   1871 
   1872         /**
   1873          * Returns the <code>Field</code> constant that corresponds to the <code>
   1874          * Calendar</code> field <code>calendarField</code>.  If there is no
   1875          * corresponding <code>Field</code> is available, null is returned.
   1876          *
   1877          * @param calendarField <code>Calendar</code> field constant
   1878          * @return <code>Field</code> associated with the <code>calendarField</code>,
   1879          * or null if no associated <code>Field</code> is available.
   1880          * @throws IllegalArgumentException if <code>calendarField</code> is not
   1881          * a valid <code>Calendar</code> field constant.
   1882          *
   1883          * @stable ICU 3.8
   1884          */
   1885         public static DateFormat.Field ofCalendarField(int calendarField) {
   1886             if (calendarField < 0 || calendarField >= CAL_FIELD_COUNT) {
   1887                 throw new IllegalArgumentException("Calendar field number is out of range");
   1888             }
   1889             return CAL_FIELDS[calendarField];
   1890         }
   1891 
   1892         /**
   1893          * Returns the <code>Calendar</code> field associated with this attribute.
   1894          * If there is no corresponding <code>Calendar</code> available, this will
   1895          * return -1.
   1896          *
   1897          * @return <code>Calendar</code> constant for this attribute.
   1898          *
   1899          * @stable ICU 3.8
   1900          */
   1901         public int getCalendarField() {
   1902             return calendarField;
   1903         }
   1904 
   1905         /**
   1906          * Resolves instances being deserialized to the predefined constants.
   1907          *
   1908          * @throws InvalidObjectException if the constant could not be resolved.
   1909          *
   1910          * @stable ICU 3.8
   1911          */
   1912         protected Object readResolve() throws InvalidObjectException {
   1913             ///CLOVER:OFF
   1914             if (this.getClass() != DateFormat.Field.class) {
   1915                 throw new InvalidObjectException(
   1916                     "A subclass of DateFormat.Field must implement readResolve.");
   1917             }
   1918             ///CLOVER:ON
   1919             Object o = FIELD_NAME_MAP.get(this.getName());
   1920             ///CLOVER:OFF
   1921             if (o == null) {
   1922                 throw new InvalidObjectException("Unknown attribute name.");
   1923             }
   1924             ///CLOVER:ON
   1925             return o;
   1926         }
   1927     }
   1928 
   1929     private static int getJDKFormatStyle(int icuFormatStyle) {
   1930         switch (icuFormatStyle) {
   1931         case DateFormat.FULL:
   1932             return java.text.DateFormat.FULL;
   1933         case DateFormat.LONG:
   1934             return java.text.DateFormat.LONG;
   1935         case DateFormat.MEDIUM:
   1936             return java.text.DateFormat.MEDIUM;
   1937         case DateFormat.SHORT:
   1938             return java.text.DateFormat.SHORT;
   1939         default:
   1940             throw new UnsupportedOperationException("Style not supported by com.ibm.icu.base");
   1941         }
   1942     }
   1943 
   1944 
   1945     protected static FieldPosition toJDKFieldPosition(FieldPosition icuPos) {
   1946         if (icuPos == null) {
   1947             return null;
   1948         }
   1949 
   1950         int fieldID = icuPos.getField();
   1951         Format.Field fieldAttribute = icuPos.getFieldAttribute();
   1952 
   1953         FieldPosition jdkPos = null;
   1954 
   1955         if (fieldID >= 0) {
   1956             switch (fieldID) {
   1957             case ERA_FIELD:
   1958                 fieldID = java.text.DateFormat.ERA_FIELD;
   1959                 break;
   1960             case YEAR_FIELD:
   1961                 fieldID = java.text.DateFormat.YEAR_FIELD;
   1962                 break;
   1963             case MONTH_FIELD:
   1964                 fieldID = java.text.DateFormat.MONTH_FIELD;
   1965                 break;
   1966             case DATE_FIELD:
   1967                 fieldID = java.text.DateFormat.DATE_FIELD;
   1968                 break;
   1969             case HOUR_OF_DAY1_FIELD:
   1970                 fieldID = java.text.DateFormat.HOUR_OF_DAY1_FIELD;
   1971                 break;
   1972             case HOUR_OF_DAY0_FIELD:
   1973                 fieldID = java.text.DateFormat.HOUR_OF_DAY0_FIELD;
   1974                 break;
   1975             case MINUTE_FIELD:
   1976                 fieldID = java.text.DateFormat.MINUTE_FIELD;
   1977                 break;
   1978             case SECOND_FIELD:
   1979                 fieldID = java.text.DateFormat.SECOND_FIELD;
   1980                 break;
   1981             case FRACTIONAL_SECOND_FIELD: // MILLISECOND_FIELD
   1982                 fieldID = java.text.DateFormat.MILLISECOND_FIELD;
   1983                 break;
   1984             case DAY_OF_WEEK_FIELD:
   1985                 fieldID = java.text.DateFormat.DAY_OF_WEEK_FIELD;
   1986                 break;
   1987             case DAY_OF_YEAR_FIELD:
   1988                 fieldID = java.text.DateFormat.DAY_OF_YEAR_FIELD;
   1989                 break;
   1990             case DAY_OF_WEEK_IN_MONTH_FIELD:
   1991                 fieldID = java.text.DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD;
   1992                 break;
   1993             case WEEK_OF_YEAR_FIELD:
   1994                 fieldID = java.text.DateFormat.WEEK_OF_YEAR_FIELD;
   1995                 break;
   1996             case WEEK_OF_MONTH_FIELD:
   1997                 fieldID = java.text.DateFormat.WEEK_OF_MONTH_FIELD;
   1998                 break;
   1999             case AM_PM_FIELD:
   2000                 fieldID = java.text.DateFormat.AM_PM_FIELD;
   2001                 break;
   2002             case HOUR1_FIELD:
   2003                 fieldID = java.text.DateFormat.HOUR1_FIELD;
   2004                 break;
   2005             case HOUR0_FIELD:
   2006                 fieldID = java.text.DateFormat.HOUR0_FIELD;
   2007                 break;
   2008             case TIMEZONE_FIELD:
   2009                 fieldID = java.text.DateFormat.TIMEZONE_FIELD;
   2010                 break;
   2011 
   2012             case YEAR_WOY_FIELD:
   2013             case DOW_LOCAL_FIELD:
   2014             case EXTENDED_YEAR_FIELD:
   2015             case JULIAN_DAY_FIELD:
   2016             case MILLISECONDS_IN_DAY_FIELD:
   2017             case TIMEZONE_RFC_FIELD:
   2018             case TIMEZONE_GENERIC_FIELD:
   2019             case STANDALONE_DAY_FIELD:
   2020             case STANDALONE_MONTH_FIELD:
   2021             case QUARTER_FIELD:
   2022             case STANDALONE_QUARTER_FIELD:
   2023             case TIMEZONE_SPECIAL_FIELD:
   2024                 throw new UnsupportedOperationException("Format Field ID not supported by com.ibm.icu.base");
   2025 
   2026             default:
   2027                 // just let it go
   2028                 break;
   2029             }
   2030         }
   2031 
   2032         if (fieldAttribute != null) {
   2033             // map field
   2034             if (fieldAttribute.equals(Field.AM_PM)) {
   2035                 fieldAttribute = java.text.DateFormat.Field.AM_PM;
   2036             } else if (fieldAttribute.equals(Field.DAY_OF_MONTH)) {
   2037                 fieldAttribute = java.text.DateFormat.Field.DAY_OF_MONTH;
   2038             } else if (fieldAttribute.equals(Field.DAY_OF_WEEK)) {
   2039                 fieldAttribute = java.text.DateFormat.Field.DAY_OF_WEEK;
   2040             } else if (fieldAttribute.equals(Field.DAY_OF_WEEK_IN_MONTH)) {
   2041                 fieldAttribute = java.text.DateFormat.Field.DAY_OF_WEEK_IN_MONTH;
   2042             } else if (fieldAttribute.equals(Field.DAY_OF_YEAR)) {
   2043                 fieldAttribute = java.text.DateFormat.Field.DAY_OF_YEAR;
   2044             } else if (fieldAttribute.equals(Field.ERA)) {
   2045                 fieldAttribute = java.text.DateFormat.Field.ERA;
   2046             } else if (fieldAttribute.equals(Field.HOUR_OF_DAY0)) {
   2047                 fieldAttribute = java.text.DateFormat.Field.HOUR_OF_DAY0;
   2048             } else if (fieldAttribute.equals(Field.HOUR_OF_DAY1)) {
   2049                 fieldAttribute = java.text.DateFormat.Field.HOUR_OF_DAY1;
   2050             } else if (fieldAttribute.equals(Field.HOUR0)) {
   2051                 fieldAttribute = java.text.DateFormat.Field.HOUR0;
   2052             } else if (fieldAttribute.equals(Field.HOUR1)) {
   2053                 fieldAttribute = java.text.DateFormat.Field.HOUR1;
   2054             } else if (fieldAttribute.equals(Field.MILLISECOND)) {
   2055                 fieldAttribute = java.text.DateFormat.Field.MILLISECOND;
   2056             } else if (fieldAttribute.equals(Field.MINUTE)) {
   2057                 fieldAttribute = java.text.DateFormat.Field.MINUTE;
   2058             } else if (fieldAttribute.equals(Field.MONTH)) {
   2059                 fieldAttribute = java.text.DateFormat.Field.MONTH;
   2060             } else if (fieldAttribute.equals(Field.SECOND)) {
   2061                 fieldAttribute = java.text.DateFormat.Field.SECOND;
   2062             } else if (fieldAttribute.equals(Field.TIME_ZONE)) {
   2063                 fieldAttribute = java.text.DateFormat.Field.TIME_ZONE;
   2064             } else if (fieldAttribute.equals(Field.WEEK_OF_MONTH)) {
   2065                 fieldAttribute = java.text.DateFormat.Field.WEEK_OF_MONTH;
   2066             } else if (fieldAttribute.equals(Field.WEEK_OF_YEAR)) {
   2067                 fieldAttribute = java.text.DateFormat.Field.WEEK_OF_YEAR;
   2068             } else if (fieldAttribute.equals(Field.YEAR)) {
   2069                 fieldAttribute = java.text.DateFormat.Field.YEAR;
   2070             }
   2071 //            else if (fieldAttribute.equals(Field.DOW_LOCAL)
   2072 //                    || fieldAttribute.equals(Field.EXTENDED_YEAR)
   2073 //                    || fieldAttribute.equals(Field.JULIAN_DAY)
   2074 //                    || fieldAttribute.equals(Field.MILLISECONDS_IN_DAY)
   2075 //                    || fieldAttribute.equals(Field.YEAR_WOY)
   2076 //                    || fieldAttribute.equals(Field.QUARTER)) {
   2077 //                // Not supported
   2078 //                throw new UnsupportedOperationException("Format Field not supported by com.ibm.icu.base");
   2079 //            }
   2080 
   2081             jdkPos = new FieldPosition(fieldAttribute, fieldID);
   2082         } else {
   2083             jdkPos = new FieldPosition(fieldID);
   2084         }
   2085 
   2086         jdkPos.setBeginIndex(icuPos.getBeginIndex());
   2087         jdkPos.setEndIndex(icuPos.getEndIndex());
   2088 
   2089         return jdkPos;
   2090     }
   2091 }
   2092