rt/emul/compact/src/main/java/java/text/DateFormat.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 03 Oct 2013 15:40:35 +0200
branchjdk7-b147
changeset 1334 588d5bf7a560
child 1339 8cc04f85a683
permissions -rw-r--r--
Set of JDK classes needed to run javac
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
/*
jtulach@1334
    27
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
jtulach@1334
    28
 * (C) Copyright IBM Corp. 1996 - All Rights Reserved
jtulach@1334
    29
 *
jtulach@1334
    30
 *   The original version of this source code and documentation is copyrighted
jtulach@1334
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
jtulach@1334
    32
 * materials are provided under terms of a License Agreement between Taligent
jtulach@1334
    33
 * and Sun. This technology is protected by multiple US and International
jtulach@1334
    34
 * patents. This notice and attribution to Taligent may not be removed.
jtulach@1334
    35
 *   Taligent is a registered trademark of Taligent, Inc.
jtulach@1334
    36
 *
jtulach@1334
    37
 */
jtulach@1334
    38
jtulach@1334
    39
package java.text;
jtulach@1334
    40
jtulach@1334
    41
import java.io.InvalidObjectException;
jtulach@1334
    42
import java.text.spi.DateFormatProvider;
jtulach@1334
    43
import java.util.Calendar;
jtulach@1334
    44
import java.util.Date;
jtulach@1334
    45
import java.util.GregorianCalendar;
jtulach@1334
    46
import java.util.HashMap;
jtulach@1334
    47
import java.util.Locale;
jtulach@1334
    48
import java.util.Map;
jtulach@1334
    49
import java.util.MissingResourceException;
jtulach@1334
    50
import java.util.ResourceBundle;
jtulach@1334
    51
import java.util.TimeZone;
jtulach@1334
    52
import java.util.spi.LocaleServiceProvider;
jtulach@1334
    53
import sun.util.LocaleServiceProviderPool;
jtulach@1334
    54
jtulach@1334
    55
/**
jtulach@1334
    56
 * {@code DateFormat} is an abstract class for date/time formatting subclasses which
jtulach@1334
    57
 * formats and parses dates or time in a language-independent manner.
jtulach@1334
    58
 * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
jtulach@1334
    59
 * formatting (i.e., date -> text), parsing (text -> date), and
jtulach@1334
    60
 * normalization.  The date is represented as a <code>Date</code> object or
jtulach@1334
    61
 * as the milliseconds since January 1, 1970, 00:00:00 GMT.
jtulach@1334
    62
 *
jtulach@1334
    63
 * <p>{@code DateFormat} provides many class methods for obtaining default date/time
jtulach@1334
    64
 * formatters based on the default or a given locale and a number of formatting
jtulach@1334
    65
 * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
jtulach@1334
    66
 * detail and examples of using these styles are provided in the method
jtulach@1334
    67
 * descriptions.
jtulach@1334
    68
 *
jtulach@1334
    69
 * <p>{@code DateFormat} helps you to format and parse dates for any locale.
jtulach@1334
    70
 * Your code can be completely independent of the locale conventions for
jtulach@1334
    71
 * months, days of the week, or even the calendar format: lunar vs. solar.
jtulach@1334
    72
 *
jtulach@1334
    73
 * <p>To format a date for the current Locale, use one of the
jtulach@1334
    74
 * static factory methods:
jtulach@1334
    75
 * <pre>
jtulach@1334
    76
 *  myString = DateFormat.getDateInstance().format(myDate);
jtulach@1334
    77
 * </pre>
jtulach@1334
    78
 * <p>If you are formatting multiple dates, it is
jtulach@1334
    79
 * more efficient to get the format and use it multiple times so that
jtulach@1334
    80
 * the system doesn't have to fetch the information about the local
jtulach@1334
    81
 * language and country conventions multiple times.
jtulach@1334
    82
 * <pre>
jtulach@1334
    83
 *  DateFormat df = DateFormat.getDateInstance();
jtulach@1334
    84
 *  for (int i = 0; i < myDate.length; ++i) {
jtulach@1334
    85
 *    output.println(df.format(myDate[i]) + "; ");
jtulach@1334
    86
 *  }
jtulach@1334
    87
 * </pre>
jtulach@1334
    88
 * <p>To format a date for a different Locale, specify it in the
jtulach@1334
    89
 * call to {@link #getDateInstance(int, Locale) getDateInstance()}.
jtulach@1334
    90
 * <pre>
jtulach@1334
    91
 *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
jtulach@1334
    92
 * </pre>
jtulach@1334
    93
 * <p>You can use a DateFormat to parse also.
jtulach@1334
    94
 * <pre>
jtulach@1334
    95
 *  myDate = df.parse(myString);
jtulach@1334
    96
 * </pre>
jtulach@1334
    97
 * <p>Use {@code getDateInstance} to get the normal date format for that country.
jtulach@1334
    98
 * There are other static factory methods available.
jtulach@1334
    99
 * Use {@code getTimeInstance} to get the time format for that country.
jtulach@1334
   100
 * Use {@code getDateTimeInstance} to get a date and time format. You can pass in
jtulach@1334
   101
 * different options to these factory methods to control the length of the
jtulach@1334
   102
 * result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}. The exact result depends
jtulach@1334
   103
 * on the locale, but generally:
jtulach@1334
   104
 * <ul><li>{@link #SHORT} is completely numeric, such as {@code 12.13.52} or {@code 3:30pm}
jtulach@1334
   105
 * <li>{@link #MEDIUM} is longer, such as {@code Jan 12, 1952}
jtulach@1334
   106
 * <li>{@link #LONG} is longer, such as {@code January 12, 1952} or {@code 3:30:32pm}
jtulach@1334
   107
 * <li>{@link #FULL} is pretty completely specified, such as
jtulach@1334
   108
 * {@code Tuesday, April 12, 1952 AD or 3:30:42pm PST}.
jtulach@1334
   109
 * </ul>
jtulach@1334
   110
 *
jtulach@1334
   111
 * <p>You can also set the time zone on the format if you wish.
jtulach@1334
   112
 * If you want even more control over the format or parsing,
jtulach@1334
   113
 * (or want to give your users more control),
jtulach@1334
   114
 * you can try casting the {@code DateFormat} you get from the factory methods
jtulach@1334
   115
 * to a {@link SimpleDateFormat}. This will work for the majority
jtulach@1334
   116
 * of countries; just remember to put it in a {@code try} block in case you
jtulach@1334
   117
 * encounter an unusual one.
jtulach@1334
   118
 *
jtulach@1334
   119
 * <p>You can also use forms of the parse and format methods with
jtulach@1334
   120
 * {@link ParsePosition} and {@link FieldPosition} to
jtulach@1334
   121
 * allow you to
jtulach@1334
   122
 * <ul><li>progressively parse through pieces of a string.
jtulach@1334
   123
 * <li>align any particular field, or find out where it is for selection
jtulach@1334
   124
 * on the screen.
jtulach@1334
   125
 * </ul>
jtulach@1334
   126
 *
jtulach@1334
   127
 * <h4><a name="synchronization">Synchronization</a></h4>
jtulach@1334
   128
 *
jtulach@1334
   129
 * <p>
jtulach@1334
   130
 * Date formats are not synchronized.
jtulach@1334
   131
 * It is recommended to create separate format instances for each thread.
jtulach@1334
   132
 * If multiple threads access a format concurrently, it must be synchronized
jtulach@1334
   133
 * externally.
jtulach@1334
   134
 *
jtulach@1334
   135
 * @see          Format
jtulach@1334
   136
 * @see          NumberFormat
jtulach@1334
   137
 * @see          SimpleDateFormat
jtulach@1334
   138
 * @see          java.util.Calendar
jtulach@1334
   139
 * @see          java.util.GregorianCalendar
jtulach@1334
   140
 * @see          java.util.TimeZone
jtulach@1334
   141
 * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
jtulach@1334
   142
 */
