src/java.base/share/classes/java/text/DateFormat.java
changeset 58288 48e480e56aad
parent 58242 94bb65cb37d3
child 58679 9c3209ff7550
equal deleted inserted replaced
58287:a7f16447085e 58288:48e480e56aad
    56 /**
    56 /**
    57  * {@code DateFormat} is an abstract class for date/time formatting subclasses which
    57  * {@code DateFormat} is an abstract class for date/time formatting subclasses which
    58  * formats and parses dates or time in a language-independent manner.
    58  * formats and parses dates or time in a language-independent manner.
    59  * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
    59  * The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
    60  * formatting (i.e., date → text), parsing (text → date), and
    60  * formatting (i.e., date → text), parsing (text → date), and
    61  * normalization.  The date is represented as a <code>Date</code> object or
    61  * normalization.  The date is represented as a {@code Date} object or
    62  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
    62  * as the milliseconds since January 1, 1970, 00:00:00 GMT.
    63  *
    63  *
    64  * <p>{@code DateFormat} provides many class methods for obtaining default date/time
    64  * <p>{@code DateFormat} provides many class methods for obtaining default date/time
    65  * formatters based on the default or a given locale and a number of formatting
    65  * formatters based on the default or a given locale and a number of formatting
    66  * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
    66  * styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
   183      * and the instant of time. This field is used for both formatting and
   183      * and the instant of time. This field is used for both formatting and
   184      * parsing.
   184      * parsing.
   185      *
   185      *
   186      * <p>Subclasses should initialize this field to a {@link Calendar}
   186      * <p>Subclasses should initialize this field to a {@link Calendar}
   187      * appropriate for the {@link Locale} associated with this
   187      * appropriate for the {@link Locale} associated with this
   188      * <code>DateFormat</code>.
   188      * {@code DateFormat}.
   189      * @serial
   189      * @serial
   190      */
   190      */
   191     protected Calendar calendar;
   191     protected Calendar calendar;
   192 
   192 
   193     /**
   193     /**
   194      * The number formatter that <code>DateFormat</code> uses to format numbers
   194      * The number formatter that {@code DateFormat} uses to format numbers
   195      * in dates and times.  Subclasses should initialize this to a number format
   195      * in dates and times.  Subclasses should initialize this to a number format
   196      * appropriate for the locale associated with this <code>DateFormat</code>.
   196      * appropriate for the locale associated with this {@code DateFormat}.
   197      * @serial
   197      * @serial
   198      */
   198      */
   199     protected NumberFormat numberFormat;
   199     protected NumberFormat numberFormat;
   200 
   200 
   201     /**
   201     /**
   381      * The method may not use the entire text of the given string.
   381      * The method may not use the entire text of the given string.
   382      * <p>
   382      * <p>
   383      * See the {@link #parse(String, ParsePosition)} method for more information
   383      * See the {@link #parse(String, ParsePosition)} method for more information
   384      * on date parsing.
   384      * on date parsing.
   385      *
   385      *
   386      * @param source A <code>String</code> whose beginning should be parsed.
   386      * @param source A {@code String} whose beginning should be parsed.
   387      * @return A <code>Date</code> parsed from the string.
   387      * @return A {@code Date} parsed from the string.
   388      * @throws    ParseException if the beginning of the specified string
   388      * @throws    ParseException if the beginning of the specified string
   389      *            cannot be parsed.
   389      *            cannot be parsed.
   390      */
   390      */
   391     public Date parse(String source) throws ParseException
   391     public Date parse(String source) throws ParseException
   392     {
   392     {
   425      * @return      A {@code Date}, or {@code null} if the input could not be parsed
   425      * @return      A {@code Date}, or {@code null} if the input could not be parsed
   426      */
   426      */
   427     public abstract Date parse(String source, ParsePosition pos);
   427     public abstract Date parse(String source, ParsePosition pos);
   428 
   428 
   429     /**
   429     /**
   430      * Parses text from a string to produce a <code>Date</code>.
   430      * Parses text from a string to produce a {@code Date}.
   431      * <p>
   431      * <p>
   432      * The method attempts to parse text starting at the index given by
   432      * The method attempts to parse text starting at the index given by
   433      * <code>pos</code>.
   433      * {@code pos}.
   434      * If parsing succeeds, then the index of <code>pos</code> is updated
   434      * If parsing succeeds, then the index of {@code pos} is updated
   435      * to the index after the last character used (parsing does not necessarily
   435      * to the index after the last character used (parsing does not necessarily
   436      * use all characters up to the end of the string), and the parsed
   436      * use all characters up to the end of the string), and the parsed
   437      * date is returned. The updated <code>pos</code> can be used to
   437      * date is returned. The updated {@code pos} can be used to
   438      * indicate the starting point for the next call to this method.
   438      * indicate the starting point for the next call to this method.
   439      * If an error occurs, then the index of <code>pos</code> is not
   439      * If an error occurs, then the index of {@code pos} is not
   440      * changed, the error index of <code>pos</code> is set to the index of
   440      * changed, the error index of {@code pos} is set to the index of
   441      * the character where the error occurred, and null is returned.
   441      * the character where the error occurred, and null is returned.
   442      * <p>
   442      * <p>
   443      * See the {@link #parse(String, ParsePosition)} method for more information
   443      * See the {@link #parse(String, ParsePosition)} method for more information
   444      * on date parsing.
   444      * on date parsing.
   445      *
   445      *
   446      * @param source A <code>String</code>, part of which should be parsed.
   446      * @param source A {@code String}, part of which should be parsed.
   447      * @param pos A <code>ParsePosition</code> object with index and error
   447      * @param pos A {@code ParsePosition} object with index and error
   448      *            index information as described above.
   448      *            index information as described above.
   449      * @return A <code>Date</code> parsed from the string. In case of
   449      * @return A {@code Date} parsed from the string. In case of
   450      *         error, returns null.
   450      *         error, returns null.
   451      * @throws NullPointerException if {@code source} or {@code pos} is null.
   451      * @throws NullPointerException if {@code source} or {@code pos} is null.
   452      */
   452      */
   453     public Object parseObject(String source, ParsePosition pos) {
   453     public Object parseObject(String source, ParsePosition pos) {
   454         return parse(source, pos);
   454         return parse(source, pos);
   626         return getDateTimeInstance(SHORT, SHORT);
   626         return getDateTimeInstance(SHORT, SHORT);
   627     }
   627     }
   628 
   628 
   629     /**
   629     /**
   630      * Returns an array of all locales for which the
   630      * Returns an array of all locales for which the
   631      * <code>get*Instance</code> methods of this class can return
   631      * {@code get*Instance} methods of this class can return
   632      * localized instances.
   632      * localized instances.
   633      * The returned array represents the union of locales supported by the Java
   633      * The returned array represents the union of locales supported by the Java
   634      * runtime and by installed
   634      * runtime and by installed
   635      * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
   635      * {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
   636      * It must contain at least a <code>Locale</code> instance equal to
   636      * It must contain at least a {@code Locale} instance equal to
   637      * {@link java.util.Locale#US Locale.US}.
   637      * {@link java.util.Locale#US Locale.US}.
   638      *
   638      *
   639      * @return An array of locales for which localized
   639      * @return An array of locales for which localized
   640      *         <code>DateFormat</code> instances are available.
   640      *         {@code DateFormat} instances are available.
   641      */
   641      */
   642     public static Locale[] getAvailableLocales()
   642     public static Locale[] getAvailableLocales()
   643     {
   643     {
   644         LocaleServiceProviderPool pool =
   644         LocaleServiceProviderPool pool =
   645             LocaleServiceProviderPool.getPool(DateFormatProvider.class);
   645             LocaleServiceProviderPool.getPool(DateFormatProvider.class);
   852      */
   852      */
   853     protected DateFormat() {}
   853     protected DateFormat() {}
   854 
   854 
   855     /**
   855     /**
   856      * Defines constants that are used as attribute keys in the
   856      * Defines constants that are used as attribute keys in the
   857      * <code>AttributedCharacterIterator</code> returned
   857      * {@code AttributedCharacterIterator} returned
   858      * from <code>DateFormat.formatToCharacterIterator</code> and as
   858      * from {@code DateFormat.formatToCharacterIterator} and as
   859      * field identifiers in <code>FieldPosition</code>.
   859      * field identifiers in {@code FieldPosition}.
   860      * <p>
   860      * <p>
   861      * The class also provides two methods to map
   861      * The class also provides two methods to map
   862      * between its constants and the corresponding Calendar constants.
   862      * between its constants and the corresponding Calendar constants.
   863      *
   863      *
   864      * @since 1.4
   864      * @since 1.4
   879 
   879 
   880         /** Calendar field. */
   880         /** Calendar field. */
   881         private int calendarField;
   881         private int calendarField;
   882 
   882 
   883         /**
   883         /**
   884          * Returns the <code>Field</code> constant that corresponds to
   884          * Returns the {@code Field} constant that corresponds to
   885          * the <code>Calendar</code> constant <code>calendarField</code>.
   885          * the {@code Calendar} constant {@code calendarField}.
   886          * If there is no direct mapping between the <code>Calendar</code>
   886          * If there is no direct mapping between the {@code Calendar}
   887          * constant and a <code>Field</code>, null is returned.
   887          * constant and a {@code Field}, null is returned.
   888          *
   888          *
   889          * @throws IllegalArgumentException if <code>calendarField</code> is
   889          * @throws IllegalArgumentException if {@code calendarField} is
   890          *         not the value of a <code>Calendar</code> field constant.
   890          *         not the value of a {@code Calendar} field constant.
   891          * @param calendarField Calendar field constant
   891          * @param calendarField Calendar field constant
   892          * @return Field instance representing calendarField.
   892          * @return Field instance representing calendarField.
   893          * @see java.util.Calendar
   893          * @see java.util.Calendar
   894          */
   894          */
   895         public static Field ofCalendarField(int calendarField) {
   895         public static Field ofCalendarField(int calendarField) {
   900             }
   900             }
   901             return calendarToFieldMapping[calendarField];
   901             return calendarToFieldMapping[calendarField];
   902         }
   902         }
   903 
   903 
   904         /**
   904         /**
   905          * Creates a <code>Field</code>.
   905          * Creates a {@code Field}.
   906          *
   906          *
   907          * @param name the name of the <code>Field</code>
   907          * @param name the name of the {@code Field}
   908          * @param calendarField the <code>Calendar</code> constant this
   908          * @param calendarField the {@code Calendar} constant this
   909          *        <code>Field</code> corresponds to; any value, even one
   909          *        {@code Field} corresponds to; any value, even one
   910          *        outside the range of legal <code>Calendar</code> values may
   910          *        outside the range of legal {@code Calendar} values may
   911          *        be used, but <code>-1</code> should be used for values
   911          *        be used, but {@code -1} should be used for values
   912          *        that don't correspond to legal <code>Calendar</code> values
   912          *        that don't correspond to legal {@code Calendar} values
   913          */
   913          */
   914         protected Field(String name, int calendarField) {
   914         protected Field(String name, int calendarField) {
   915             super(name);
   915             super(name);
   916             this.calendarField = calendarField;
   916             this.calendarField = calendarField;
   917             if (this.getClass() == DateFormat.Field.class) {
   917             if (this.getClass() == DateFormat.Field.class) {
   922                 }
   922                 }
   923             }
   923             }
   924         }
   924         }
   925 
   925 
   926         /**
   926         /**
   927          * Returns the <code>Calendar</code> field associated with this
   927          * Returns the {@code Calendar} field associated with this
   928          * attribute. For example, if this represents the hours field of
   928          * attribute. For example, if this represents the hours field of
   929          * a <code>Calendar</code>, this would return
   929          * a {@code Calendar}, this would return
   930          * <code>Calendar.HOUR</code>. If there is no corresponding
   930          * {@code Calendar.HOUR}. If there is no corresponding
   931          * <code>Calendar</code> constant, this will return -1.
   931          * {@code Calendar} constant, this will return -1.
   932          *
   932          *
   933          * @return Calendar constant for this field
   933          * @return Calendar constant for this field
   934          * @see java.util.Calendar
   934          * @see java.util.Calendar
   935          */
   935          */
   936         public int getCalendarField() {
   936         public int getCalendarField() {