0N/A/*
3261N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
0N/A * (C) Copyright IBM Corp. 1996 - All Rights Reserved
0N/A *
0N/A * The original version of this source code and documentation is copyrighted
0N/A * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
0N/A * materials are provided under terms of a License Agreement between Taligent
0N/A * and Sun. This technology is protected by multiple US and International
0N/A * patents. This notice and attribution to Taligent may not be removed.
0N/A * Taligent is a registered trademark of Taligent, Inc.
0N/A *
0N/A */
0N/A
0N/Apackage java.text;
0N/A
0N/Aimport java.io.InvalidObjectException;
0N/Aimport java.text.spi.DateFormatProvider;
0N/Aimport java.util.Calendar;
0N/Aimport java.util.Date;
0N/Aimport java.util.GregorianCalendar;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.TimeZone;
0N/Aimport java.util.spi.LocaleServiceProvider;
0N/Aimport sun.util.LocaleServiceProviderPool;
0N/A
0N/A/**
2293N/A * {@code DateFormat} is an abstract class for date/time formatting subclasses which
0N/A * formats and parses dates or time in a language-independent manner.
2293N/A * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
0N/A * formatting (i.e., date -> text), parsing (text -> date), and
0N/A * normalization. The date is represented as a <code>Date</code> object or
0N/A * as the milliseconds since January 1, 1970, 00:00:00 GMT.
0N/A *
2293N/A * <p>{@code DateFormat} provides many class methods for obtaining default date/time
0N/A * formatters based on the default or a given locale and a number of formatting
2293N/A * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
0N/A * detail and examples of using these styles are provided in the method
0N/A * descriptions.
0N/A *
2293N/A * <p>{@code DateFormat} helps you to format and parse dates for any locale.
0N/A * Your code can be completely independent of the locale conventions for
0N/A * months, days of the week, or even the calendar format: lunar vs. solar.
0N/A *
0N/A * <p>To format a date for the current Locale, use one of the
0N/A * static factory methods:
0N/A * <pre>
0N/A * myString = DateFormat.getDateInstance().format(myDate);
0N/A * </pre>
0N/A * <p>If you are formatting multiple dates, it is
0N/A * more efficient to get the format and use it multiple times so that
0N/A * the system doesn't have to fetch the information about the local
0N/A * language and country conventions multiple times.
0N/A * <pre>
0N/A * DateFormat df = DateFormat.getDateInstance();
0N/A * for (int i = 0; i < myDate.length; ++i) {
0N/A * output.println(df.format(myDate[i]) + "; ");
0N/A * }
0N/A * </pre>
0N/A * <p>To format a date for a different Locale, specify it in the
2293N/A * call to {@link #getDateInstance(int, Locale) getDateInstance()}.
0N/A * <pre>
0N/A * DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
0N/A * </pre>
0N/A * <p>You can use a DateFormat to parse also.
0N/A * <pre>
0N/A * myDate = df.parse(myString);
0N/A * </pre>
2293N/A * <p>Use {@code getDateInstance} to get the normal date format for that country.
0N/A * There are other static factory methods available.
2293N/A * Use {@code getTimeInstance} to get the time format for that country.
2293N/A * Use {@code getDateTimeInstance} to get a date and time format. You can pass in
0N/A * different options to these factory methods to control the length of the
2293N/A * result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}. The exact result depends
0N/A * on the locale, but generally:
2293N/A * <ul><li>{@link #SHORT} is completely numeric, such as {@code 12.13.52} or {@code 3:30pm}
2293N/A * <li>{@link #MEDIUM} is longer, such as {@code Jan 12, 1952}
2293N/A * <li>{@link #LONG} is longer, such as {@code January 12, 1952} or {@code 3:30:32pm}
2293N/A * <li>{@link #FULL} is pretty completely specified, such as
2293N/A * {@code Tuesday, April 12, 1952 AD or 3:30:42pm PST}.
0N/A * </ul>
0N/A *
0N/A * <p>You can also set the time zone on the format if you wish.
0N/A * If you want even more control over the format or parsing,
0N/A * (or want to give your users more control),
2293N/A * you can try casting the {@code DateFormat} you get from the factory methods
2293N/A * to a {@link SimpleDateFormat}. This will work for the majority
2293N/A * of countries; just remember to put it in a {@code try} block in case you
0N/A * encounter an unusual one.
0N/A *
0N/A * <p>You can also use forms of the parse and format methods with
2293N/A * {@link ParsePosition} and {@link FieldPosition} to
0N/A * allow you to
0N/A * <ul><li>progressively parse through pieces of a string.
0N/A * <li>align any particular field, or find out where it is for selection
0N/A * on the screen.
0N/A * </ul>
0N/A *
0N/A * <h4><a name="synchronization">Synchronization</a></h4>
0N/A *
0N/A * <p>
0N/A * Date formats are not synchronized.
0N/A * It is recommended to create separate format instances for each thread.
0N/A * If multiple threads access a format concurrently, it must be synchronized
0N/A * externally.
0N/A *
0N/A * @see Format
0N/A * @see NumberFormat
0N/A * @see SimpleDateFormat
0N/A * @see java.util.Calendar
0N/A * @see java.util.GregorianCalendar
0N/A * @see java.util.TimeZone
0N/A * @author Mark Davis, Chen-Lieh Huang, Alan Liu
0N/A */
0N/Apublic abstract class DateFormat extends Format {
0N/A
0N/A /**
2293N/A * The {@link Calendar} instance used for calculating the date-time fields
2293N/A * and the instant of time. This field is used for both formatting and
2293N/A * parsing.
2293N/A *
2293N/A * <p>Subclasses should initialize this field to a {@link Calendar}
2293N/A * appropriate for the {@link Locale} associated with this
2293N/A * <code>DateFormat</code>.
0N/A * @serial
0N/A */
0N/A protected Calendar calendar;
0N/A
0N/A /**
0N/A * The number formatter that <code>DateFormat</code> uses to format numbers
0N/A * in dates and times. Subclasses should initialize this to a number format
0N/A * appropriate for the locale associated with this <code>DateFormat</code>.
0N/A * @serial
0N/A */
0N/A protected NumberFormat numberFormat;
0N/A
0N/A /**
0N/A * Useful constant for ERA field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int ERA_FIELD = 0;
0N/A /**
0N/A * Useful constant for YEAR field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int YEAR_FIELD = 1;
0N/A /**
0N/A * Useful constant for MONTH field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int MONTH_FIELD = 2;
0N/A /**
0N/A * Useful constant for DATE field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int DATE_FIELD = 3;
0N/A /**
0N/A * Useful constant for one-based HOUR_OF_DAY field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
0N/A * For example, 23:59 + 01:00 results in 24:59.
0N/A */
0N/A public final static int HOUR_OF_DAY1_FIELD = 4;
0N/A /**
0N/A * Useful constant for zero-based HOUR_OF_DAY field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
0N/A * For example, 23:59 + 01:00 results in 00:59.
0N/A */
0N/A public final static int HOUR_OF_DAY0_FIELD = 5;
0N/A /**
0N/A * Useful constant for MINUTE field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int MINUTE_FIELD = 6;
0N/A /**
0N/A * Useful constant for SECOND field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int SECOND_FIELD = 7;
0N/A /**
0N/A * Useful constant for MILLISECOND field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int MILLISECOND_FIELD = 8;
0N/A /**
0N/A * Useful constant for DAY_OF_WEEK field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int DAY_OF_WEEK_FIELD = 9;
0N/A /**
0N/A * Useful constant for DAY_OF_YEAR field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int DAY_OF_YEAR_FIELD = 10;
0N/A /**
0N/A * Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
0N/A /**
0N/A * Useful constant for WEEK_OF_YEAR field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int WEEK_OF_YEAR_FIELD = 12;
0N/A /**
0N/A * Useful constant for WEEK_OF_MONTH field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int WEEK_OF_MONTH_FIELD = 13;
0N/A /**
0N/A * Useful constant for AM_PM field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int AM_PM_FIELD = 14;
0N/A /**
0N/A * Useful constant for one-based HOUR field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A * HOUR1_FIELD is used for the one-based 12-hour clock.
0N/A * For example, 11:30 PM + 1 hour results in 12:30 AM.
0N/A */
0N/A public final static int HOUR1_FIELD = 15;
0N/A /**
0N/A * Useful constant for zero-based HOUR field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A * HOUR0_FIELD is used for the zero-based 12-hour clock.
0N/A * For example, 11:30 PM + 1 hour results in 00:30 AM.
0N/A */
0N/A public final static int HOUR0_FIELD = 16;
0N/A /**
0N/A * Useful constant for TIMEZONE field alignment.
0N/A * Used in FieldPosition of date/time formatting.
0N/A */
0N/A public final static int TIMEZONE_FIELD = 17;
0N/A
0N/A // Proclaim serial compatibility with 1.1 FCS
0N/A private static final long serialVersionUID = 7218322306649953788L;
0N/A
0N/A /**
0N/A * Overrides Format.
0N/A * Formats a time object into a time string. Examples of time objects
0N/A * are a time value expressed in milliseconds and a Date object.
0N/A * @param obj must be a Number or a Date.
0N/A * @param toAppendTo the string buffer for the returning time string.
0N/A * @return the string buffer passed in as toAppendTo, with formatted text appended.
0N/A * @param fieldPosition keeps track of the position of the field
0N/A * within the returned string.
0N/A * On input: an alignment field,
0N/A * if desired. On output: the offsets of the alignment field. For
0N/A * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
0N/A * if the given fieldPosition is DateFormat.YEAR_FIELD, the
0N/A * begin index and end index of fieldPosition will be set to
0N/A * 0 and 4, respectively.
0N/A * Notice that if the same time field appears
0N/A * more than once in a pattern, the fieldPosition will be set for the first
0N/A * occurrence of that time field. For instance, formatting a Date to
0N/A * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
0N/A * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
0N/A * the begin index and end index of fieldPosition will be set to
0N/A * 5 and 8, respectively, for the first occurrence of the timezone
0N/A * pattern character 'z'.
0N/A * @see java.text.Format
0N/A */
0N/A public final StringBuffer format(Object obj, StringBuffer toAppendTo,
0N/A FieldPosition fieldPosition)
0N/A {
0N/A if (obj instanceof Date)
0N/A return format( (Date)obj, toAppendTo, fieldPosition );
0N/A else if (obj instanceof Number)
0N/A return format( new Date(((Number)obj).longValue()),
0N/A toAppendTo, fieldPosition );
0N/A else
0N/A throw new IllegalArgumentException("Cannot format given Object as a Date");
0N/A }
0N/A
0N/A /**
0N/A * Formats a Date into a date/time string.
0N/A * @param date a Date to be formatted into a date/time string.
0N/A * @param toAppendTo the string buffer for the returning date/time string.
0N/A * @param fieldPosition keeps track of the position of the field
0N/A * within the returned string.
0N/A * On input: an alignment field,
0N/A * if desired. On output: the offsets of the alignment field. For
0N/A * example, given a time text "1996.07.10 AD at 15:08:56 PDT",
0N/A * if the given fieldPosition is DateFormat.YEAR_FIELD, the
0N/A * begin index and end index of fieldPosition will be set to
0N/A * 0 and 4, respectively.
0N/A * Notice that if the same time field appears
0N/A * more than once in a pattern, the fieldPosition will be set for the first
0N/A * occurrence of that time field. For instance, formatting a Date to
0N/A * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern
0N/A * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,
0N/A * the begin index and end index of fieldPosition will be set to
0N/A * 5 and 8, respectively, for the first occurrence of the timezone
0N/A * pattern character 'z'.
0N/A * @return the string buffer passed in as toAppendTo, with formatted text appended.
0N/A */
0N/A public abstract StringBuffer format(Date date, StringBuffer toAppendTo,
0N/A FieldPosition fieldPosition);
0N/A
0N/A /**
0N/A * Formats a Date into a date/time string.
0N/A * @param date the time value to be formatted into a time string.
0N/A * @return the formatted time string.
0N/A */
0N/A public final String format(Date date)
0N/A {
0N/A return format(date, new StringBuffer(),
0N/A DontCareFieldPosition.INSTANCE).toString();
0N/A }
0N/A
0N/A /**
0N/A * Parses text from the beginning of the given string to produce a date.
0N/A * The method may not use the entire text of the given string.
0N/A * <p>
0N/A * See the {@link #parse(String, ParsePosition)} method for more information
0N/A * on date parsing.
0N/A *
0N/A * @param source A <code>String</code> whose beginning should be parsed.
0N/A * @return A <code>Date</code> parsed from the string.
0N/A * @exception ParseException if the beginning of the specified string
0N/A * cannot be parsed.
0N/A */
0N/A public Date parse(String source) throws ParseException
0N/A {
0N/A ParsePosition pos = new ParsePosition(0);
0N/A Date result = parse(source, pos);
0N/A if (pos.index == 0)
0N/A throw new ParseException("Unparseable date: \"" + source + "\"" ,
0N/A pos.errorIndex);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Parse a date/time string according to the given parse position. For
2293N/A * example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date}
2293N/A * that is equivalent to {@code Date(837039900000L)}.
0N/A *
0N/A * <p> By default, parsing is lenient: If the input is not in the form used
0N/A * by this object's format method but can still be parsed as a date, then
0N/A * the parse succeeds. Clients may insist on strict adherence to the
2293N/A * format by calling {@link #setLenient(boolean) setLenient(false)}.
0N/A *
2293N/A * <p>This parsing operation uses the {@link #calendar} to produce
2293N/A * a {@code Date}. As a result, the {@code calendar}'s date-time
2293N/A * fields and the {@code TimeZone} value may have been
2293N/A * overwritten, depending on subclass implementations. Any {@code
2293N/A * TimeZone} value that has previously been set by a call to
2293N/A * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
2293N/A * to be restored for further operations.
0N/A *
0N/A * @param source The date/time string to be parsed
0N/A *
0N/A * @param pos On input, the position at which to start parsing; on
0N/A * output, the position at which parsing terminated, or the
0N/A * start position if the parse failed.
0N/A *
2293N/A * @return A {@code Date}, or {@code null} if the input could not be parsed
0N/A */
0N/A public abstract Date parse(String source, ParsePosition pos);
0N/A
0N/A /**
0N/A * Parses text from a string to produce a <code>Date</code>.
0N/A * <p>
0N/A * The method attempts to parse text starting at the index given by
0N/A * <code>pos</code>.
0N/A * If parsing succeeds, then the index of <code>pos</code> is updated
0N/A * to the index after the last character used (parsing does not necessarily
0N/A * use all characters up to the end of the string), and the parsed
0N/A * date is returned. The updated <code>pos</code> can be used to
0N/A * indicate the starting point for the next call to this method.
0N/A * If an error occurs, then the index of <code>pos</code> is not
0N/A * changed, the error index of <code>pos</code> is set to the index of
0N/A * the character where the error occurred, and null is returned.
0N/A * <p>
0N/A * See the {@link #parse(String, ParsePosition)} method for more information
0N/A * on date parsing.
0N/A *
0N/A * @param source A <code>String</code>, part of which should be parsed.
0N/A * @param pos A <code>ParsePosition</code> object with index and error
0N/A * index information as described above.
0N/A * @return A <code>Date</code> parsed from the string. In case of
0N/A * error, returns null.
0N/A * @exception NullPointerException if <code>pos</code> is null.
0N/A */
0N/A public Object parseObject(String source, ParsePosition pos) {
0N/A return parse(source, pos);
0N/A }
0N/A
0N/A /**
0N/A * Constant for full style pattern.
0N/A */
0N/A public static final int FULL = 0;
0N/A /**
0N/A * Constant for long style pattern.
0N/A */
0N/A public static final int LONG = 1;
0N/A /**
0N/A * Constant for medium style pattern.
0N/A */
0N/A public static final int MEDIUM = 2;
0N/A /**
0N/A * Constant for short style pattern.
0N/A */
0N/A public static final int SHORT = 3;
0N/A /**
0N/A * Constant for default style pattern. Its value is MEDIUM.
0N/A */
0N/A public static final int DEFAULT = MEDIUM;
0N/A
0N/A /**
0N/A * Gets the time formatter with the default formatting style
0N/A * for the default locale.
0N/A * @return a time formatter.
0N/A */
0N/A public final static DateFormat getTimeInstance()
0N/A {
2700N/A return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the time formatter with the given formatting style
0N/A * for the default locale.
0N/A * @param style the given formatting style. For example,
0N/A * SHORT for "h:mm a" in the US locale.
0N/A * @return a time formatter.
0N/A */
0N/A public final static DateFormat getTimeInstance(int style)
0N/A {
2700N/A return get(style, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the time formatter with the given formatting style
0N/A * for the given locale.
0N/A * @param style the given formatting style. For example,
0N/A * SHORT for "h:mm a" in the US locale.
0N/A * @param aLocale the given locale.
0N/A * @return a time formatter.
0N/A */
0N/A public final static DateFormat getTimeInstance(int style,
0N/A Locale aLocale)
0N/A {
0N/A return get(style, 0, 1, aLocale);
0N/A }
0N/A
0N/A /**
0N/A * Gets the date formatter with the default formatting style
0N/A * for the default locale.
0N/A * @return a date formatter.
0N/A */
0N/A public final static DateFormat getDateInstance()
0N/A {
2700N/A return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the date formatter with the given formatting style
0N/A * for the default locale.
0N/A * @param style the given formatting style. For example,
0N/A * SHORT for "M/d/yy" in the US locale.
0N/A * @return a date formatter.
0N/A */
0N/A public final static DateFormat getDateInstance(int style)
0N/A {
2700N/A return get(0, style, 2, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the date formatter with the given formatting style
0N/A * for the given locale.
0N/A * @param style the given formatting style. For example,
0N/A * SHORT for "M/d/yy" in the US locale.
0N/A * @param aLocale the given locale.
0N/A * @return a date formatter.
0N/A */
0N/A public final static DateFormat getDateInstance(int style,
0N/A Locale aLocale)
0N/A {
0N/A return get(0, style, 2, aLocale);
0N/A }
0N/A
0N/A /**
0N/A * Gets the date/time formatter with the default formatting style
0N/A * for the default locale.
0N/A * @return a date/time formatter.
0N/A */
0N/A public final static DateFormat getDateTimeInstance()
0N/A {
2700N/A return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the date/time formatter with the given date and time
0N/A * formatting styles for the default locale.
0N/A * @param dateStyle the given date formatting style. For example,
0N/A * SHORT for "M/d/yy" in the US locale.
0N/A * @param timeStyle the given time formatting style. For example,
0N/A * SHORT for "h:mm a" in the US locale.
0N/A * @return a date/time formatter.
0N/A */
0N/A public final static DateFormat getDateTimeInstance(int dateStyle,
0N/A int timeStyle)
0N/A {
2700N/A return get(timeStyle, dateStyle, 3, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Gets the date/time formatter with the given formatting styles
0N/A * for the given locale.
0N/A * @param dateStyle the given date formatting style.
0N/A * @param timeStyle the given time formatting style.
0N/A * @param aLocale the given locale.
0N/A * @return a date/time formatter.
0N/A */
0N/A public final static DateFormat
0N/A getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
0N/A {
0N/A return get(timeStyle, dateStyle, 3, aLocale);
0N/A }
0N/A
0N/A /**
0N/A * Get a default date/time formatter that uses the SHORT style for both the
0N/A * date and the time.
0N/A */
0N/A public final static DateFormat getInstance() {
0N/A return getDateTimeInstance(SHORT, SHORT);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all locales for which the
0N/A * <code>get*Instance</code> methods of this class can return
0N/A * localized instances.
0N/A * The returned array represents the union of locales supported by the Java
0N/A * runtime and by installed
0N/A * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
0N/A * It must contain at least a <code>Locale</code> instance equal to
0N/A * {@link java.util.Locale#US Locale.US}.
0N/A *
0N/A * @return An array of locales for which localized
0N/A * <code>DateFormat</code> instances are available.
0N/A */
0N/A public static Locale[] getAvailableLocales()
0N/A {
0N/A LocaleServiceProviderPool pool =
0N/A LocaleServiceProviderPool.getPool(DateFormatProvider.class);
0N/A return pool.getAvailableLocales();
0N/A }
0N/A
0N/A /**
0N/A * Set the calendar to be used by this date format. Initially, the default
0N/A * calendar for the specified or default locale is used.
2293N/A *
2293N/A * <p>Any {@link java.util.TimeZone TimeZone} and {@linkplain
2293N/A * #isLenient() leniency} values that have previously been set are
2293N/A * overwritten by {@code newCalendar}'s values.
2293N/A *
2293N/A * @param newCalendar the new {@code Calendar} to be used by the date format
0N/A */
0N/A public void setCalendar(Calendar newCalendar)
0N/A {
0N/A this.calendar = newCalendar;
0N/A }
0N/A
0N/A /**
0N/A * Gets the calendar associated with this date/time formatter.
2293N/A *
0N/A * @return the calendar associated with this date/time formatter.
0N/A */
0N/A public Calendar getCalendar()
0N/A {
0N/A return calendar;
0N/A }
0N/A
0N/A /**
0N/A * Allows you to set the number formatter.
0N/A * @param newNumberFormat the given new NumberFormat.
0N/A */
0N/A public void setNumberFormat(NumberFormat newNumberFormat)
0N/A {
0N/A this.numberFormat = newNumberFormat;
0N/A }
0N/A
0N/A /**
0N/A * Gets the number formatter which this date/time formatter uses to
0N/A * format and parse a time.
0N/A * @return the number formatter which this date/time formatter uses.
0N/A */
0N/A public NumberFormat getNumberFormat()
0N/A {
0N/A return numberFormat;
0N/A }
0N/A
0N/A /**
2293N/A * Sets the time zone for the calendar of this {@code DateFormat} object.
2293N/A * This method is equivalent to the following call.
2293N/A * <blockquote><pre>
2293N/A * getCalendar().setTimeZone(zone)
2293N/A * </pre></blockquote>
2293N/A *
2293N/A * <p>The {@code TimeZone} set by this method is overwritten by a
2293N/A * {@link #setCalendar(java.util.Calendar) setCalendar} call.
2293N/A *
2293N/A * <p>The {@code TimeZone} set by this method may be overwritten as
2293N/A * a result of a call to the parse method.
2293N/A *
0N/A * @param zone the given new time zone.
0N/A */
0N/A public void setTimeZone(TimeZone zone)
0N/A {
0N/A calendar.setTimeZone(zone);
0N/A }
0N/A
0N/A /**
0N/A * Gets the time zone.
2293N/A * This method is equivalent to the following call.
2293N/A * <blockquote><pre>
2293N/A * getCalendar().getTimeZone()
2293N/A * </pre></blockquote>
2293N/A *
0N/A * @return the time zone associated with the calendar of DateFormat.
0N/A */
0N/A public TimeZone getTimeZone()
0N/A {
0N/A return calendar.getTimeZone();
0N/A }
0N/A
0N/A /**
0N/A * Specify whether or not date/time parsing is to be lenient. With
0N/A * lenient parsing, the parser may use heuristics to interpret inputs that
0N/A * do not precisely match this object's format. With strict parsing,
0N/A * inputs must match this object's format.
2293N/A *
2293N/A * <p>This method is equivalent to the following call.
2293N/A * <blockquote><pre>
2293N/A * getCalendar().setLenient(lenient)
2293N/A * </pre></blockquote>
2293N/A *
2293N/A * <p>This leniency value is overwritten by a call to {@link
2293N/A * #setCalendar(java.util.Calendar) setCalendar()}.
2293N/A *
2293N/A * @param lenient when {@code true}, parsing is lenient
2293N/A * @see java.util.Calendar#setLenient(boolean)
0N/A */
0N/A public void setLenient(boolean lenient)
0N/A {
0N/A calendar.setLenient(lenient);
0N/A }
0N/A
0N/A /**
0N/A * Tell whether date/time parsing is to be lenient.
2293N/A * This method is equivalent to the following call.
2293N/A * <blockquote><pre>
2293N/A * getCalendar().isLenient()
2293N/A * </pre></blockquote>
2293N/A *
2293N/A * @return {@code true} if the {@link #calendar} is lenient;
2293N/A * {@code false} otherwise.
2293N/A * @see java.util.Calendar#isLenient()
0N/A */
0N/A public boolean isLenient()
0N/A {
0N/A return calendar.isLenient();
0N/A }
0N/A
0N/A /**
0N/A * Overrides hashCode
0N/A */
0N/A public int hashCode() {
0N/A return numberFormat.hashCode();
0N/A // just enough fields for a reasonable distribution
0N/A }
0N/A
0N/A /**
0N/A * Overrides equals
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) return true;
0N/A if (obj == null || getClass() != obj.getClass()) return false;
0N/A DateFormat other = (DateFormat) obj;
0N/A return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET!
0N/A calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() &&
0N/A calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() &&
0N/A calendar.isLenient() == other.calendar.isLenient() &&
0N/A calendar.getTimeZone().equals(other.calendar.getTimeZone()) &&
0N/A numberFormat.equals(other.numberFormat));
0N/A }
0N/A
0N/A /**
0N/A * Overrides Cloneable
0N/A */
0N/A public Object clone()
0N/A {
0N/A DateFormat other = (DateFormat) super.clone();
0N/A other.calendar = (Calendar) calendar.clone();
0N/A other.numberFormat = (NumberFormat) numberFormat.clone();
0N/A return other;
0N/A }
0N/A
0N/A /**
0N/A * Creates a DateFormat with the given time and/or date style in the given
0N/A * locale.
0N/A * @param timeStyle a value from 0 to 3 indicating the time format,
0N/A * ignored if flags is 2
0N/A * @param dateStyle a value from 0 to 3 indicating the time format,
0N/A * ignored if flags is 1
0N/A * @param flags either 1 for a time format, 2 for a date format,
0N/A * or 3 for a date/time format
0N/A * @param loc the locale for the format
0N/A */
0N/A private static DateFormat get(int timeStyle, int dateStyle,
0N/A int flags, Locale loc) {
0N/A if ((flags & 1) != 0) {
0N/A if (timeStyle < 0 || timeStyle > 3) {
0N/A throw new IllegalArgumentException("Illegal time style " + timeStyle);
0N/A }
0N/A } else {
0N/A timeStyle = -1;
0N/A }
0N/A if ((flags & 2) != 0) {
0N/A if (dateStyle < 0 || dateStyle > 3) {
0N/A throw new IllegalArgumentException("Illegal date style " + dateStyle);
0N/A }
0N/A } else {
0N/A dateStyle = -1;
0N/A }
0N/A try {
0N/A // Check whether a provider can provide an implementation that's closer
0N/A // to the requested locale than what the Java runtime itself can provide.
0N/A LocaleServiceProviderPool pool =
0N/A LocaleServiceProviderPool.getPool(DateFormatProvider.class);
0N/A if (pool.hasProviders()) {
0N/A DateFormat providersInstance = pool.getLocalizedObject(
0N/A DateFormatGetter.INSTANCE,
0N/A loc,
0N/A timeStyle,
0N/A dateStyle,
0N/A flags);
0N/A if (providersInstance != null) {
0N/A return providersInstance;
0N/A }
0N/A }
0N/A
0N/A return new SimpleDateFormat(timeStyle, dateStyle, loc);
0N/A } catch (MissingResourceException e) {
0N/A return new SimpleDateFormat("M/d/yy h:mm a");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Create a new date format.
0N/A */
0N/A protected DateFormat() {}
0N/A
0N/A /**
0N/A * Defines constants that are used as attribute keys in the
0N/A * <code>AttributedCharacterIterator</code> returned
0N/A * from <code>DateFormat.formatToCharacterIterator</code> and as
0N/A * field identifiers in <code>FieldPosition</code>.
0N/A * <p>
0N/A * The class also provides two methods to map
0N/A * between its constants and the corresponding Calendar constants.
0N/A *
0N/A * @since 1.4
0N/A * @see java.util.Calendar
0N/A */
0N/A public static class Field extends Format.Field {
0N/A
0N/A // Proclaim serial compatibility with 1.4 FCS
0N/A private static final long serialVersionUID = 7441350119349544720L;
0N/A
0N/A // table of all instances in this class, used by readResolve
0N/A private static final Map instanceMap = new HashMap(18);
0N/A // Maps from Calendar constant (such as Calendar.ERA) to Field
0N/A // constant (such as Field.ERA).
0N/A private static final Field[] calendarToFieldMapping =
0N/A new Field[Calendar.FIELD_COUNT];
0N/A
0N/A /** Calendar field. */
0N/A private int calendarField;
0N/A
0N/A /**
0N/A * Returns the <code>Field</code> constant that corresponds to
0N/A * the <code>Calendar</code> constant <code>calendarField</code>.
0N/A * If there is no direct mapping between the <code>Calendar</code>
0N/A * constant and a <code>Field</code>, null is returned.
0N/A *
0N/A * @throws IllegalArgumentException if <code>calendarField</code> is
0N/A * not the value of a <code>Calendar</code> field constant.
0N/A * @param calendarField Calendar field constant
0N/A * @return Field instance representing calendarField.
0N/A * @see java.util.Calendar
0N/A */
0N/A public static Field ofCalendarField(int calendarField) {
0N/A if (calendarField < 0 || calendarField >=
0N/A calendarToFieldMapping.length) {
0N/A throw new IllegalArgumentException("Unknown Calendar constant "
0N/A + calendarField);
0N/A }
0N/A return calendarToFieldMapping[calendarField];
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>Field</code>.
0N/A *
0N/A * @param name the name of the <code>Field</code>
0N/A * @param calendarField the <code>Calendar</code> constant this
0N/A * <code>Field</code> corresponds to; any value, even one
0N/A * outside the range of legal <code>Calendar</code> values may
0N/A * be used, but <code>-1</code> should be used for values
0N/A * that don't correspond to legal <code>Calendar</code> values
0N/A */
0N/A protected Field(String name, int calendarField) {
0N/A super(name);
0N/A this.calendarField = calendarField;
0N/A if (this.getClass() == DateFormat.Field.class) {
0N/A instanceMap.put(name, this);
0N/A if (calendarField >= 0) {
0N/A // assert(calendarField < Calendar.FIELD_COUNT);
0N/A calendarToFieldMapping[calendarField] = this;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Calendar</code> field associated with this
0N/A * attribute. For example, if this represents the hours field of
0N/A * a <code>Calendar</code>, this would return
0N/A * <code>Calendar.HOUR</code>. If there is no corresponding
0N/A * <code>Calendar</code> constant, this will return -1.
0N/A *
0N/A * @return Calendar constant for this field
0N/A * @see java.util.Calendar
0N/A */
0N/A public int getCalendarField() {
0N/A return calendarField;
0N/A }
0N/A
0N/A /**
0N/A * Resolves instances being deserialized to the predefined constants.
0N/A *
0N/A * @throws InvalidObjectException if the constant could not be
0N/A * resolved.
0N/A * @return resolved DateFormat.Field constant
0N/A */
0N/A protected Object readResolve() throws InvalidObjectException {
0N/A if (this.getClass() != DateFormat.Field.class) {
0N/A throw new InvalidObjectException("subclass didn't correctly implement readResolve");
0N/A }
0N/A
0N/A Object instance = instanceMap.get(getName());
0N/A if (instance != null) {
0N/A return instance;
0N/A } else {
0N/A throw new InvalidObjectException("unknown attribute name");
0N/A }
0N/A }
0N/A
0N/A //
0N/A // The constants
0N/A //
0N/A
0N/A /**
0N/A * Constant identifying the era field.
0N/A */
0N/A public final static Field ERA = new Field("era", Calendar.ERA);
0N/A
0N/A /**
0N/A * Constant identifying the year field.
0N/A */
0N/A public final static Field YEAR = new Field("year", Calendar.YEAR);
0N/A
0N/A /**
0N/A * Constant identifying the month field.
0N/A */
0N/A public final static Field MONTH = new Field("month", Calendar.MONTH);
0N/A
0N/A /**
0N/A * Constant identifying the day of month field.
0N/A */
0N/A public final static Field DAY_OF_MONTH = new
0N/A Field("day of month", Calendar.DAY_OF_MONTH);
0N/A
0N/A /**
0N/A * Constant identifying the hour of day field, where the legal values
0N/A * are 1 to 24.
0N/A */
0N/A public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1);
0N/A
0N/A /**
0N/A * Constant identifying the hour of day field, where the legal values
0N/A * are 0 to 23.
0N/A */
0N/A public final static Field HOUR_OF_DAY0 = new
0N/A Field("hour of day", Calendar.HOUR_OF_DAY);
0N/A
0N/A /**
0N/A * Constant identifying the minute field.
0N/A */
0N/A public final static Field MINUTE =new Field("minute", Calendar.MINUTE);
0N/A
0N/A /**
0N/A * Constant identifying the second field.
0N/A */
0N/A public final static Field SECOND =new Field("second", Calendar.SECOND);
0N/A
0N/A /**
0N/A * Constant identifying the millisecond field.
0N/A */
0N/A public final static Field MILLISECOND = new
0N/A Field("millisecond", Calendar.MILLISECOND);
0N/A
0N/A /**
0N/A * Constant identifying the day of week field.
0N/A */
0N/A public final static Field DAY_OF_WEEK = new
0N/A Field("day of week", Calendar.DAY_OF_WEEK);
0N/A
0N/A /**
0N/A * Constant identifying the day of year field.
0N/A */
0N/A public final static Field DAY_OF_YEAR = new
0N/A Field("day of year", Calendar.DAY_OF_YEAR);
0N/A
0N/A /**
0N/A * Constant identifying the day of week field.
0N/A */
0N/A public final static Field DAY_OF_WEEK_IN_MONTH =
0N/A new Field("day of week in month",
0N/A Calendar.DAY_OF_WEEK_IN_MONTH);
0N/A
0N/A /**
0N/A * Constant identifying the week of year field.
0N/A */
0N/A public final static Field WEEK_OF_YEAR = new
0N/A Field("week of year", Calendar.WEEK_OF_YEAR);
0N/A
0N/A /**
0N/A * Constant identifying the week of month field.
0N/A */
0N/A public final static Field WEEK_OF_MONTH = new
0N/A Field("week of month", Calendar.WEEK_OF_MONTH);
0N/A
0N/A /**
0N/A * Constant identifying the time of day indicator
0N/A * (e.g. "a.m." or "p.m.") field.
0N/A */
0N/A public final static Field AM_PM = new
0N/A Field("am pm", Calendar.AM_PM);
0N/A
0N/A /**
0N/A * Constant identifying the hour field, where the legal values are
0N/A * 1 to 12.
0N/A */
0N/A public final static Field HOUR1 = new Field("hour 1", -1);
0N/A
0N/A /**
0N/A * Constant identifying the hour field, where the legal values are
0N/A * 0 to 11.
0N/A */
0N/A public final static Field HOUR0 = new
0N/A Field("hour", Calendar.HOUR);
0N/A
0N/A /**
0N/A * Constant identifying the time zone field.
0N/A */
0N/A public final static Field TIME_ZONE = new Field("time zone", -1);
0N/A }
0N/A
0N/A /**
0N/A * Obtains a DateFormat instance from a DateFormatProvider
0N/A * implementation.
0N/A */
0N/A private static class DateFormatGetter
0N/A implements LocaleServiceProviderPool.LocalizedObjectGetter<DateFormatProvider, DateFormat> {
0N/A private static final DateFormatGetter INSTANCE = new DateFormatGetter();
0N/A
0N/A public DateFormat getObject(DateFormatProvider dateFormatProvider,
0N/A Locale locale,
0N/A String key,
0N/A Object... params) {
0N/A assert params.length == 3;
0N/A
0N/A int timeStyle = (Integer)params[0];
0N/A int dateStyle = (Integer)params[1];
0N/A int flags = (Integer)params[2];
0N/A
0N/A switch (flags) {
0N/A case 1:
0N/A return dateFormatProvider.getTimeInstance(timeStyle, locale);
0N/A case 2:
0N/A return dateFormatProvider.getDateInstance(dateStyle, locale);
0N/A case 3:
0N/A return dateFormatProvider.getDateTimeInstance(dateStyle, timeStyle, locale);
0N/A default:
0N/A assert false : "should not happen";
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A }
0N/A}