jtulach@1334
   143
public abstract class DateFormat extends Format {
jtulach@1334
   144
jtulach@1334
   145
    /**
jtulach@1334
   146
     * The {@link Calendar} instance used for calculating the date-time fields
jtulach@1334
   147
     * and the instant of time. This field is used for both formatting and
jtulach@1334
   148
     * parsing.
jtulach@1334
   149
     *
jtulach@1334
   150
     * <p>Subclasses should initialize this field to a {@link Calendar}
jtulach@1334
   151
     * appropriate for the {@link Locale} associated with this
jtulach@1334
   152
     * <code>DateFormat</code>.
jtulach@1334
   153
     * @serial
jtulach@1334
   154
     */
jtulach@1334
   155
    protected Calendar calendar;
jtulach@1334
   156
jtulach@1334
   157
    /**
jtulach@1334
   158
     * The number formatter that <code>DateFormat</code> uses to format numbers
jtulach@1334
   159
     * in dates and times.  Subclasses should initialize this to a number format
jtulach@1334
   160
     * appropriate for the locale associated with this <code>DateFormat</code>.
jtulach@1334
   161
     * @serial
jtulach@1334
   162
     */
jtulach@1334
   163
    protected NumberFormat numberFormat;
jtulach@1334
   164
jtulach@1334
   165
    /**
jtulach@1334
   166
     * Useful constant for ERA field alignment.
jtulach@1334
   167
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   168
     */
jtulach@1334
   169
    public final static int ERA_FIELD = 0;
jtulach@1334
   170
    /**
jtulach@1334
   171
     * Useful constant for YEAR field alignment.
jtulach@1334
   172
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   173
     */
jtulach@1334
   174
    public final static int YEAR_FIELD = 1;
jtulach@1334
   175
    /**
jtulach@1334
   176
     * Useful constant for MONTH field alignment.
jtulach@1334
   177
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   178
     */
jtulach@1334
   179
    public final static int MONTH_FIELD = 2;
jtulach@1334
   180
    /**
jtulach@1334
   181
     * Useful constant for DATE field alignment.
jtulach@1334
   182
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   183
     */
jtulach@1334
   184
    public final static int DATE_FIELD = 3;
jtulach@1334
   185
    /**
jtulach@1334
   186
     * Useful constant for one-based HOUR_OF_DAY field alignment.
jtulach@1334
   187
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   188
     * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
jtulach@1334
   189
     * For example, 23:59 + 01:00 results in 24:59.
jtulach@1334
   190
     */
jtulach@1334
   191
    public final static int HOUR_OF_DAY1_FIELD = 4;
jtulach@1334
   192
    /**
jtulach@1334
   193
     * Useful constant for zero-based HOUR_OF_DAY field alignment.
jtulach@1334
   194
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   195
     * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
jtulach@1334
   196
     * For example, 23:59 + 01:00 results in 00:59.
jtulach@1334
   197
     */
jtulach@1334
   198
    public final static int HOUR_OF_DAY0_FIELD = 5;
jtulach@1334
   199
    /**
jtulach@1334
   200
     * Useful constant for MINUTE field alignment.
jtulach@1334
   201
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   202
     */
jtulach@1334
   203
    public final static int MINUTE_FIELD = 6;
jtulach@1334
   204
    /**
jtulach@1334
   205
     * Useful constant for SECOND field alignment.
jtulach@1334
   206
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   207
     */
jtulach@1334
   208
    public final static int SECOND_FIELD = 7;
jtulach@1334
   209
    /**
jtulach@1334
   210
     * Useful constant for MILLISECOND field alignment.
jtulach@1334
   211
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   212
     */
jtulach@1334
   213
    public final static int MILLISECOND_FIELD = 8;
jtulach@1334
   214
    /**
jtulach@1334
   215
     * Useful constant for DAY_OF_WEEK field alignment.
jtulach@1334
   216
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   217
     */
jtulach@1334
   218
    public final static int DAY_OF_WEEK_FIELD = 9;
jtulach@1334
   219
    /**
jtulach@1334
   220
     * Useful constant for DAY_OF_YEAR field alignment.
jtulach@1334
   221
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   222
     */
jtulach@1334
   223
    public final static int DAY_OF_YEAR_FIELD = 10;
jtulach@1334
   224
    /**
jtulach@1334
   225
     * Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
jtulach@1334
   226
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   227
     */
jtulach@1334
   228
    public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
jtulach@1334
   229
    /**
jtulach@1334
   230
     * Useful constant for WEEK_OF_YEAR field alignment.
jtulach@1334
   231
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   232
     */
jtulach@1334
   233
    public final static int WEEK_OF_YEAR_FIELD = 12;
jtulach@1334
   234
    /**
jtulach@1334
   235
     * Useful constant for WEEK_OF_MONTH field alignment.
jtulach@1334
   236
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   237
     */
jtulach@1334
   238
    public final static int WEEK_OF_MONTH_FIELD = 13;
jtulach@1334
   239
    /**
jtulach@1334
   240
     * Useful constant for AM_PM field alignment.
jtulach@1334
   241
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   242
     */
jtulach@1334
   243
    public final static int AM_PM_FIELD = 14;
jtulach@1334
   244
    /**
jtulach@1334
   245
     * Useful constant for one-based HOUR field alignment.
jtulach@1334
   246
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   247
     * HOUR1_FIELD is used for the one-based 12-hour clock.
jtulach@1334
   248
     * For example, 11:30 PM + 1 hour results in 12:30 AM.
jtulach@1334
   249
     */
jtulach@1334
   250
    public final static int HOUR1_FIELD = 15;
jtulach@1334
   251
    /**
jtulach@1334
   252
     * Useful constant for zero-based HOUR field alignment.
jtulach@1334
   253
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   254
     * HOUR0_FIELD is used for the zero-based 12-hour clock.
jtulach@1334
   255
     * For example, 11:30 PM + 1 hour results in 00:30 AM.
jtulach@1334
   256
     */
jtulach@1334
   257
    public final static int HOUR0_FIELD = 16;
jtulach@1334
   258
    /**
jtulach@1334
   259
     * Useful constant for TIMEZONE field alignment.
jtulach@1334
   260
     * Used in FieldPosition of date/time formatting.
jtulach@1334
   261
     */
jtulach@1334
   262
    public final static int TIMEZONE_FIELD = 17;
jtulach@1334
   263
jtulach@1334
   264
    // Proclaim serial compatibility with 1.1 FCS
jtulach@1334
   265
    private static final long serialVersionUID = 7218322306649953788L;
jtulach@1334
   266
jtulach@1334
   267
    /**
jtulach@1334
   268
     * Overrides Format.
jtulach@1334
   269
     * Formats a time object into a time string. Examples of time objects
jtulach@1334
   270
     * are a time value expressed in milliseconds and a Date object.
jtulach@1334
   271
     * @param obj must be a Number or a Date.
jtulach@1334
   272
     * @param toAppendTo the string buffer for the returning time string.
jtulach@1334
   273
     * @return the string buffer passed in as toAppendTo, with formatted text appended.
jtulach@1334
   274
     * @param fieldPosition keeps track of the position of the field
jtulach@1334
   275
     * within the returned string.
jtulach@1334
   276
     * On input: an alignment field,
jtulach@1334
   277
     * if desired. On output: the offsets of the alignment field. For
jtulach@1334
   278
     * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
jtulach@1334
   279
     * if the given fieldPosition is DateFormat.YEAR_FIELD, the
jtulach@1334
   280
     * begin index and end index of fieldPosition will be set to
jtulach@1334
   281
     * 0 and 4, respectively.
jtulach@1334
   282
     * Notice that if the same time field appears
jtulach@1334
   283
     * more than once in a pattern, the fieldPosition will be set for the first
jtulach@1334
   284
     * occurrence of that time field. For instance, formatting a Date to
jtulach@1334
   285
     * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
jtulach@1334
   286
     * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
jtulach@1334
   287
     * the begin index and end index of fieldPosition will be set to
jtulach@1334
   288
     * 5 and 8, respectively, for the first occurrence of the timezone
jtulach@1334
   289
     * pattern character 'z'.
jtulach@1334
   290
     * @see java.text.Format
jtulach@1334
   291
     */
jtulach@1334
   292
    public final StringBuffer format(Object obj, StringBuffer toAppendTo,
jtulach@1334
   293
                                     FieldPosition fieldPosition)
jtulach@1334
   294
    {
jtulach@1334
   295
        if (obj instanceof Date)
jtulach@1334
   296
            return format( (Date)obj, toAppendTo, fieldPosition );
jtulach@1334
   297
        else if (obj instanceof Number)
jtulach@1334
   298
            return format( new Date(((Number)obj).longValue()),
jtulach@1334
   299
                          toAppendTo, fieldPosition );
jtulach@1334
   300
        else
jtulach@1334
   301
            throw new IllegalArgumentException("Cannot format given Object as a Date");
jtulach@1334
   302
    }
jtulach@1334
   303
jtulach@1334
   304
    /**
jtulach@1334
   305
     * Formats a Date into a date/time string.
jtulach@1334
   306
     * @param date a Date to be formatted into a date/time string.
jtulach@1334
   307
     * @param toAppendTo the string buffer for the returning date/time string.
jtulach@1334
   308
     * @param fieldPosition keeps track of the position of the field
jtulach@1334
   309
     * within the returned string.
jtulach@1334
   310
     * On input: an alignment field,
jtulach@1334
   311
     * if desired. On output: the offsets of the alignment field. For
jtulach@1334
   312
     * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
jtulach@1334
   313
     * if the given fieldPosition is DateFormat.YEAR_FIELD, the
jtulach@1334
   314
     * begin index and end index of fieldPosition will be set to
jtulach@1334
   315
     * 0 and 4, respectively.
jtulach@1334
   316
     * Notice that if the same time field appears
jtulach@1334
   317
     * more than once in a pattern, the fieldPosition will be set for the first
jtulach@1334
   318
     * occurrence of that time field. For instance, formatting a Date to
jtulach@1334
   319
     * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
jtulach@1334
   320
     * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
jtulach@1334
   321
     * the begin index and end index of fieldPosition will be set to
jtulach@1334
   322
     * 5 and 8, respectively, for the first occurrence of the timezone
jtulach@1334
   323
     * pattern character 'z'.
jtulach@1334
   324
     * @return the string buffer passed in as toAppendTo, with formatted text appended.
jtulach@1334
   325
     */
jtulach@1334
   326
    public abstract StringBuffer format(Date date, StringBuffer toAppendTo,
jtulach@1334
   327
                                        FieldPosition fieldPosition);
jtulach@1334
   328
jtulach@1334
   329
    /**
jtulach@1334
   330
     * Formats a Date into a date/time string.
jtulach@1334
   331
     * @param date the time value to be formatted into a time string.
jtulach@1334
   332
     * @return the formatted time string.
jtulach@1334
   333
     */
jtulach@1334
   334
    public final String format(Date date)
jtulach@1334
   335
    {
jtulach@1334
   336
        return format(date, new StringBuffer(),
jtulach@1334
   337
                      DontCareFieldPosition.INSTANCE).toString();
jtulach@1334
   338
    }
jtulach@1334
   339
jtulach@1334
   340
    /**
jtulach@1334
   341
     * Parses text from the beginning of the given string to produce a date.
jtulach@1334
   342
     * The method may not use the entire text of the given string.
jtulach@1334
   343
     * <p>
jtulach@1334
   344
     * See the {@link #parse(String, ParsePosition)} method for more information
jtulach@1334
   345
     * on date parsing.
jtulach@1334
   346
     *
jtulach@1334
   347
     * @param source A <code>String</code> whose beginning should be parsed.
jtulach@1334
   348
     * @return A <code>Date</code> parsed from the string.
jtulach@1334
   349
     * @exception ParseException if the beginning of the specified string
jtulach@1334
   350
     *            cannot be parsed.
jtulach@1334
   351
     */
jtulach@1334
   352
    public Date parse(String source) throws ParseException
jtulach@1334
   353
    {
jtulach@1334
   354
        ParsePosition pos = new ParsePosition(0);
jtulach@1334
   355
        Date result = parse(source, pos);
jtulach@1334
   356
        if (pos.index == 0)
jtulach@1334
   357
            throw new ParseException("Unparseable date: \"" + source + "\"" ,
jtulach@1334
   358
                pos.errorIndex);
jtulach@1334
   359
        return result;
jtulach@1334
   360
    }
jtulach@1334
   361
jtulach@1334
   362
    /**
jtulach@1334
   363
     * Parse a date/time string according to the given parse position.  For
jtulach@1334
   364
     * example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date}
jtulach@1334
   365
     * that is equivalent to {@code Date(837039900000L)}.
jtulach@1334
   366
     *
jtulach@1334
   367
     * <p> By default, parsing is lenient: If the input is not in the form used
jtulach@1334
   368
     * by this object's format method but can still be parsed as a date, then
jtulach@1334
   369
     * the parse succeeds.  Clients may insist on strict adherence to the
jtulach@1334
   370
     * format by calling {@link #setLenient(boolean) setLenient(false)}.
jtulach@1334
   371
     *
jtulach@1334
   372
     * <p>This parsing operation uses the {@link #calendar} to produce
jtulach@1334
   373
     * a {@code Date}. As a result, the {@code calendar}'s date-time
jtulach@1334
   374
     * fields and the {@code TimeZone} value may have been
jtulach@1334
   375
     * overwritten, depending on subclass implementations. Any {@code
jtulach@1334
   376
     * TimeZone} value that has previously been set by a call to
jtulach@1334
   377
     * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
jtulach@1334
   378
     * to be restored for further operations.
jtulach@1334
   379
     *
jtulach@1334
   380
     * @param source  The date/time string to be parsed
jtulach@1334
   381
     *
jtulach@1334
   382
     * @param pos   On input, the position at which to start parsing; on
jtulach@1334
   383
     *              output, the position at which parsing terminated, or the
jtulach@1334
   384
     *              start position if the parse failed.
jtulach@1334
   385
     *
jtulach@1334
   386
     * @return      A {@code Date}, or {@code null} if the input could not be parsed
jtulach@1334
   387
     */
jtulach@1334
   388
    public abstract Date parse(String source, ParsePosition pos);
jtulach@1334
   389
jtulach@1334
   390
    /**
jtulach@1334
   391
     * Parses text from a string to produce a <code>Date</code>.
jtulach@1334
   392
     * <p>
jtulach@1334
   393
     * The method attempts to parse text starting at the index given by
jtulach@1334
   394
     * <code>pos</code>.
jtulach@1334
   395
     * If parsing succeeds, then the index of <code>pos</code> is updated
jtulach@1334
   396
     * to the index after the last character used (parsing does not necessarily
jtulach@1334
   397
     * use all characters up to the end of the string), and the parsed
jtulach@1334
   398
     * date is returned. The updated <code>pos</code> can be used to
jtulach@1334
   399
     * indicate the starting point for the next call to this method.
jtulach@1334
   400
     * If an error occurs, then the index of <code>pos</code> is not
jtulach@1334
   401
     * changed, the error index of <code>pos</code> is set to the index of
jtulach@1334
   402
     * the character where the error occurred, and null is returned.
jtulach@1334
   403
     * <p>
jtulach@1334
   404
     * See the {@link #parse(String, ParsePosition)} method for more information
jtulach@1334
   405
     * on date parsing.
jtulach@1334
   406
     *
jtulach@1334
   407
     * @param source A <code>String</code>, part of which should be parsed.
jtulach@1334
   408
     * @param pos A <code>ParsePosition</code> object with index and error
jtulach@1334
   409
     *            index information as described above.
jtulach@1334
   410
     * @return A <code>Date</code> parsed from the string. In case of
jtulach@1334
   411
     *         error, returns null.
jtulach@1334
   412
     * @exception NullPointerException if <code>pos</code> is null.
jtulach@1334
   413
     */
jtulach@1334
   414
    public Object parseObject(String source, ParsePosition pos) {
jtulach@1334
   415
        return parse(source, pos);
jtulach@1334
   416
    }
jtulach@1334
   417
jtulach@1334
   418
    /**
jtulach@1334
   419
     * Constant for full style pattern.
jtulach@1334
   420
     */
jtulach@1334
   421
    public static final int FULL = 0;
jtulach@1334
   422
    /**
jtulach@1334
   423
     * Constant for long style pattern.
jtulach@1334
   424
     */
jtulach@1334
   425
    public static final int LONG = 1;
jtulach@1334
   426
    /**
jtulach@1334
   427
     * Constant for medium style pattern.
jtulach@1334
   428
     */
jtulach@1334
   429
    public static final int MEDIUM = 2;
jtulach@1334
   430
    /**
jtulach@1334
   431
     * Constant for short style pattern.
jtulach@1334
   432
     */
jtulach@1334
   433
    public static final int SHORT = 3;
jtulach@1334
   434
    /**
jtulach@1334
   435
     * Constant for default style pattern.  Its value is MEDIUM.
jtulach@1334
   436
     */
jtulach@1334
   437
    public static final int DEFAULT = MEDIUM;
jtulach@1334
   438
jtulach@1334
   439
    /**
jtulach@1334
   440
     * Gets the time formatter with the default formatting style
jtulach@1334
   441
     * for the default locale.
jtulach@1334
   442
     * @return a time formatter.
jtulach@1334
   443
     */
jtulach@1334
   444
    public final static DateFormat getTimeInstance()
jtulach@1334
   445
    {
jtulach@1334
   446
        return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   447
    }
jtulach@1334
   448
jtulach@1334
   449
    /**
jtulach@1334
   450
     * Gets the time formatter with the given formatting style
jtulach@1334
   451
     * for the default locale.
jtulach@1334
   452
     * @param style the given formatting style. For example,
jtulach@1334
   453
     * SHORT for "h:mm a" in the US locale.
jtulach@1334
   454
     * @return a time formatter.
jtulach@1334
   455
     */
jtulach@1334
   456
    public final static DateFormat getTimeInstance(int style)
jtulach@1334
   457
    {
jtulach@1334
   458
        return get(style, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   459
    }
jtulach@1334
   460
jtulach@1334
   461
    /**
jtulach@1334
   462
     * Gets the time formatter with the given formatting style
jtulach@1334
   463
     * for the given locale.
jtulach@1334
   464
     * @param style the given formatting style. For example,
jtulach@1334
   465
     * SHORT for "h:mm a" in the US locale.
jtulach@1334
   466
     * @param aLocale the given locale.
jtulach@1334
   467
     * @return a time formatter.
jtulach@1334
   468
     */
jtulach@1334
   469
    public final static DateFormat getTimeInstance(int style,
jtulach@1334
   470
                                                 Locale aLocale)
jtulach@1334
   471
    {
jtulach@1334
   472
        return get(style, 0, 1, aLocale);
jtulach@1334
   473
    }
jtulach@1334
   474
jtulach@1334
   475
    /**
jtulach@1334
   476
     * Gets the date formatter with the default formatting style
jtulach@1334
   477
     * for the default locale.
jtulach@1334
   478
     * @return a date formatter.
jtulach@1334
   479
     */
jtulach@1334
   480
    public final static DateFormat getDateInstance()
jtulach@1334
   481
    {
jtulach@1334
   482
        return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   483
    }
jtulach@1334
   484
jtulach@1334
   485
    /**
jtulach@1334
   486
     * Gets the date formatter with the given formatting style
jtulach@1334
   487
     * for the default locale.
jtulach@1334
   488
     * @param style the given formatting style. For example,
jtulach@1334
   489
     * SHORT for "M/d/yy" in the US locale.
jtulach@1334
   490
     * @return a date formatter.
jtulach@1334
   491
     */
jtulach@1334
   492
    public final static DateFormat getDateInstance(int style)
jtulach@1334
   493
    {
jtulach@1334
   494
        return get(0, style, 2, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   495
    }
jtulach@1334
   496
jtulach@1334
   497
    /**
jtulach@1334
   498
     * Gets the date formatter with the given formatting style
jtulach@1334
   499
     * for the given locale.
jtulach@1334
   500
     * @param style the given formatting style. For example,
jtulach@1334
   501
     * SHORT for "M/d/yy" in the US locale.
jtulach@1334
   502
     * @param aLocale the given locale.
jtulach@1334
   503
     * @return a date formatter.
jtulach@1334
   504
     */
jtulach@1334
   505
    public final static DateFormat getDateInstance(int style,
jtulach@1334
   506
                                                 Locale aLocale)
jtulach@1334
   507
    {
jtulach@1334
   508
        return get(0, style, 2, aLocale);
jtulach@1334
   509
    }
jtulach@1334
   510
jtulach@1334
   511
    /**
jtulach@1334
   512
     * Gets the date/time formatter with the default formatting style
jtulach@1334
   513
     * for the default locale.
jtulach@1334
   514
     * @return a date/time formatter.
jtulach@1334
   515
     */
jtulach@1334
   516
    public final static DateFormat getDateTimeInstance()
jtulach@1334
   517
    {
jtulach@1334
   518
        return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   519
    }
jtulach@1334
   520
jtulach@1334
   521
    /**
jtulach@1334
   522
     * Gets the date/time formatter with the given date and time
jtulach@1334
   523
     * formatting styles for the default locale.
jtulach@1334
   524
     * @param dateStyle the given date formatting style. For example,
jtulach@1334
   525
     * SHORT for "M/d/yy" in the US locale.
jtulach@1334
   526
     * @param timeStyle the given time formatting style. For example,
jtulach@1334
   527
     * SHORT for "h:mm a" in the US locale.
jtulach@1334
   528
     * @return a date/time formatter.
jtulach@1334
   529
     */
jtulach@1334
   530
    public final static DateFormat getDateTimeInstance(int dateStyle,
jtulach@1334
   531
                                                       int timeStyle)
jtulach@1334
   532
    {
jtulach@1334
   533
        return get(timeStyle, dateStyle, 3, Locale.getDefault(Locale.Category.FORMAT));
jtulach@1334
   534
    }
jtulach@1334
   535
jtulach@1334
   536
    /**
jtulach@1334
   537
     * Gets the date/time formatter with the given formatting styles
jtulach@1334
   538
     * for the given locale.
jtulach@1334
   539
     * @param dateStyle the given date formatting style.
jtulach@1334
   540
     * @param timeStyle the given time formatting style.
jtulach@1334
   541
     * @param aLocale the given locale.
jtulach@1334
   542
     * @return a date/time formatter.
jtulach@1334
   543
     */
jtulach@1334
   544
    public final static DateFormat
jtulach@1334
   545
        getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
jtulach@1334
   546
    {
jtulach@1334
   547
        return get(timeStyle, dateStyle, 3, aLocale);
jtulach@1334
   548
    }
jtulach@1334
   549
jtulach@1334
   550
    /**
jtulach@1334
   551
     * Get a default date/time formatter that uses the SHORT style for both the
jtulach@1334
   552
     * date and the time.
jtulach@1334
   553
     */
jtulach@1334
   554
    public final static DateFormat getInstance() {
jtulach@1334
   555
        return getDateTimeInstance(SHORT, SHORT);
jtulach@1334
   556
    }
jtulach@1334
   557
jtulach@1334
   558
    /**
jtulach@1334
   559
     * Returns an array of all locales for which the
jtulach@1334
   560
     * <code>get*Instance</code> methods of this class can return
jtulach@1334
   561
     * localized instances.
jtulach@1334
   562
     * The returned array represents the union of locales supported by the Java
jtulach@1334
   563
     * runtime and by installed
jtulach@1334
   564
     * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
jtulach@1334
   565
     * It must contain at least a <code>Locale</code> instance equal to
jtulach@1334
   566
     * {@link java.util.Locale#US Locale.US}.
jtulach@1334
   567
     *
jtulach@1334
   568
     * @return An array of locales for which localized
jtulach@1334
   569
     *         <code>DateFormat</code> instances are available.
jtulach@1334
   570
     */
jtulach@1334
   571
    public static Locale[] getAvailableLocales()
jtulach@1334
   572
    {
jtulach@1334
   573
        LocaleServiceProviderPool pool =
jtulach@1334
   574
            LocaleServiceProviderPool.getPool(DateFormatProvider.class);
jtulach@1334
   575
        return pool.getAvailableLocales();
jtulach@1334
   576
    }
jtulach@1334
   577
jtulach@1334
   578
    /**
jtulach@1334
   579
     * Set the calendar to be used by this date format.  Initially, the default
jtulach@1334
   580
     * calendar for the specified or default locale is used.
jtulach@1334
   581
     *
jtulach@1334
   582
     * <p>Any {@link java.util.TimeZone TimeZone} and {@linkplain
jtulach@1334
   583
     * #isLenient() leniency} values that have previously been set are
jtulach@1334
   584
     * overwritten by {@code newCalendar}'s values.
jtulach@1334
   585
     *
jtulach@1334
   586
     * @param newCalendar the new {@code Calendar} to be used by the date format
jtulach@1334
   587
     */
jtulach@1334
   588
    public void setCalendar(Calendar newCalendar)
jtulach@1334
   589
    {
jtulach@1334
   590
        this.calendar = newCalendar;
jtulach@1334
   591
    }
jtulach@1334
   592
jtulach@1334
   593
    /**
jtulach@1334
   594
     * Gets the calendar associated with this date/time formatter.
jtulach@1334
   595
     *
jtulach@1334
   596
     * @return the calendar associated with this date/time formatter.
jtulach@1334
   597
     */
jtulach@1334
   598
    public Calendar getCalendar()
jtulach@1334
   599
    {
jtulach@1334
   600
        return calendar;
jtulach@1334
   601
    }
jtulach@1334
   602
jtulach@1334
   603
    /**
jtulach@1334
   604
     * Allows you to set the number formatter.
jtulach@1334
   605
     * @param newNumberFormat the given new NumberFormat.
jtulach@1334
   606
     */
jtulach@1334
   607
    public void setNumberFormat(NumberFormat newNumberFormat)
jtulach@1334
   608
    {
jtulach@1334
   609
        this.numberFormat = newNumberFormat;
jtulach@1334
   610
    }
jtulach@1334
   611
jtulach@1334
   612
    /**
jtulach@1334
   613
     * Gets the number formatter which this date/time formatter uses to
jtulach@1334
   614
     * format and parse a time.
jtulach@1334
   615
     * @return the number formatter which this date/time formatter uses.
jtulach@1334
   616
     */
jtulach@1334
   617
    public NumberFormat getNumberFormat()
jtulach@1334
   618
    {
jtulach@1334
   619
        return numberFormat;
jtulach@1334
   620
    }
jtulach@1334
   621
jtulach@1334
   622
    /**
jtulach@1334
   623
     * Sets the time zone for the calendar of this {@code DateFormat} object.
jtulach@1334
   624
     * This method is equivalent to the following call.
jtulach@1334
   625
     * <blockquote><pre>
jtulach@1334
   626
     *  getCalendar().setTimeZone(zone)
jtulach@1334
   627
     * </pre></blockquote>
jtulach@1334
   628
     *
jtulach@1334
   629
     * <p>The {@code TimeZone} set by this method is overwritten by a
jtulach@1334
   630
     * {@link #setCalendar(java.util.Calendar) setCalendar} call.
jtulach@1334
   631
     *
jtulach@1334
   632
     * <p>The {@code TimeZone} set by this method may be overwritten as
jtulach@1334
   633
     * a result of a call to the parse method.
jtulach@1334
   634
     *
jtulach@1334
   635
     * @param zone the given new time zone.
jtulach@1334
   636
     */
jtulach@1334
   637
    public void setTimeZone(TimeZone zone)
jtulach@1334
   638
    {
jtulach@1334
   639
        calendar.setTimeZone(zone);
jtulach@1334
   640
    }
jtulach@1334
   641
jtulach@1334
   642
    /**
jtulach@1334
   643
     * Gets the time zone.
jtulach@1334
   644
     * This method is equivalent to the following call.
jtulach@1334
   645
     * <blockquote><pre>
jtulach@1334
   646
     *  getCalendar().getTimeZone()
jtulach@1334
   647
     * </pre></blockquote>
jtulach@1334
   648
     *
jtulach@1334
   649
     * @return the time zone associated with the calendar of DateFormat.
jtulach@1334
   650
     */
jtulach@1334
   651
    public TimeZone getTimeZone()
jtulach@1334
   652
    {
jtulach@1334
   653
        return calendar.getTimeZone();
jtulach@1334
   654
    }
jtulach@1334
   655
jtulach@1334
   656
    /**
jtulach@1334
   657
     * Specify whether or not date/time parsing is to be lenient.  With
jtulach@1334
   658
     * lenient parsing, the parser may use heuristics to interpret inputs that
jtulach@1334
   659
     * do not precisely match this object's format.  With strict parsing,
jtulach@1334
   660
     * inputs must match this object's format.
jtulach@1334
   661
     *
jtulach@1334
   662
     * <p>This method is equivalent to the following call.
jtulach@1334
   663
     * <blockquote><pre>
jtulach@1334
   664
     *  getCalendar().setLenient(lenient)
jtulach@1334
   665
     * </pre></blockquote>
jtulach@1334
   666
     *
jtulach@1334
   667
     * <p>This leniency value is overwritten by a call to {@link
jtulach@1334
   668
     * #setCalendar(java.util.Calendar) setCalendar()}.
jtulach@1334
   669
     *
jtulach@1334
   670
     * @param lenient when {@code true}, parsing is lenient
jtulach@1334
   671
     * @see java.util.Calendar#setLenient(boolean)
jtulach@1334
   672
     */
jtulach@1334
   673
    public void setLenient(boolean lenient)
jtulach@1334
   674
    {
jtulach@1334
   675
        calendar.setLenient(lenient);
jtulach@1334
   676
    }
jtulach@1334
   677
jtulach@1334
   678
    /**
jtulach@1334
   679
     * Tell whether date/time parsing is to be lenient.
jtulach@1334
   680
     * This method is equivalent to the following call.
jtulach@1334
   681
     * <blockquote><pre>
jtulach@1334
   682
     *  getCalendar().isLenient()
jtulach@1334
   683
     * </pre></blockquote>
jtulach@1334
   684
     *
jtulach@1334
   685
     * @return {@code true} if the {@link #calendar} is lenient;
jtulach@1334
   686
     *         {@code false} otherwise.
jtulach@1334
   687
     * @see java.util.Calendar#isLenient()
jtulach@1334
   688
     */
jtulach@1334
   689
    public boolean isLenient()
jtulach@1334
   690
    {
jtulach@1334
   691
        return calendar.isLenient();
jtulach@1334
   692
    }
jtulach@1334
   693
jtulach@1334
   694
    /**
jtulach@1334
   695
     * Overrides hashCode
jtulach@1334
   696
     */
jtulach@1334
   697
    public int hashCode() {
jtulach@1334
   698
        return numberFormat.hashCode();
jtulach@1334
   699
        // just enough fields for a reasonable distribution
jtulach@1334
   700
    }
jtulach@1334
   701
jtulach@1334
   702
    /**
jtulach@1334
   703
     * Overrides equals
jtulach@1334
   704
     */
jtulach@1334
   705
    public boolean equals(Object obj) {
jtulach@1334
   706
        if (this == obj) return true;
jtulach@1334
   707
        if (obj == null || getClass() != obj.getClass()) return false;
jtulach@1334
   708
        DateFormat other = (DateFormat) obj;
jtulach@1334
   709
        return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET!
jtulach@1334
   710
                calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() &&
jtulach@1334
   711
                calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() &&
jtulach@1334
   712
                calendar.isLenient() == other.calendar.isLenient() &&
jtulach@1334
   713
                calendar.getTimeZone().equals(other.calendar.getTimeZone()) &&
jtulach@1334
   714
                numberFormat.equals(other.numberFormat));
jtulach@1334
   715
    }
jtulach@1334
   716
jtulach@1334
   717
    /**
jtulach@1334
   718
     * Overrides Cloneable
jtulach@1334
   719
     */
jtulach@1334
   720
    public Object clone()
jtulach@1334
   721
    {
jtulach@1334
   722
        DateFormat other = (DateFormat) super.clone();
jtulach@1334
   723
        other.calendar = (Calendar) calendar.clone();
jtulach@1334
   724
        other.numberFormat = (NumberFormat) numberFormat.clone();
jtulach@1334
   725
        return other;
jtulach@1334
   726
    }
jtulach@1334
   727
jtulach@1334
   728
    /**
jtulach@1334
   729
     * Creates a DateFormat with the given time and/or date style in the given
jtulach@1334
   730
     * locale.
jtulach@1334
   731
     * @param timeStyle a value from 0 to 3 indicating the time format,
jtulach@1334
   732
     * ignored if flags is 2
jtulach@1334
   733
     * @param dateStyle a value from 0 to 3 indicating the time format,
jtulach@1334
   734
     * ignored if flags is 1
jtulach@1334
   735
     * @param flags either 1 for a time format, 2 for a date format,
jtulach@1334
   736
     * or 3 for a date/time format
jtulach@1334
   737
     * @param loc the locale for the format
jtulach@1334
   738
     */
jtulach@1334
   739
    private static DateFormat get(int timeStyle, int dateStyle,
jtulach@1334
   740
                                  int flags, Locale loc) {
jtulach@1334
   741
        if ((flags & 1) != 0) {
jtulach@1334
   742
            if (timeStyle < 0 || timeStyle > 3) {
jtulach@1334
   743
                throw new IllegalArgumentException("Illegal time style " + timeStyle);
jtulach@1334
   744
            }
jtulach@1334
   745
        } else {
jtulach@1334
   746
            timeStyle = -1;
jtulach@1334
   747
        }
jtulach@1334
   748
        if ((flags & 2) != 0) {
jtulach@1334
   749
            if (dateStyle < 0 || dateStyle > 3) {
jtulach@1334
   750
                throw new IllegalArgumentException("Illegal date style " + dateStyle);
jtulach@1334
   751
            }
jtulach@1334
   752
        } else {
jtulach@1334
   753
            dateStyle = -1;
jtulach@1334
   754
        }
jtulach@1334
   755
        try {
jtulach@1334
   756
            // Check whether a provider can provide an implementation that's closer
jtulach@1334
   757
            // to the requested locale than what the Java runtime itself can provide.
jtulach@1334
   758
            LocaleServiceProviderPool pool =
jtulach@1334
   759
                LocaleServiceProviderPool.getPool(DateFormatProvider.class);
jtulach@1334
   760
            if (pool.hasProviders()) {
jtulach@1334
   761
                DateFormat providersInstance = pool.getLocalizedObject(
jtulach@1334
   762
                                                    DateFormatGetter.INSTANCE,
jtulach@1334
   763
                                                    loc,
jtulach@1334
   764
                                                    timeStyle,
jtulach@1334
   765
                                                    dateStyle,
jtulach@1334
   766
                                                    flags);
jtulach@1334
   767
                if (providersInstance != null) {
jtulach@1334
   768
                    return providersInstance;
jtulach@1334
   769
                }
jtulach@1334
   770
            }
jtulach@1334
   771
jtulach@1334
   772
            return new SimpleDateFormat(timeStyle, dateStyle, loc);
jtulach@1334
   773
        } catch (MissingResourceException e) {
jtulach@1334
   774
            return new SimpleDateFormat("M/d/yy h:mm a");
jtulach@1334
   775
        }
jtulach@1334
   776
    }
jtulach@1334
   777
jtulach@1334
   778
    /**
jtulach@1334
   779
     * Create a new date format.
jtulach@1334
   780
     */
jtulach@1334
   781
    protected DateFormat() {}
jtulach@1334
   782
jtulach@1334
   783
    /**
jtulach@1334
   784
     * Defines constants that are used as attribute keys in the
jtulach@1334
   785
     * <code>AttributedCharacterIterator</code> returned
jtulach@1334
   786
     * from <code>DateFormat.formatToCharacterIterator</code> and as
jtulach@1334
   787
     * field identifiers in <code>FieldPosition</code>.
jtulach@1334
   788
     * <p>
jtulach@1334
   789
     * The class also provides two methods to map
jtulach@1334
   790
     * between its constants and the corresponding Calendar constants.
jtulach@1334
   791
     *
jtulach@1334
   792
     * @since 1.4
jtulach@1334
   793
     * @see java.util.Calendar
jtulach@1334
   794
     */
jtulach@1334
   795
    public static class Field extends Format.Field {
jtulach@1334
   796
jtulach@1334
   797
        // Proclaim serial compatibility with 1.4 FCS
jtulach@1334
   798
        private static final long serialVersionUID = 7441350119349544720L;
jtulach@1334
   799
jtulach@1334
   800
        // table of all instances in this class, used by readResolve
jtulach@1334
   801
        private static final Map instanceMap = new HashMap(18);
jtulach@1334
   802
        // Maps from Calendar constant (such as Calendar.ERA) to Field
jtulach@1334
   803
        // constant (such as Field.ERA).
jtulach@1334
   804
        private static final Field[] calendarToFieldMapping =
jtulach@1334
   805
                                             new Field[Calendar.FIELD_COUNT];
jtulach@1334
   806
jtulach@1334
   807
        /** Calendar field. */
jtulach@1334
   808
        private int calendarField;
jtulach@1334
   809
jtulach@1334
   810
        /**
jtulach@1334
   811
         * Returns the <code>Field</code> constant that corresponds to
jtulach@1334
   812
         * the <code>Calendar</code> constant <code>calendarField</code>.
jtulach@1334
   813
         * If there is no direct mapping between the <code>Calendar</code>
jtulach@1334
   814
         * constant and a <code>Field</code>, null is returned.
jtulach@1334
   815
         *
jtulach@1334
   816
         * @throws IllegalArgumentException if <code>calendarField</code> is
jtulach@1334
   817
         *         not the value of a <code>Calendar</code> field constant.
jtulach@1334
   818
         * @param calendarField Calendar field constant
jtulach@1334
   819
         * @return Field instance representing calendarField.
jtulach@1334
   820
         * @see java.util.Calendar
jtulach@1334
   821
         */
jtulach@1334
   822
        public static Field ofCalendarField(int calendarField) {
jtulach@1334
   823
            if (calendarField < 0 || calendarField >=
jtulach@1334
   824
                        calendarToFieldMapping.length) {
jtulach@1334
   825
                throw new IllegalArgumentException("Unknown Calendar constant "
jtulach@1334
   826
                                                   + calendarField);
jtulach@1334
   827
            }
jtulach@1334
   828
            return calendarToFieldMapping[calendarField];
jtulach@1334
   829
        }
jtulach@1334
   830
jtulach@1334
   831
        /**
jtulach@1334
   832
         * Creates a <code>Field</code>.
jtulach@1334
   833
         *
jtulach@1334
   834
         * @param name the name of the <code>Field</code>
jtulach@1334
   835
         * @param calendarField the <code>Calendar</code> constant this
jtulach@1334
   836
         *        <code>Field</code> corresponds to; any value, even one
jtulach@1334
   837
         *        outside the range of legal <code>Calendar</code> values may
jtulach@1334
   838
         *        be used, but <code>-1</code> should be used for values
jtulach@1334
   839
         *        that don't correspond to legal <code>Calendar</code> values
jtulach@1334
   840
         */
jtulach@1334
   841
        protected Field(String name, int calendarField) {
jtulach@1334
   842
            super(name);
jtulach@1334
   843
            this.calendarField = calendarField;
jtulach@1334
   844
            if (this.getClass() == DateFormat.Field.class) {
jtulach@1334
   845
                instanceMap.put(name, this);
jtulach@1334
   846
                if (calendarField >= 0) {
jtulach@1334
   847
                    // assert(calendarField < Calendar.FIELD_COUNT);
jtulach@1334
   848
                    calendarToFieldMapping[calendarField] = this;
jtulach@1334
   849
                }
jtulach@1334
   850
            }
jtulach@1334
   851
        }
jtulach@1334
   852
jtulach@1334
   853
        /**
jtulach@1334
   854
         * Returns the <code>Calendar</code> field associated with this
jtulach@1334
   855
         * attribute. For example, if this represents the hours field of
jtulach@1334
   856
         * a <code>Calendar</code>, this would return
jtulach@1334
   857
         * <code>Calendar.HOUR</code>. If there is no corresponding
jtulach@1334
   858
         * <code>Calendar</code> constant, this will return -1.
jtulach@1334
   859
         *
jtulach@1334
   860
         * @return Calendar constant for this field
jtulach@1334
   861
         * @see java.util.Calendar
jtulach@1334
   862
         */
jtulach@1334
   863
        public int getCalendarField() {
jtulach@1334
   864
            return calendarField;
jtulach@1334
   865
        }
jtulach@1334
   866
jtulach@1334
   867
        /**
jtulach@1334
   868
         * Resolves instances being deserialized to the predefined constants.
jtulach@1334
   869
         *
jtulach@1334
   870
         * @throws InvalidObjectException if the constant could not be
jtulach@1334
   871
         *         resolved.
jtulach@1334
   872
         * @return resolved DateFormat.Field constant
jtulach@1334
   873
         */
jtulach@1334
   874
        protected Object readResolve() throws InvalidObjectException {
jtulach@1334
   875
            if (this.getClass() != DateFormat.Field.class) {
jtulach@1334
   876
                throw new InvalidObjectException("subclass didn't correctly implement readResolve");
jtulach@1334
   877
            }
jtulach@1334
   878
jtulach@1334
   879
            Object instance = instanceMap.get(getName());
jtulach@1334
   880
            if (instance != null) {
jtulach@1334
   881
                return instance;
jtulach@1334
   882
            } else {
jtulach@1334
   883
                throw new InvalidObjectException("unknown attribute name");
jtulach@1334
   884
            }
jtulach@1334
   885
        }
jtulach@1334
   886
jtulach@1334
   887
        //
jtulach@1334
   888
        // The constants
jtulach@1334
   889
        //
jtulach@1334
   890
jtulach@1334
   891
        /**
jtulach@1334
   892
         * Constant identifying the era field.
jtulach@1334
   893
         */
jtulach@1334
   894
        public final static Field ERA = new Field("era", Calendar.ERA);
jtulach@1334
   895
jtulach@1334
   896
        /**
jtulach@1334
   897
         * Constant identifying the year field.
jtulach@1334
   898
         */
jtulach@1334
   899
        public final static Field YEAR = new Field("year", Calendar.YEAR);
jtulach@1334
   900
jtulach@1334
   901
        /**
jtulach@1334
   902
         * Constant identifying the month field.
jtulach@1334
   903
         */
jtulach@1334
   904
        public final static Field MONTH = new Field("month", Calendar.MONTH);
jtulach@1334
   905
jtulach@1334
   906
        /**
jtulach@1334
   907
         * Constant identifying the day of month field.
jtulach@1334
   908
         */
jtulach@1334
   909
        public final static Field DAY_OF_MONTH = new
jtulach@1334
   910
                            Field("day of month", Calendar.DAY_OF_MONTH);
jtulach@1334
   911
jtulach@1334
   912
        /**
jtulach@1334
   913
         * Constant identifying the hour of day field, where the legal values
jtulach@1334
   914
         * are 1 to 24.
jtulach@1334
   915
         */
jtulach@1334
   916
        public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1);
jtulach@1334
   917
jtulach@1334
   918
        /**
jtulach@1334
   919
         * Constant identifying the hour of day field, where the legal values
jtulach@1334
   920
         * are 0 to 23.
jtulach@1334
   921
         */
jtulach@1334
   922
        public final static Field HOUR_OF_DAY0 = new
jtulach@1334
   923
               Field("hour of day", Calendar.HOUR_OF_DAY);
jtulach@1334
   924
jtulach@1334
   925
        /**
jtulach@1334
   926
         * Constant identifying the minute field.
jtulach@1334
   927
         */
jtulach@1334
   928
        public final static Field MINUTE =new Field("minute", Calendar.MINUTE);
jtulach@1334
   929
jtulach@1334
   930
        /**
jtulach@1334
   931
         * Constant identifying the second field.
jtulach@1334
   932
         */
jtulach@1334
   933
        public final static Field SECOND =new Field("second", Calendar.SECOND);
jtulach@1334
   934
jtulach@1334
   935
        /**
jtulach@1334
   936
         * Constant identifying the millisecond field.
jtulach@1334
   937
         */
jtulach@1334
   938
        public final static Field MILLISECOND = new
jtulach@1334
   939
                Field("millisecond", Calendar.MILLISECOND);
jtulach@1334
   940
jtulach@1334
   941
        /**
jtulach@1334
   942
         * Constant identifying the day of week field.
jtulach@1334
   943
         */
jtulach@1334
   944
        public final static Field DAY_OF_WEEK = new
jtulach@1334
   945
                Field("day of week", Calendar.DAY_OF_WEEK);
jtulach@1334
   946
jtulach@1334
   947
        /**
jtulach@1334
   948
         * Constant identifying the day of year field.
jtulach@1334
   949
         */
jtulach@1334
   950
        public final static Field DAY_OF_YEAR = new
jtulach@1334
   951
                Field("day of year", Calendar.DAY_OF_YEAR);
jtulach@1334
   952
jtulach@1334
   953
        /**
jtulach@1334
   954
         * Constant identifying the day of week field.
jtulach@1334
   955
         */
jtulach@1334
   956
        public final static Field DAY_OF_WEEK_IN_MONTH =
jtulach@1334
   957
                     new Field("day of week in month",
jtulach@1334
   958
                                            Calendar.DAY_OF_WEEK_IN_MONTH);
jtulach@1334
   959
jtulach@1334
   960
        /**
jtulach@1334
   961
         * Constant identifying the week of year field.
jtulach@1334
   962
         */
jtulach@1334
   963
        public final static Field WEEK_OF_YEAR = new
jtulach@1334
   964
              Field("week of year", Calendar.WEEK_OF_YEAR);
jtulach@1334
   965
jtulach@1334
   966
        /**
jtulach@1334
   967
         * Constant identifying the week of month field.
jtulach@1334
   968
         */
jtulach@1334
   969
        public final static Field WEEK_OF_MONTH = new
jtulach@1334
   970
            Field("week of month", Calendar.WEEK_OF_MONTH);
jtulach@1334
   971
jtulach@1334
   972
        /**
jtulach@1334
   973
         * Constant identifying the time of day indicator
jtulach@1334
   974
         * (e.g. "a.m." or "p.m.") field.
jtulach@1334
   975
         */
jtulach@1334
   976
        public final static Field AM_PM = new
jtulach@1334
   977
                            Field("am pm", Calendar.AM_PM);
jtulach@1334
   978
jtulach@1334
   979
        /**
jtulach@1334
   980
         * Constant identifying the hour field, where the legal values are
jtulach@1334
   981
         * 1 to 12.
jtulach@1334
   982
         */
jtulach@1334
   983
        public final static Field HOUR1 = new Field("hour 1", -1);
jtulach@1334
   984
jtulach@1334
   985
        /**
jtulach@1334
   986
         * Constant identifying the hour field, where the legal values are
jtulach@1334
   987
         * 0 to 11.
jtulach@1334
   988
         */
jtulach@1334
   989
        public final static Field HOUR0 = new
jtulach@1334
   990
                            Field("hour", Calendar.HOUR);
jtulach@1334
   991
jtulach@1334
   992
        /**
jtulach@1334
   993
         * Constant identifying the time zone field.
jtulach@1334
   994
         */
jtulach@1334
   995
        public final static Field TIME_ZONE = new Field("time zone", -1);
jtulach@1334
   996
    }
jtulach@1334
   997
jtulach@1334
   998
    /**
jtulach@1334
   999
     * Obtains a DateFormat instance from a DateFormatProvider
jtulach@1334
  1000
     * implementation.
jtulach@1334
  1001
     */
jtulach@1334
  1002
    private static class DateFormatGetter
jtulach@1334
  1003
        implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatProvider, DateFormat> {
jtulach@1334
  1004
        private static final DateFormatGetter INSTANCE = new DateFormatGetter();
jtulach@1334
  1005
jtulach@1334
  1006
        public DateFormat getObject(DateFormatProvider dateFormatProvider,
jtulach@1334
  1007
                                Locale locale,
jtulach@1334
  1008
                                String key,
jtulach@1334
  1009
                                Object... params) {
jtulach@1334
  1010
            assert params.length == 3;
jtulach@1334
  1011
jtulach@1334
  1012
            int timeStyle = (Integer)params[0];
jtulach@1334
  1013
            int dateStyle = (Integer)params[1];
jtulach@1334
  1014
            int flags = (Integer)params[2];
jtulach@1334
  1015
jtulach@1334
  1016
            switch (flags) {
jtulach@1334
  1017
            case 1:
jtulach@1334
  1018
                return dateFormatProvider.getTimeInstance(timeStyle, locale);
jtulach@1334
  1019
            case 2:
jtulach@1334
  1020
                return dateFormatProvider.getDateInstance(dateStyle, locale);
jtulach@1334
  1021
            case 3:
jtulach@1334
  1022
                return dateFormatProvider.getDateTimeInstance(dateStyle, timeStyle, locale);
jtulach@1334
  1023
            default:
jtulach@1334
  1024
                assert false : "should not happen";
jtulach@1334
  1025
            }
jtulach@1334
  1026
jtulach@1334
  1027
            return null;
jtulach@1334
  1028
        }
jtulach@1334
  1029
    }
jtulach@1334
  1030
}