jtulach@1334: /* jtulach@1334: * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved jtulach@1334: * (C) Copyright IBM Corp. 1996 - All Rights Reserved jtulach@1334: * jtulach@1334: * The original version of this source code and documentation is copyrighted jtulach@1334: * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These jtulach@1334: * materials are provided under terms of a License Agreement between Taligent jtulach@1334: * and Sun. This technology is protected by multiple US and International jtulach@1334: * patents. This notice and attribution to Taligent may not be removed. jtulach@1334: * Taligent is a registered trademark of Taligent, Inc. jtulach@1334: * jtulach@1334: */ jtulach@1334: jtulach@1334: package java.text; jtulach@1334: jtulach@1334: import java.io.InvalidObjectException; jtulach@1334: import java.text.spi.DateFormatProvider; jtulach@1334: import java.util.Calendar; jtulach@1334: import java.util.Date; jtulach@1334: import java.util.GregorianCalendar; jtulach@1334: import java.util.HashMap; jtulach@1334: import java.util.Locale; jtulach@1334: import java.util.Map; jtulach@1334: import java.util.MissingResourceException; jtulach@1334: import java.util.ResourceBundle; jtulach@1334: import java.util.TimeZone; jtulach@1334: import java.util.spi.LocaleServiceProvider; jtulach@1334: import sun.util.LocaleServiceProviderPool; jtulach@1334: jtulach@1334: /** jtulach@1334: * {@code DateFormat} is an abstract class for date/time formatting subclasses which jtulach@1334: * formats and parses dates or time in a language-independent manner. jtulach@1334: * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for jtulach@1334: * formatting (i.e., date -> text), parsing (text -> date), and jtulach@1334: * normalization. The date is represented as a Date object or jtulach@1334: * as the milliseconds since January 1, 1970, 00:00:00 GMT. jtulach@1334: * jtulach@1334: *

{@code DateFormat} provides many class methods for obtaining default date/time jtulach@1334: * formatters based on the default or a given locale and a number of formatting jtulach@1334: * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More jtulach@1334: * detail and examples of using these styles are provided in the method jtulach@1334: * descriptions. jtulach@1334: * jtulach@1334: *

{@code DateFormat} helps you to format and parse dates for any locale. jtulach@1334: * Your code can be completely independent of the locale conventions for jtulach@1334: * months, days of the week, or even the calendar format: lunar vs. solar. jtulach@1334: * jtulach@1334: *

To format a date for the current Locale, use one of the jtulach@1334: * static factory methods: jtulach@1334: *

jtulach@1334:  *  myString = DateFormat.getDateInstance().format(myDate);
jtulach@1334:  * 
jtulach@1334: *

If you are formatting multiple dates, it is jtulach@1334: * more efficient to get the format and use it multiple times so that jtulach@1334: * the system doesn't have to fetch the information about the local jtulach@1334: * language and country conventions multiple times. jtulach@1334: *

jtulach@1334:  *  DateFormat df = DateFormat.getDateInstance();
jtulach@1334:  *  for (int i = 0; i < myDate.length; ++i) {
jtulach@1334:  *    output.println(df.format(myDate[i]) + "; ");
jtulach@1334:  *  }
jtulach@1334:  * 
jtulach@1334: *

To format a date for a different Locale, specify it in the jtulach@1334: * call to {@link #getDateInstance(int, Locale) getDateInstance()}. jtulach@1334: *

jtulach@1334:  *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
jtulach@1334:  * 
jtulach@1334: *

You can use a DateFormat to parse also. jtulach@1334: *

jtulach@1334:  *  myDate = df.parse(myString);
jtulach@1334:  * 
jtulach@1334: *

Use {@code getDateInstance} to get the normal date format for that country. jtulach@1334: * There are other static factory methods available. jtulach@1334: * Use {@code getTimeInstance} to get the time format for that country. jtulach@1334: * Use {@code getDateTimeInstance} to get a date and time format. You can pass in jtulach@1334: * different options to these factory methods to control the length of the jtulach@1334: * result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}. The exact result depends jtulach@1334: * on the locale, but generally: jtulach@1334: *

jtulach@1334: * jtulach@1334: *

You can also set the time zone on the format if you wish. jtulach@1334: * If you want even more control over the format or parsing, jtulach@1334: * (or want to give your users more control), jtulach@1334: * you can try casting the {@code DateFormat} you get from the factory methods jtulach@1334: * to a {@link SimpleDateFormat}. This will work for the majority jtulach@1334: * of countries; just remember to put it in a {@code try} block in case you jtulach@1334: * encounter an unusual one. jtulach@1334: * jtulach@1334: *

You can also use forms of the parse and format methods with jtulach@1334: * {@link ParsePosition} and {@link FieldPosition} to jtulach@1334: * allow you to jtulach@1334: *

jtulach@1334: * jtulach@1334: *

Synchronization

jtulach@1334: * jtulach@1334: *

jtulach@1334: * Date formats are not synchronized. jtulach@1334: * It is recommended to create separate format instances for each thread. jtulach@1334: * If multiple threads access a format concurrently, it must be synchronized jtulach@1334: * externally. jtulach@1334: * jtulach@1334: * @see Format jtulach@1334: * @see NumberFormat jtulach@1334: * @see SimpleDateFormat jtulach@1334: * @see java.util.Calendar jtulach@1334: * @see java.util.GregorianCalendar jtulach@1334: * @see java.util.TimeZone jtulach@1334: * @author Mark Davis, Chen-Lieh Huang, Alan Liu jtulach@1334: */ jtulach@1334: public abstract class DateFormat extends Format { jtulach@1334: jtulach@1334: /** jtulach@1334: * The {@link Calendar} instance used for calculating the date-time fields jtulach@1334: * and the instant of time. This field is used for both formatting and jtulach@1334: * parsing. jtulach@1334: * jtulach@1334: *

Subclasses should initialize this field to a {@link Calendar} jtulach@1334: * appropriate for the {@link Locale} associated with this jtulach@1334: * DateFormat. jtulach@1334: * @serial jtulach@1334: */ jtulach@1334: protected Calendar calendar; jtulach@1334: jtulach@1334: /** jtulach@1334: * The number formatter that DateFormat uses to format numbers jtulach@1334: * in dates and times. Subclasses should initialize this to a number format jtulach@1334: * appropriate for the locale associated with this DateFormat. jtulach@1334: * @serial jtulach@1334: */ jtulach@1334: protected NumberFormat numberFormat; jtulach@1334: jtulach@1334: /** jtulach@1334: * Useful constant for ERA field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int ERA_FIELD = 0; jtulach@1334: /** jtulach@1334: * Useful constant for YEAR field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int YEAR_FIELD = 1; jtulach@1334: /** jtulach@1334: * Useful constant for MONTH field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int MONTH_FIELD = 2; jtulach@1334: /** jtulach@1334: * Useful constant for DATE field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int DATE_FIELD = 3; jtulach@1334: /** jtulach@1334: * Useful constant for one-based HOUR_OF_DAY field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock. jtulach@1334: * For example, 23:59 + 01:00 results in 24:59. jtulach@1334: */ jtulach@1334: public final static int HOUR_OF_DAY1_FIELD = 4; jtulach@1334: /** jtulach@1334: * Useful constant for zero-based HOUR_OF_DAY field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock. jtulach@1334: * For example, 23:59 + 01:00 results in 00:59. jtulach@1334: */ jtulach@1334: public final static int HOUR_OF_DAY0_FIELD = 5; jtulach@1334: /** jtulach@1334: * Useful constant for MINUTE field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int MINUTE_FIELD = 6; jtulach@1334: /** jtulach@1334: * Useful constant for SECOND field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int SECOND_FIELD = 7; jtulach@1334: /** jtulach@1334: * Useful constant for MILLISECOND field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int MILLISECOND_FIELD = 8; jtulach@1334: /** jtulach@1334: * Useful constant for DAY_OF_WEEK field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int DAY_OF_WEEK_FIELD = 9; jtulach@1334: /** jtulach@1334: * Useful constant for DAY_OF_YEAR field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int DAY_OF_YEAR_FIELD = 10; jtulach@1334: /** jtulach@1334: * Useful constant for DAY_OF_WEEK_IN_MONTH field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11; jtulach@1334: /** jtulach@1334: * Useful constant for WEEK_OF_YEAR field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int WEEK_OF_YEAR_FIELD = 12; jtulach@1334: /** jtulach@1334: * Useful constant for WEEK_OF_MONTH field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int WEEK_OF_MONTH_FIELD = 13; jtulach@1334: /** jtulach@1334: * Useful constant for AM_PM field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int AM_PM_FIELD = 14; jtulach@1334: /** jtulach@1334: * Useful constant for one-based HOUR field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: * HOUR1_FIELD is used for the one-based 12-hour clock. jtulach@1334: * For example, 11:30 PM + 1 hour results in 12:30 AM. jtulach@1334: */ jtulach@1334: public final static int HOUR1_FIELD = 15; jtulach@1334: /** jtulach@1334: * Useful constant for zero-based HOUR field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: * HOUR0_FIELD is used for the zero-based 12-hour clock. jtulach@1334: * For example, 11:30 PM + 1 hour results in 00:30 AM. jtulach@1334: */ jtulach@1334: public final static int HOUR0_FIELD = 16; jtulach@1334: /** jtulach@1334: * Useful constant for TIMEZONE field alignment. jtulach@1334: * Used in FieldPosition of date/time formatting. jtulach@1334: */ jtulach@1334: public final static int TIMEZONE_FIELD = 17; jtulach@1334: jtulach@1334: // Proclaim serial compatibility with 1.1 FCS jtulach@1334: private static final long serialVersionUID = 7218322306649953788L; jtulach@1334: jtulach@1334: /** jtulach@1334: * Overrides Format. jtulach@1334: * Formats a time object into a time string. Examples of time objects jtulach@1334: * are a time value expressed in milliseconds and a Date object. jtulach@1334: * @param obj must be a Number or a Date. jtulach@1334: * @param toAppendTo the string buffer for the returning time string. jtulach@1334: * @return the string buffer passed in as toAppendTo, with formatted text appended. jtulach@1334: * @param fieldPosition keeps track of the position of the field jtulach@1334: * within the returned string. jtulach@1334: * On input: an alignment field, jtulach@1334: * if desired. On output: the offsets of the alignment field. For jtulach@1334: * example, given a time text "1996.07.10 AD at 15:08:56 PDT", jtulach@1334: * if the given fieldPosition is DateFormat.YEAR_FIELD, the jtulach@1334: * begin index and end index of fieldPosition will be set to jtulach@1334: * 0 and 4, respectively. jtulach@1334: * Notice that if the same time field appears jtulach@1334: * more than once in a pattern, the fieldPosition will be set for the first jtulach@1334: * occurrence of that time field. For instance, formatting a Date to jtulach@1334: * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern jtulach@1334: * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, jtulach@1334: * the begin index and end index of fieldPosition will be set to jtulach@1334: * 5 and 8, respectively, for the first occurrence of the timezone jtulach@1334: * pattern character 'z'. jtulach@1334: * @see java.text.Format jtulach@1334: */ jtulach@1334: public final StringBuffer format(Object obj, StringBuffer toAppendTo, jtulach@1334: FieldPosition fieldPosition) jtulach@1334: { jtulach@1334: if (obj instanceof Date) jtulach@1334: return format( (Date)obj, toAppendTo, fieldPosition ); jtulach@1334: else if (obj instanceof Number) jtulach@1334: return format( new Date(((Number)obj).longValue()), jtulach@1334: toAppendTo, fieldPosition ); jtulach@1334: else jtulach@1334: throw new IllegalArgumentException("Cannot format given Object as a Date"); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Formats a Date into a date/time string. jtulach@1334: * @param date a Date to be formatted into a date/time string. jtulach@1334: * @param toAppendTo the string buffer for the returning date/time string. jtulach@1334: * @param fieldPosition keeps track of the position of the field jtulach@1334: * within the returned string. jtulach@1334: * On input: an alignment field, jtulach@1334: * if desired. On output: the offsets of the alignment field. For jtulach@1334: * example, given a time text "1996.07.10 AD at 15:08:56 PDT", jtulach@1334: * if the given fieldPosition is DateFormat.YEAR_FIELD, the jtulach@1334: * begin index and end index of fieldPosition will be set to jtulach@1334: * 0 and 4, respectively. jtulach@1334: * Notice that if the same time field appears jtulach@1334: * more than once in a pattern, the fieldPosition will be set for the first jtulach@1334: * occurrence of that time field. For instance, formatting a Date to jtulach@1334: * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern jtulach@1334: * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD, jtulach@1334: * the begin index and end index of fieldPosition will be set to jtulach@1334: * 5 and 8, respectively, for the first occurrence of the timezone jtulach@1334: * pattern character 'z'. jtulach@1334: * @return the string buffer passed in as toAppendTo, with formatted text appended. jtulach@1334: */ jtulach@1334: public abstract StringBuffer format(Date date, StringBuffer toAppendTo, jtulach@1334: FieldPosition fieldPosition); jtulach@1334: jtulach@1334: /** jtulach@1334: * Formats a Date into a date/time string. jtulach@1334: * @param date the time value to be formatted into a time string. jtulach@1334: * @return the formatted time string. jtulach@1334: */ jtulach@1334: public final String format(Date date) jtulach@1334: { jtulach@1334: return format(date, new StringBuffer(), jtulach@1334: DontCareFieldPosition.INSTANCE).toString(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Parses text from the beginning of the given string to produce a date. jtulach@1334: * The method may not use the entire text of the given string. jtulach@1334: *

jtulach@1334: * See the {@link #parse(String, ParsePosition)} method for more information jtulach@1334: * on date parsing. jtulach@1334: * jtulach@1334: * @param source A String whose beginning should be parsed. jtulach@1334: * @return A Date parsed from the string. jtulach@1334: * @exception ParseException if the beginning of the specified string jtulach@1334: * cannot be parsed. jtulach@1334: */ jtulach@1334: public Date parse(String source) throws ParseException jtulach@1334: { jtulach@1334: ParsePosition pos = new ParsePosition(0); jtulach@1334: Date result = parse(source, pos); jtulach@1334: if (pos.index == 0) jtulach@1334: throw new ParseException("Unparseable date: \"" + source + "\"" , jtulach@1334: pos.errorIndex); jtulach@1334: return result; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Parse a date/time string according to the given parse position. For jtulach@1334: * example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date} jtulach@1334: * that is equivalent to {@code Date(837039900000L)}. jtulach@1334: * jtulach@1334: *

By default, parsing is lenient: If the input is not in the form used jtulach@1334: * by this object's format method but can still be parsed as a date, then jtulach@1334: * the parse succeeds. Clients may insist on strict adherence to the jtulach@1334: * format by calling {@link #setLenient(boolean) setLenient(false)}. jtulach@1334: * jtulach@1334: *

This parsing operation uses the {@link #calendar} to produce jtulach@1334: * a {@code Date}. As a result, the {@code calendar}'s date-time jtulach@1334: * fields and the {@code TimeZone} value may have been jtulach@1334: * overwritten, depending on subclass implementations. Any {@code jtulach@1334: * TimeZone} value that has previously been set by a call to jtulach@1334: * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need jtulach@1334: * to be restored for further operations. jtulach@1334: * jtulach@1334: * @param source The date/time string to be parsed jtulach@1334: * jtulach@1334: * @param pos On input, the position at which to start parsing; on jtulach@1334: * output, the position at which parsing terminated, or the jtulach@1334: * start position if the parse failed. jtulach@1334: * jtulach@1334: * @return A {@code Date}, or {@code null} if the input could not be parsed jtulach@1334: */ jtulach@1334: public abstract Date parse(String source, ParsePosition pos); jtulach@1334: jtulach@1334: /** jtulach@1334: * Parses text from a string to produce a Date. jtulach@1334: *

jtulach@1334: * The method attempts to parse text starting at the index given by jtulach@1334: * pos. jtulach@1334: * If parsing succeeds, then the index of pos is updated jtulach@1334: * to the index after the last character used (parsing does not necessarily jtulach@1334: * use all characters up to the end of the string), and the parsed jtulach@1334: * date is returned. The updated pos can be used to jtulach@1334: * indicate the starting point for the next call to this method. jtulach@1334: * If an error occurs, then the index of pos is not jtulach@1334: * changed, the error index of pos is set to the index of jtulach@1334: * the character where the error occurred, and null is returned. jtulach@1334: *

jtulach@1334: * See the {@link #parse(String, ParsePosition)} method for more information jtulach@1334: * on date parsing. jtulach@1334: * jtulach@1334: * @param source A String, part of which should be parsed. jtulach@1334: * @param pos A ParsePosition object with index and error jtulach@1334: * index information as described above. jtulach@1334: * @return A Date parsed from the string. In case of jtulach@1334: * error, returns null. jtulach@1334: * @exception NullPointerException if pos is null. jtulach@1334: */ jtulach@1334: public Object parseObject(String source, ParsePosition pos) { jtulach@1334: return parse(source, pos); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant for full style pattern. jtulach@1334: */ jtulach@1334: public static final int FULL = 0; jtulach@1334: /** jtulach@1334: * Constant for long style pattern. jtulach@1334: */ jtulach@1334: public static final int LONG = 1; jtulach@1334: /** jtulach@1334: * Constant for medium style pattern. jtulach@1334: */ jtulach@1334: public static final int MEDIUM = 2; jtulach@1334: /** jtulach@1334: * Constant for short style pattern. jtulach@1334: */ jtulach@1334: public static final int SHORT = 3; jtulach@1334: /** jtulach@1334: * Constant for default style pattern. Its value is MEDIUM. jtulach@1334: */ jtulach@1334: public static final int DEFAULT = MEDIUM; jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the time formatter with the default formatting style jtulach@1334: * for the default locale. jtulach@1334: * @return a time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getTimeInstance() jtulach@1334: { jtulach@1334: return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the time formatter with the given formatting style jtulach@1334: * for the default locale. jtulach@1334: * @param style the given formatting style. For example, jtulach@1334: * SHORT for "h:mm a" in the US locale. jtulach@1334: * @return a time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getTimeInstance(int style) jtulach@1334: { jtulach@1334: return get(style, 0, 1, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the time formatter with the given formatting style jtulach@1334: * for the given locale. jtulach@1334: * @param style the given formatting style. For example, jtulach@1334: * SHORT for "h:mm a" in the US locale. jtulach@1334: * @param aLocale the given locale. jtulach@1334: * @return a time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getTimeInstance(int style, jtulach@1334: Locale aLocale) jtulach@1334: { jtulach@1334: return get(style, 0, 1, aLocale); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date formatter with the default formatting style jtulach@1334: * for the default locale. jtulach@1334: * @return a date formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getDateInstance() jtulach@1334: { jtulach@1334: return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date formatter with the given formatting style jtulach@1334: * for the default locale. jtulach@1334: * @param style the given formatting style. For example, jtulach@1334: * SHORT for "M/d/yy" in the US locale. jtulach@1334: * @return a date formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getDateInstance(int style) jtulach@1334: { jtulach@1334: return get(0, style, 2, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date formatter with the given formatting style jtulach@1334: * for the given locale. jtulach@1334: * @param style the given formatting style. For example, jtulach@1334: * SHORT for "M/d/yy" in the US locale. jtulach@1334: * @param aLocale the given locale. jtulach@1334: * @return a date formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getDateInstance(int style, jtulach@1334: Locale aLocale) jtulach@1334: { jtulach@1334: return get(0, style, 2, aLocale); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date/time formatter with the default formatting style jtulach@1334: * for the default locale. jtulach@1334: * @return a date/time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getDateTimeInstance() jtulach@1334: { jtulach@1334: return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date/time formatter with the given date and time jtulach@1334: * formatting styles for the default locale. jtulach@1334: * @param dateStyle the given date formatting style. For example, jtulach@1334: * SHORT for "M/d/yy" in the US locale. jtulach@1334: * @param timeStyle the given time formatting style. For example, jtulach@1334: * SHORT for "h:mm a" in the US locale. jtulach@1334: * @return a date/time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat getDateTimeInstance(int dateStyle, jtulach@1334: int timeStyle) jtulach@1334: { jtulach@1334: return get(timeStyle, dateStyle, 3, Locale.getDefault(Locale.Category.FORMAT)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the date/time formatter with the given formatting styles jtulach@1334: * for the given locale. jtulach@1334: * @param dateStyle the given date formatting style. jtulach@1334: * @param timeStyle the given time formatting style. jtulach@1334: * @param aLocale the given locale. jtulach@1334: * @return a date/time formatter. jtulach@1334: */ jtulach@1334: public final static DateFormat jtulach@1334: getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) jtulach@1334: { jtulach@1334: return get(timeStyle, dateStyle, 3, aLocale); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Get a default date/time formatter that uses the SHORT style for both the jtulach@1334: * date and the time. jtulach@1334: */ jtulach@1334: public final static DateFormat getInstance() { jtulach@1334: return getDateTimeInstance(SHORT, SHORT); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns an array of all locales for which the jtulach@1334: * get*Instance methods of this class can return jtulach@1334: * localized instances. jtulach@1334: * The returned array represents the union of locales supported by the Java jtulach@1334: * runtime and by installed jtulach@1334: * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations. jtulach@1334: * It must contain at least a Locale instance equal to jtulach@1334: * {@link java.util.Locale#US Locale.US}. jtulach@1334: * jtulach@1334: * @return An array of locales for which localized jtulach@1334: * DateFormat instances are available. jtulach@1334: */ jtulach@1334: public static Locale[] getAvailableLocales() jtulach@1334: { jtulach@1334: LocaleServiceProviderPool pool = jtulach@1334: LocaleServiceProviderPool.getPool(DateFormatProvider.class); jtulach@1334: return pool.getAvailableLocales(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Set the calendar to be used by this date format. Initially, the default jtulach@1334: * calendar for the specified or default locale is used. jtulach@1334: * jtulach@1334: *

Any {@link java.util.TimeZone TimeZone} and {@linkplain jtulach@1334: * #isLenient() leniency} values that have previously been set are jtulach@1334: * overwritten by {@code newCalendar}'s values. jtulach@1334: * jtulach@1334: * @param newCalendar the new {@code Calendar} to be used by the date format jtulach@1334: */ jtulach@1334: public void setCalendar(Calendar newCalendar) jtulach@1334: { jtulach@1334: this.calendar = newCalendar; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the calendar associated with this date/time formatter. jtulach@1334: * jtulach@1334: * @return the calendar associated with this date/time formatter. jtulach@1334: */ jtulach@1334: public Calendar getCalendar() jtulach@1334: { jtulach@1334: return calendar; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Allows you to set the number formatter. jtulach@1334: * @param newNumberFormat the given new NumberFormat. jtulach@1334: */ jtulach@1334: public void setNumberFormat(NumberFormat newNumberFormat) jtulach@1334: { jtulach@1334: this.numberFormat = newNumberFormat; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the number formatter which this date/time formatter uses to jtulach@1334: * format and parse a time. jtulach@1334: * @return the number formatter which this date/time formatter uses. jtulach@1334: */ jtulach@1334: public NumberFormat getNumberFormat() jtulach@1334: { jtulach@1334: return numberFormat; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the time zone for the calendar of this {@code DateFormat} object. jtulach@1334: * This method is equivalent to the following call. jtulach@1334: *

jtulach@1334:      *  getCalendar().setTimeZone(zone)
jtulach@1334:      * 
jtulach@1334: * jtulach@1334: *

The {@code TimeZone} set by this method is overwritten by a jtulach@1334: * {@link #setCalendar(java.util.Calendar) setCalendar} call. jtulach@1334: * jtulach@1334: *

The {@code TimeZone} set by this method may be overwritten as jtulach@1334: * a result of a call to the parse method. jtulach@1334: * jtulach@1334: * @param zone the given new time zone. jtulach@1334: */ jtulach@1334: public void setTimeZone(TimeZone zone) jtulach@1334: { jtulach@1334: calendar.setTimeZone(zone); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the time zone. jtulach@1334: * This method is equivalent to the following call. jtulach@1334: *

jtulach@1334:      *  getCalendar().getTimeZone()
jtulach@1334:      * 
jtulach@1334: * jtulach@1334: * @return the time zone associated with the calendar of DateFormat. jtulach@1334: */ jtulach@1334: public TimeZone getTimeZone() jtulach@1334: { jtulach@1334: return calendar.getTimeZone(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Specify whether or not date/time parsing is to be lenient. With jtulach@1334: * lenient parsing, the parser may use heuristics to interpret inputs that jtulach@1334: * do not precisely match this object's format. With strict parsing, jtulach@1334: * inputs must match this object's format. jtulach@1334: * jtulach@1334: *

This method is equivalent to the following call. jtulach@1334: *

jtulach@1334:      *  getCalendar().setLenient(lenient)
jtulach@1334:      * 
jtulach@1334: * jtulach@1334: *

This leniency value is overwritten by a call to {@link jtulach@1334: * #setCalendar(java.util.Calendar) setCalendar()}. jtulach@1334: * jtulach@1334: * @param lenient when {@code true}, parsing is lenient jtulach@1334: * @see java.util.Calendar#setLenient(boolean) jtulach@1334: */ jtulach@1334: public void setLenient(boolean lenient) jtulach@1334: { jtulach@1334: calendar.setLenient(lenient); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Tell whether date/time parsing is to be lenient. jtulach@1334: * This method is equivalent to the following call. jtulach@1334: *

jtulach@1334:      *  getCalendar().isLenient()
jtulach@1334:      * 
jtulach@1334: * jtulach@1334: * @return {@code true} if the {@link #calendar} is lenient; jtulach@1334: * {@code false} otherwise. jtulach@1334: * @see java.util.Calendar#isLenient() jtulach@1334: */ jtulach@1334: public boolean isLenient() jtulach@1334: { jtulach@1334: return calendar.isLenient(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Overrides hashCode jtulach@1334: */ jtulach@1334: public int hashCode() { jtulach@1334: return numberFormat.hashCode(); jtulach@1334: // just enough fields for a reasonable distribution jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Overrides equals jtulach@1334: */ jtulach@1334: public boolean equals(Object obj) { jtulach@1334: if (this == obj) return true; jtulach@1334: if (obj == null || getClass() != obj.getClass()) return false; jtulach@1334: DateFormat other = (DateFormat) obj; jtulach@1334: return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET! jtulach@1334: calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() && jtulach@1334: calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() && jtulach@1334: calendar.isLenient() == other.calendar.isLenient() && jtulach@1334: calendar.getTimeZone().equals(other.calendar.getTimeZone()) && jtulach@1334: numberFormat.equals(other.numberFormat)); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Overrides Cloneable jtulach@1334: */ jtulach@1334: public Object clone() jtulach@1334: { jtulach@1334: DateFormat other = (DateFormat) super.clone(); jtulach@1334: other.calendar = (Calendar) calendar.clone(); jtulach@1334: other.numberFormat = (NumberFormat) numberFormat.clone(); jtulach@1334: return other; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a DateFormat with the given time and/or date style in the given jtulach@1334: * locale. jtulach@1334: * @param timeStyle a value from 0 to 3 indicating the time format, jtulach@1334: * ignored if flags is 2 jtulach@1334: * @param dateStyle a value from 0 to 3 indicating the time format, jtulach@1334: * ignored if flags is 1 jtulach@1334: * @param flags either 1 for a time format, 2 for a date format, jtulach@1334: * or 3 for a date/time format jtulach@1334: * @param loc the locale for the format jtulach@1334: */ jtulach@1334: private static DateFormat get(int timeStyle, int dateStyle, jtulach@1334: int flags, Locale loc) { jtulach@1334: if ((flags & 1) != 0) { jtulach@1334: if (timeStyle < 0 || timeStyle > 3) { jtulach@1334: throw new IllegalArgumentException("Illegal time style " + timeStyle); jtulach@1334: } jtulach@1334: } else { jtulach@1334: timeStyle = -1; jtulach@1334: } jtulach@1334: if ((flags & 2) != 0) { jtulach@1334: if (dateStyle < 0 || dateStyle > 3) { jtulach@1334: throw new IllegalArgumentException("Illegal date style " + dateStyle); jtulach@1334: } jtulach@1334: } else { jtulach@1334: dateStyle = -1; jtulach@1334: } jtulach@1334: try { jtulach@1334: // Check whether a provider can provide an implementation that's closer jtulach@1334: // to the requested locale than what the Java runtime itself can provide. jtulach@1334: LocaleServiceProviderPool pool = jtulach@1334: LocaleServiceProviderPool.getPool(DateFormatProvider.class); jtulach@1334: if (pool.hasProviders()) { jtulach@1334: DateFormat providersInstance = pool.getLocalizedObject( jtulach@1334: DateFormatGetter.INSTANCE, jtulach@1334: loc, jtulach@1334: timeStyle, jtulach@1334: dateStyle, jtulach@1334: flags); jtulach@1334: if (providersInstance != null) { jtulach@1334: return providersInstance; jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: return new SimpleDateFormat(timeStyle, dateStyle, loc); jtulach@1334: } catch (MissingResourceException e) { jtulach@1334: return new SimpleDateFormat("M/d/yy h:mm a"); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Create a new date format. jtulach@1334: */ jtulach@1334: protected DateFormat() {} jtulach@1334: jtulach@1334: /** jtulach@1334: * Defines constants that are used as attribute keys in the jtulach@1334: * AttributedCharacterIterator returned jtulach@1334: * from DateFormat.formatToCharacterIterator and as jtulach@1334: * field identifiers in FieldPosition. jtulach@1334: *

jtulach@1334: * The class also provides two methods to map jtulach@1334: * between its constants and the corresponding Calendar constants. jtulach@1334: * jtulach@1334: * @since 1.4 jtulach@1334: * @see java.util.Calendar jtulach@1334: */ jtulach@1334: public static class Field extends Format.Field { jtulach@1334: jtulach@1334: // Proclaim serial compatibility with 1.4 FCS jtulach@1334: private static final long serialVersionUID = 7441350119349544720L; jtulach@1334: jtulach@1334: // table of all instances in this class, used by readResolve jtulach@1334: private static final Map instanceMap = new HashMap(18); jtulach@1334: // Maps from Calendar constant (such as Calendar.ERA) to Field jtulach@1334: // constant (such as Field.ERA). jtulach@1334: private static final Field[] calendarToFieldMapping = jtulach@1334: new Field[Calendar.FIELD_COUNT]; jtulach@1334: jtulach@1334: /** Calendar field. */ jtulach@1334: private int calendarField; jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the Field constant that corresponds to jtulach@1334: * the Calendar constant calendarField. jtulach@1334: * If there is no direct mapping between the Calendar jtulach@1334: * constant and a Field, null is returned. jtulach@1334: * jtulach@1334: * @throws IllegalArgumentException if calendarField is jtulach@1334: * not the value of a Calendar field constant. jtulach@1334: * @param calendarField Calendar field constant jtulach@1334: * @return Field instance representing calendarField. jtulach@1334: * @see java.util.Calendar jtulach@1334: */ jtulach@1334: public static Field ofCalendarField(int calendarField) { jtulach@1334: if (calendarField < 0 || calendarField >= jtulach@1334: calendarToFieldMapping.length) { jtulach@1334: throw new IllegalArgumentException("Unknown Calendar constant " jtulach@1334: + calendarField); jtulach@1334: } jtulach@1334: return calendarToFieldMapping[calendarField]; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a Field. jtulach@1334: * jtulach@1334: * @param name the name of the Field jtulach@1334: * @param calendarField the Calendar constant this jtulach@1334: * Field corresponds to; any value, even one jtulach@1334: * outside the range of legal Calendar values may jtulach@1334: * be used, but -1 should be used for values jtulach@1334: * that don't correspond to legal Calendar values jtulach@1334: */ jtulach@1334: protected Field(String name, int calendarField) { jtulach@1334: super(name); jtulach@1334: this.calendarField = calendarField; jtulach@1334: if (this.getClass() == DateFormat.Field.class) { jtulach@1334: instanceMap.put(name, this); jtulach@1334: if (calendarField >= 0) { jtulach@1334: // assert(calendarField < Calendar.FIELD_COUNT); jtulach@1334: calendarToFieldMapping[calendarField] = this; jtulach@1334: } jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the Calendar field associated with this jtulach@1334: * attribute. For example, if this represents the hours field of jtulach@1334: * a Calendar, this would return jtulach@1334: * Calendar.HOUR. If there is no corresponding jtulach@1334: * Calendar constant, this will return -1. jtulach@1334: * jtulach@1334: * @return Calendar constant for this field jtulach@1334: * @see java.util.Calendar jtulach@1334: */ jtulach@1334: public int getCalendarField() { jtulach@1334: return calendarField; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Resolves instances being deserialized to the predefined constants. jtulach@1334: * jtulach@1334: * @throws InvalidObjectException if the constant could not be jtulach@1334: * resolved. jtulach@1334: * @return resolved DateFormat.Field constant jtulach@1334: */ jtulach@1334: protected Object readResolve() throws InvalidObjectException { jtulach@1334: if (this.getClass() != DateFormat.Field.class) { jtulach@1334: throw new InvalidObjectException("subclass didn't correctly implement readResolve"); jtulach@1334: } jtulach@1334: jtulach@1334: Object instance = instanceMap.get(getName()); jtulach@1334: if (instance != null) { jtulach@1334: return instance; jtulach@1334: } else { jtulach@1334: throw new InvalidObjectException("unknown attribute name"); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: // jtulach@1334: // The constants jtulach@1334: // jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the era field. jtulach@1334: */ jtulach@1334: public final static Field ERA = new Field("era", Calendar.ERA); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the year field. jtulach@1334: */ jtulach@1334: public final static Field YEAR = new Field("year", Calendar.YEAR); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the month field. jtulach@1334: */ jtulach@1334: public final static Field MONTH = new Field("month", Calendar.MONTH); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the day of month field. jtulach@1334: */ jtulach@1334: public final static Field DAY_OF_MONTH = new jtulach@1334: Field("day of month", Calendar.DAY_OF_MONTH); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the hour of day field, where the legal values jtulach@1334: * are 1 to 24. jtulach@1334: */ jtulach@1334: public final static Field HOUR_OF_DAY1 = new Field("hour of day 1",-1); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the hour of day field, where the legal values jtulach@1334: * are 0 to 23. jtulach@1334: */ jtulach@1334: public final static Field HOUR_OF_DAY0 = new jtulach@1334: Field("hour of day", Calendar.HOUR_OF_DAY); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the minute field. jtulach@1334: */ jtulach@1334: public final static Field MINUTE =new Field("minute", Calendar.MINUTE); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the second field. jtulach@1334: */ jtulach@1334: public final static Field SECOND =new Field("second", Calendar.SECOND); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the millisecond field. jtulach@1334: */ jtulach@1334: public final static Field MILLISECOND = new jtulach@1334: Field("millisecond", Calendar.MILLISECOND); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the day of week field. jtulach@1334: */ jtulach@1334: public final static Field DAY_OF_WEEK = new jtulach@1334: Field("day of week", Calendar.DAY_OF_WEEK); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the day of year field. jtulach@1334: */ jtulach@1334: public final static Field DAY_OF_YEAR = new jtulach@1334: Field("day of year", Calendar.DAY_OF_YEAR); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the day of week field. jtulach@1334: */ jtulach@1334: public final static Field DAY_OF_WEEK_IN_MONTH = jtulach@1334: new Field("day of week in month", jtulach@1334: Calendar.DAY_OF_WEEK_IN_MONTH); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the week of year field. jtulach@1334: */ jtulach@1334: public final static Field WEEK_OF_YEAR = new jtulach@1334: Field("week of year", Calendar.WEEK_OF_YEAR); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the week of month field. jtulach@1334: */ jtulach@1334: public final static Field WEEK_OF_MONTH = new jtulach@1334: Field("week of month", Calendar.WEEK_OF_MONTH); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the time of day indicator jtulach@1334: * (e.g. "a.m." or "p.m.") field. jtulach@1334: */ jtulach@1334: public final static Field AM_PM = new jtulach@1334: Field("am pm", Calendar.AM_PM); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the hour field, where the legal values are jtulach@1334: * 1 to 12. jtulach@1334: */ jtulach@1334: public final static Field HOUR1 = new Field("hour 1", -1); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the hour field, where the legal values are jtulach@1334: * 0 to 11. jtulach@1334: */ jtulach@1334: public final static Field HOUR0 = new jtulach@1334: Field("hour", Calendar.HOUR); jtulach@1334: jtulach@1334: /** jtulach@1334: * Constant identifying the time zone field. jtulach@1334: */ jtulach@1334: public final static Field TIME_ZONE = new Field("time zone", -1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Obtains a DateFormat instance from a DateFormatProvider jtulach@1334: * implementation. jtulach@1334: */ jtulach@1334: private static class DateFormatGetter jtulach@1334: implements LocaleServiceProviderPool.LocalizedObjectGetter { jtulach@1334: private static final DateFormatGetter INSTANCE = new DateFormatGetter(); jtulach@1334: jtulach@1334: public DateFormat getObject(DateFormatProvider dateFormatProvider, jtulach@1334: Locale locale, jtulach@1334: String key, jtulach@1334: Object... params) { jtulach@1334: assert params.length == 3; jtulach@1334: jtulach@1334: int timeStyle = (Integer)params[0]; jtulach@1334: int dateStyle = (Integer)params[1]; jtulach@1334: int flags = (Integer)params[2]; jtulach@1334: jtulach@1334: switch (flags) { jtulach@1334: case 1: jtulach@1334: return dateFormatProvider.getTimeInstance(timeStyle, locale); jtulach@1334: case 2: jtulach@1334: return dateFormatProvider.getDateInstance(dateStyle, locale); jtulach@1334: case 3: jtulach@1334: return dateFormatProvider.getDateTimeInstance(dateStyle, timeStyle, locale); jtulach@1334: default: jtulach@1334: assert false : "should not happen"; jtulach@1334: } jtulach@1334: jtulach@1334: return null; jtulach@1334: } jtulach@1334: } jtulach@1334: }