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