src/java.base/share/classes/java/text/SimpleDateFormat.java
changeset 58242 94bb65cb37d3
parent 57956 e0b8b019d2f5
child 58288 48e480e56aad
equal deleted inserted replaced
58241:33de7752835c 58242:94bb65cb37d3
   590      *     SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}.
   590      *     SimpleDateFormat(pattern, Locale.getDefault(Locale.Category.FORMAT))}.
   591      *
   591      *
   592      * @see java.util.Locale#getDefault(java.util.Locale.Category)
   592      * @see java.util.Locale#getDefault(java.util.Locale.Category)
   593      * @see java.util.Locale.Category#FORMAT
   593      * @see java.util.Locale.Category#FORMAT
   594      * @param pattern the pattern describing the date and time format
   594      * @param pattern the pattern describing the date and time format
   595      * @exception NullPointerException if the given pattern is null
   595      * @throws    NullPointerException if the given pattern is null
   596      * @exception IllegalArgumentException if the given pattern is invalid
   596      * @throws    IllegalArgumentException if the given pattern is invalid
   597      */
   597      */
   598     public SimpleDateFormat(String pattern)
   598     public SimpleDateFormat(String pattern)
   599     {
   599     {
   600         this(pattern, Locale.getDefault(Locale.Category.FORMAT));
   600         this(pattern, Locale.getDefault(Locale.Category.FORMAT));
   601     }
   601     }
   607      * For full coverage, use the factory methods in the {@link DateFormat}
   607      * For full coverage, use the factory methods in the {@link DateFormat}
   608      * class.
   608      * class.
   609      *
   609      *
   610      * @param pattern the pattern describing the date and time format
   610      * @param pattern the pattern describing the date and time format
   611      * @param locale the locale whose date format symbols should be used
   611      * @param locale the locale whose date format symbols should be used
   612      * @exception NullPointerException if the given pattern or locale is null
   612      * @throws    NullPointerException if the given pattern or locale is null
   613      * @exception IllegalArgumentException if the given pattern is invalid
   613      * @throws    IllegalArgumentException if the given pattern is invalid
   614      */
   614      */
   615     public SimpleDateFormat(String pattern, Locale locale)
   615     public SimpleDateFormat(String pattern, Locale locale)
   616     {
   616     {
   617         if (pattern == null || locale == null) {
   617         if (pattern == null || locale == null) {
   618             throw new NullPointerException();
   618             throw new NullPointerException();
   629      * Constructs a <code>SimpleDateFormat</code> using the given pattern and
   629      * Constructs a <code>SimpleDateFormat</code> using the given pattern and
   630      * date format symbols.
   630      * date format symbols.
   631      *
   631      *
   632      * @param pattern the pattern describing the date and time format
   632      * @param pattern the pattern describing the date and time format
   633      * @param formatSymbols the date format symbols to be used for formatting
   633      * @param formatSymbols the date format symbols to be used for formatting
   634      * @exception NullPointerException if the given pattern or formatSymbols is null
   634      * @throws    NullPointerException if the given pattern or formatSymbols is null
   635      * @exception IllegalArgumentException if the given pattern is invalid
   635      * @throws    IllegalArgumentException if the given pattern is invalid
   636      */
   636      */
   637     public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
   637     public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
   638     {
   638     {
   639         if (pattern == null || formatSymbols == null) {
   639         if (pattern == null || formatSymbols == null) {
   640             throw new NullPointerException();
   640             throw new NullPointerException();
   737      * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
   737      * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
   738      * character in place of Length. For example, if the given pattern
   738      * character in place of Length. For example, if the given pattern
   739      * is "'o'", the TaggedData entry is
   739      * is "'o'", the TaggedData entry is
   740      * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
   740      * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
   741      *
   741      *
   742      * @exception NullPointerException if the given pattern is null
   742      * @throws    NullPointerException if the given pattern is null
   743      * @exception IllegalArgumentException if the given pattern is invalid
   743      * @throws    IllegalArgumentException if the given pattern is invalid
   744      */
   744      */
   745     private char[] compile(String pattern) {
   745     private char[] compile(String pattern) {
   746         int length = pattern.length();
   746         int length = pattern.length();
   747         boolean inQuote = false;
   747         boolean inQuote = false;
   748         StringBuilder compiledCode = new StringBuilder(length * 2);
   748         StringBuilder compiledCode = new StringBuilder(length * 2);
   956      * pattern {@code "h a z (zzzz)"} and the alignment field
   956      * pattern {@code "h a z (zzzz)"} and the alignment field
   957      * {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
   957      * {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
   958      * {@code fieldPosition} will be set to 5 and 8, respectively, for the
   958      * {@code fieldPosition} will be set to 5 and 8, respectively, for the
   959      * first occurrence of the timezone pattern character {@code 'z'}.
   959      * first occurrence of the timezone pattern character {@code 'z'}.
   960      * @return the formatted date-time string.
   960      * @return the formatted date-time string.
   961      * @exception NullPointerException if any of the parameters is {@code null}.
   961      * @throws    NullPointerException if any of the parameters is {@code null}.
   962      */
   962      */
   963     @Override
   963     @Override
   964     public StringBuffer format(Date date, StringBuffer toAppendTo,
   964     public StringBuffer format(Date date, StringBuffer toAppendTo,
   965                                FieldPosition pos)
   965                                FieldPosition pos)
   966     {
   966     {
  1010      * <p>
  1010      * <p>
  1011      * Each attribute key of the AttributedCharacterIterator will be of type
  1011      * Each attribute key of the AttributedCharacterIterator will be of type
  1012      * <code>DateFormat.Field</code>, with the corresponding attribute value
  1012      * <code>DateFormat.Field</code>, with the corresponding attribute value
  1013      * being the same as the attribute key.
  1013      * being the same as the attribute key.
  1014      *
  1014      *
  1015      * @exception NullPointerException if obj is null.
  1015      * @throws    NullPointerException if obj is null.
  1016      * @exception IllegalArgumentException if the Format cannot format the
  1016      * @throws    IllegalArgumentException if the Format cannot format the
  1017      *            given object, or if the Format's pattern string is invalid.
  1017      *            given object, or if the Format's pattern string is invalid.
  1018      * @param obj The object to format
  1018      * @param obj The object to format
  1019      * @return AttributedCharacterIterator describing the formatted value.
  1019      * @return AttributedCharacterIterator describing the formatted value.
  1020      * @since 1.4
  1020      * @since 1.4
  1021      */
  1021      */
  1457      * @param text  A <code>String</code>, part of which should be parsed.
  1457      * @param text  A <code>String</code>, part of which should be parsed.
  1458      * @param pos   A <code>ParsePosition</code> object with index and error
  1458      * @param pos   A <code>ParsePosition</code> object with index and error
  1459      *              index information as described above.
  1459      *              index information as described above.
  1460      * @return A <code>Date</code> parsed from the string. In case of
  1460      * @return A <code>Date</code> parsed from the string. In case of
  1461      *         error, returns null.
  1461      *         error, returns null.
  1462      * @exception NullPointerException if <code>text</code> or <code>pos</code> is null.
  1462      * @throws    NullPointerException if <code>text</code> or <code>pos</code> is null.
  1463      */
  1463      */
  1464     @Override
  1464     @Override
  1465     public Date parse(String text, ParsePosition pos)
  1465     public Date parse(String text, ParsePosition pos)
  1466     {
  1466     {
  1467         checkNegativeNumberExpression();
  1467         checkNegativeNumberExpression();
  2274 
  2274 
  2275     /**
  2275     /**
  2276      * Translates a pattern, mapping each character in the from string to the
  2276      * Translates a pattern, mapping each character in the from string to the
  2277      * corresponding character in the to string.
  2277      * corresponding character in the to string.
  2278      *
  2278      *
  2279      * @exception IllegalArgumentException if the given pattern is invalid
  2279      * @throws    IllegalArgumentException if the given pattern is invalid
  2280      */
  2280      */
  2281     private String translatePattern(String pattern, String from, String to) {
  2281     private String translatePattern(String pattern, String from, String to) {
  2282         StringBuilder result = new StringBuilder();
  2282         StringBuilder result = new StringBuilder();
  2283         boolean inQuote = false;
  2283         boolean inQuote = false;
  2284         for (int i = 0; i < pattern.length(); ++i) {
  2284         for (int i = 0; i < pattern.length(); ++i) {
  2337 
  2337 
  2338     /**
  2338     /**
  2339      * Applies the given pattern string to this date format.
  2339      * Applies the given pattern string to this date format.
  2340      *
  2340      *
  2341      * @param pattern the new date and time pattern for this date format
  2341      * @param pattern the new date and time pattern for this date format
  2342      * @exception NullPointerException if the given pattern is null
  2342      * @throws    NullPointerException if the given pattern is null
  2343      * @exception IllegalArgumentException if the given pattern is invalid
  2343      * @throws    IllegalArgumentException if the given pattern is invalid
  2344      */
  2344      */
  2345     public void applyPattern(String pattern)
  2345     public void applyPattern(String pattern)
  2346     {
  2346     {
  2347         applyPatternImpl(pattern);
  2347         applyPatternImpl(pattern);
  2348     }
  2348     }
  2355     /**
  2355     /**
  2356      * Applies the given localized pattern string to this date format.
  2356      * Applies the given localized pattern string to this date format.
  2357      *
  2357      *
  2358      * @param pattern a String to be mapped to the new date and time format
  2358      * @param pattern a String to be mapped to the new date and time format
  2359      *        pattern for this format
  2359      *        pattern for this format
  2360      * @exception NullPointerException if the given pattern is null
  2360      * @throws    NullPointerException if the given pattern is null
  2361      * @exception IllegalArgumentException if the given pattern is invalid
  2361      * @throws    IllegalArgumentException if the given pattern is invalid
  2362      */
  2362      */
  2363     public void applyLocalizedPattern(String pattern) {
  2363     public void applyLocalizedPattern(String pattern) {
  2364          String p = translatePattern(pattern,
  2364          String p = translatePattern(pattern,
  2365                                      formatData.getLocalPatternChars(),
  2365                                      formatData.getLocalPatternChars(),
  2366                                      DateFormatSymbols.patternChars);
  2366                                      DateFormatSymbols.patternChars);
  2381 
  2381 
  2382     /**
  2382     /**
  2383      * Sets the date and time format symbols of this date format.
  2383      * Sets the date and time format symbols of this date format.
  2384      *
  2384      *
  2385      * @param newFormatSymbols the new date and time format symbols
  2385      * @param newFormatSymbols the new date and time format symbols
  2386      * @exception NullPointerException if the given newFormatSymbols is null
  2386      * @throws    NullPointerException if the given newFormatSymbols is null
  2387      * @see #getDateFormatSymbols
  2387      * @see #getDateFormatSymbols
  2388      */
  2388      */
  2389     public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
  2389     public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
  2390     {
  2390     {
  2391         this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
  2391         this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
  2468 
  2468 
  2469     /**
  2469     /**
  2470      * After reading an object from the input stream, the format
  2470      * After reading an object from the input stream, the format
  2471      * pattern in the object is verified.
  2471      * pattern in the object is verified.
  2472      *
  2472      *
  2473      * @exception InvalidObjectException if the pattern is invalid
  2473      * @throws    InvalidObjectException if the pattern is invalid
  2474      */
  2474      */
  2475     @java.io.Serial
  2475     @java.io.Serial
  2476     private void readObject(ObjectInputStream stream)
  2476     private void readObject(ObjectInputStream stream)
  2477                          throws IOException, ClassNotFoundException {
  2477                          throws IOException, ClassNotFoundException {
  2478         stream.defaultReadObject();
  2478         stream.defaultReadObject();