0N/A/*
6073N/A * Copyright (c) 1996, 2013, 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-1998 - 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.IOException;
0N/Aimport java.io.InvalidObjectException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.util.Calendar;
0N/Aimport java.util.Date;
0N/Aimport java.util.GregorianCalendar;
0N/Aimport java.util.Locale;
0N/Aimport java.util.Map;
0N/Aimport java.util.MissingResourceException;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.SimpleTimeZone;
0N/Aimport java.util.TimeZone;
2962N/Aimport java.util.concurrent.ConcurrentHashMap;
2962N/Aimport java.util.concurrent.ConcurrentMap;
0N/Aimport sun.util.calendar.CalendarUtils;
0N/Aimport sun.util.calendar.ZoneInfoFile;
0N/Aimport sun.util.resources.LocaleData;
0N/A
2702N/Aimport static java.text.DateFormatSymbols.*;
2702N/A
0N/A/**
0N/A * <code>SimpleDateFormat</code> is a concrete class for formatting and
0N/A * parsing dates in a locale-sensitive manner. It allows for formatting
0N/A * (date -> text), parsing (text -> date), and normalization.
0N/A *
0N/A * <p>
0N/A * <code>SimpleDateFormat</code> allows you to start by choosing
0N/A * any user-defined patterns for date-time formatting. However, you
0N/A * are encouraged to create a date-time formatter with either
0N/A * <code>getTimeInstance</code>, <code>getDateInstance</code>, or
0N/A * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each
0N/A * of these class methods can return a date/time formatter initialized
0N/A * with a default format pattern. You may modify the format pattern
0N/A * using the <code>applyPattern</code> methods as desired.
0N/A * For more information on using these methods, see
0N/A * {@link DateFormat}.
0N/A *
0N/A * <h4>Date and Time Patterns</h4>
0N/A * <p>
0N/A * Date and time formats are specified by <em>date and time pattern</em>
0N/A * strings.
0N/A * Within date and time pattern strings, unquoted letters from
0N/A * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
0N/A * <code>'z'</code> are interpreted as pattern letters representing the
0N/A * components of a date or time string.
0N/A * Text can be quoted using single quotes (<code>'</code>) to avoid
0N/A * interpretation.
0N/A * <code>"''"</code> represents a single quote.
0N/A * All other characters are not interpreted; they're simply copied into the
0N/A * output string during formatting or matched against the input string
0N/A * during parsing.
0N/A * <p>
0N/A * The following pattern letters are defined (all other characters from
0N/A * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
0N/A * <code>'z'</code> are reserved):
0N/A * <blockquote>
0N/A * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
0N/A * <tr bgcolor="#ccccff">
0N/A * <th align=left>Letter
0N/A * <th align=left>Date or Time Component
0N/A * <th align=left>Presentation
0N/A * <th align=left>Examples
0N/A * <tr>
0N/A * <td><code>G</code>
0N/A * <td>Era designator
0N/A * <td><a href="#text">Text</a>
0N/A * <td><code>AD</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>y</code>
0N/A * <td>Year
0N/A * <td><a href="#year">Year</a>
0N/A * <td><code>1996</code>; <code>96</code>
0N/A * <tr>
2702N/A * <td><code>Y</code>
2702N/A * <td>Week year
2702N/A * <td><a href="#year">Year</a>
2702N/A * <td><code>2009</code>; <code>09</code>
2702N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>M</code>
0N/A * <td>Month in year
0N/A * <td><a href="#month">Month</a>
0N/A * <td><code>July</code>; <code>Jul</code>; <code>07</code>
2702N/A * <tr>
0N/A * <td><code>w</code>
0N/A * <td>Week in year
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>27</code>
2702N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>W</code>
0N/A * <td>Week in month
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>2</code>
2702N/A * <tr>
0N/A * <td><code>D</code>
0N/A * <td>Day in year
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>189</code>
2702N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>d</code>
0N/A * <td>Day in month
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>10</code>
2702N/A * <tr>
0N/A * <td><code>F</code>
0N/A * <td>Day of week in month
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>2</code>
2702N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>E</code>
2702N/A * <td>Day name in week
0N/A * <td><a href="#text">Text</a>
0N/A * <td><code>Tuesday</code>; <code>Tue</code>
2702N/A * <tr>
2702N/A * <td><code>u</code>
2702N/A * <td>Day number of week (1 = Monday, ..., 7 = Sunday)
2702N/A * <td><a href="#number">Number</a>
2702N/A * <td><code>1</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>a</code>
0N/A * <td>Am/pm marker
0N/A * <td><a href="#text">Text</a>
0N/A * <td><code>PM</code>
0N/A * <tr>
0N/A * <td><code>H</code>
0N/A * <td>Hour in day (0-23)
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>0</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>k</code>
0N/A * <td>Hour in day (1-24)
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>24</code>
0N/A * <tr>
0N/A * <td><code>K</code>
0N/A * <td>Hour in am/pm (0-11)
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>0</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>h</code>
0N/A * <td>Hour in am/pm (1-12)
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>12</code>
0N/A * <tr>
0N/A * <td><code>m</code>
0N/A * <td>Minute in hour
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>30</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>s</code>
0N/A * <td>Second in minute
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>55</code>
0N/A * <tr>
0N/A * <td><code>S</code>
0N/A * <td>Millisecond
0N/A * <td><a href="#number">Number</a>
0N/A * <td><code>978</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>z</code>
0N/A * <td>Time zone
0N/A * <td><a href="#timezone">General time zone</a>
0N/A * <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
0N/A * <tr>
0N/A * <td><code>Z</code>
0N/A * <td>Time zone
0N/A * <td><a href="#rfc822timezone">RFC 822 time zone</a>
0N/A * <td><code>-0800</code>
2708N/A * <tr bgcolor="#eeeeff">
2708N/A * <td><code>X</code>
2708N/A * <td>Time zone
2708N/A * <td><a href="#iso8601timezone">ISO 8601 time zone</a>
2708N/A * <td><code>-08</code>; <code>-0800</code>; <code>-08:00</code>
0N/A * </table>
0N/A * </blockquote>
0N/A * Pattern letters are usually repeated, as their number determines the
0N/A * exact presentation:
0N/A * <ul>
0N/A * <li><strong><a name="text">Text:</a></strong>
0N/A * For formatting, if the number of pattern letters is 4 or more,
0N/A * the full form is used; otherwise a short or abbreviated form
0N/A * is used if available.
0N/A * For parsing, both forms are accepted, independent of the number
2702N/A * of pattern letters.<br><br></li>
0N/A * <li><strong><a name="number">Number:</a></strong>
0N/A * For formatting, the number of pattern letters is the minimum
0N/A * number of digits, and shorter numbers are zero-padded to this amount.
0N/A * For parsing, the number of pattern letters is ignored unless
2702N/A * it's needed to separate two adjacent fields.<br><br></li>
0N/A * <li><strong><a name="year">Year:</a></strong>
0N/A * If the formatter's {@link #getCalendar() Calendar} is the Gregorian
0N/A * calendar, the following rules are applied.<br>
0N/A * <ul>
0N/A * <li>For formatting, if the number of pattern letters is 2, the year
0N/A * is truncated to 2 digits; otherwise it is interpreted as a
0N/A * <a href="#number">number</a>.
0N/A * <li>For parsing, if the number of pattern letters is more than 2,
0N/A * the year is interpreted literally, regardless of the number of
0N/A * digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to
0N/A * Jan 11, 12 A.D.
0N/A * <li>For parsing with the abbreviated year pattern ("y" or "yy"),
0N/A * <code>SimpleDateFormat</code> must interpret the abbreviated year
0N/A * relative to some century. It does this by adjusting dates to be
0N/A * within 80 years before and 20 years after the time the <code>SimpleDateFormat</code>
0N/A * instance is created. For example, using a pattern of "MM/dd/yy" and a
0N/A * <code>SimpleDateFormat</code> instance created on Jan 1, 1997, the string
0N/A * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
0N/A * would be interpreted as May 4, 1964.
0N/A * During parsing, only strings consisting of exactly two digits, as defined by
0N/A * {@link Character#isDigit(char)}, will be parsed into the default century.
0N/A * Any other numeric string, such as a one digit string, a three or more digit
0N/A * string, or a two digit string that isn't all digits (for example, "-1"), is
0N/A * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the
0N/A * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
0N/A * </ul>
0N/A * Otherwise, calendar system specific forms are applied.
0N/A * For both formatting and parsing, if the number of pattern
0N/A * letters is 4 or more, a calendar specific {@linkplain
0N/A * Calendar#LONG long form} is used. Otherwise, a calendar
0N/A * specific {@linkplain Calendar#SHORT short or abbreviated form}
2702N/A * is used.<br>
2702N/A * <br>
2702N/A * If week year {@code 'Y'} is specified and the {@linkplain
2702N/A * #getCalendar() calendar} doesn't support any <a
2702N/A * href="../util/GregorianCalendar.html#week_year"> week
2702N/A * years</a>, the calendar year ({@code 'y'}) is used instead. The
2702N/A * support of week years can be tested with a call to {@link
2702N/A * DateFormat#getCalendar() getCalendar()}.{@link
2702N/A * java.util.Calendar#isWeekDateSupported()
2702N/A * isWeekDateSupported()}.<br><br></li>
0N/A * <li><strong><a name="month">Month:</a></strong>
0N/A * If the number of pattern letters is 3 or more, the month is
0N/A * interpreted as <a href="#text">text</a>; otherwise,
2702N/A * it is interpreted as a <a href="#number">number</a>.<br><br></li>
0N/A * <li><strong><a name="timezone">General time zone:</a></strong>
0N/A * Time zones are interpreted as <a href="#text">text</a> if they have
0N/A * names. For time zones representing a GMT offset value, the
0N/A * following syntax is used:
0N/A * <pre>
0N/A * <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a>
0N/A * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
0N/A * <i>Sign:</i> one of
0N/A * <code>+ -</code>
0N/A * <i>Hours:</i>
0N/A * <i>Digit</i>
0N/A * <i>Digit</i> <i>Digit</i>
0N/A * <i>Minutes:</i>
0N/A * <i>Digit</i> <i>Digit</i>
0N/A * <i>Digit:</i> one of
0N/A * <code>0 1 2 3 4 5 6 7 8 9</code></pre>
0N/A * <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between
0N/A * 00 and 59. The format is locale independent and digits must be taken
0N/A * from the Basic Latin block of the Unicode standard.
0N/A * <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also
2702N/A * accepted.<br><br></li>
0N/A * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong>
0N/A * For formatting, the RFC 822 4-digit time zone format is used:
2708N/A *
0N/A * <pre>
0N/A * <i>RFC822TimeZone:</i>
0N/A * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
0N/A * <i>TwoDigitHours:</i>
0N/A * <i>Digit Digit</i></pre>
0N/A * <i>TwoDigitHours</i> must be between 00 and 23. Other definitions
0N/A * are as for <a href="#timezone">general time zones</a>.
2708N/A *
0N/A * <p>For parsing, <a href="#timezone">general time zones</a> are also
0N/A * accepted.
2708N/A * <li><strong><a name="iso8601timezone">ISO 8601 Time zone:</a></strong>
2708N/A * The number of pattern letters designates the format for both formatting
2708N/A * and parsing as follows:
2708N/A * <pre>
2708N/A * <i>ISO8601TimeZone:</i>
2708N/A * <i>OneLetterISO8601TimeZone</i>
2708N/A * <i>TwoLetterISO8601TimeZone</i>
2708N/A * <i>ThreeLetterISO8601TimeZone</i>
2708N/A * <i>OneLetterISO8601TimeZone:</i>
2708N/A * <i>Sign</i> <i>TwoDigitHours</i>
2708N/A * {@code Z}
2708N/A * <i>TwoLetterISO8601TimeZone:</i>
2708N/A * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
2708N/A * {@code Z}
2708N/A * <i>ThreeLetterISO8601TimeZone:</i>
2708N/A * <i>Sign</i> <i>TwoDigitHours</i> {@code :} <i>Minutes</i>
2708N/A * {@code Z}</pre>
2708N/A * Other definitions are as for <a href="#timezone">general time zones</a> or
2708N/A * <a href="#rfc822timezone">RFC 822 time zones</a>.
2708N/A *
2708N/A * <p>For formatting, if the offset value from GMT is 0, {@code "Z"} is
2708N/A * produced. If the number of pattern letters is 1, any fraction of an hour
2708N/A * is ignored. For example, if the pattern is {@code "X"} and the time zone is
2708N/A * {@code "GMT+05:30"}, {@code "+05"} is produced.
2708N/A *
2708N/A * <p>For parsing, {@code "Z"} is parsed as the UTC time zone designator.
2708N/A * <a href="#timezone">General time zones</a> are <em>not</em> accepted.
2708N/A *
2708N/A * <p>If the number of pattern letters is 4 or more, {@link
2708N/A * IllegalArgumentException} is thrown when constructing a {@code
2708N/A * SimpleDateFormat} or {@linkplain #applyPattern(String) applying a
2708N/A * pattern}.
0N/A * </ul>
0N/A * <code>SimpleDateFormat</code> also supports <em>localized date and time
0N/A * pattern</em> strings. In these strings, the pattern letters described above
0N/A * may be replaced with other, locale dependent, pattern letters.
0N/A * <code>SimpleDateFormat</code> does not deal with the localization of text
0N/A * other than the pattern letters; that's up to the client of the class.
0N/A * <p>
0N/A *
0N/A * <h4>Examples</h4>
0N/A *
0N/A * The following examples show how date and time patterns are interpreted in
0N/A * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
0N/A * in the U.S. Pacific Time time zone.
0N/A * <blockquote>
0N/A * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale">
0N/A * <tr bgcolor="#ccccff">
0N/A * <th align=left>Date and Time Pattern
0N/A * <th align=left>Result
0N/A * <tr>
0N/A * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
0N/A * <td><code>2001.07.04 AD at 12:08:56 PDT</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>"EEE, MMM d, ''yy"</code>
0N/A * <td><code>Wed, Jul 4, '01</code>
0N/A * <tr>
0N/A * <td><code>"h:mm a"</code>
0N/A * <td><code>12:08 PM</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>"hh 'o''clock' a, zzzz"</code>
0N/A * <td><code>12 o'clock PM, Pacific Daylight Time</code>
0N/A * <tr>
0N/A * <td><code>"K:mm a, z"</code>
0N/A * <td><code>0:08 PM, PDT</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
0N/A * <td><code>02001.July.04 AD 12:08 PM</code>
0N/A * <tr>
0N/A * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
0N/A * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
0N/A * <tr bgcolor="#eeeeff">
0N/A * <td><code>"yyMMddHHmmssZ"</code>
0N/A * <td><code>010704120856-0700</code>
0N/A * <tr>
0N/A * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
0N/A * <td><code>2001-07-04T12:08:56.235-0700</code>
2702N/A * <tr bgcolor="#eeeeff">
2708N/A * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"</code>
2708N/A * <td><code>2001-07-04T12:08:56.235-07:00</code>
2708N/A * <tr>
2702N/A * <td><code>"YYYY-'W'ww-u"</code>
2702N/A * <td><code>2001-W27-3</code>
0N/A * </table>
0N/A * </blockquote>
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 <a href="http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
0N/A * @see java.util.Calendar
0N/A * @see java.util.TimeZone
0N/A * @see DateFormat
0N/A * @see DateFormatSymbols
0N/A * @author Mark Davis, Chen-Lieh Huang, Alan Liu
0N/A */
0N/Apublic class SimpleDateFormat extends DateFormat {
0N/A
0N/A // the official serial version ID which says cryptically
0N/A // which version we're compatible with
0N/A static final long serialVersionUID = 4774881970558875024L;
0N/A
0N/A // the internal serial version which says which version was written
0N/A // - 0 (default) for version up to JDK 1.1.3
0N/A // - 1 for version from JDK 1.1.4, which includes a new field
0N/A static final int currentSerialVersion = 1;
0N/A
0N/A /**
0N/A * The version of the serialized data on the stream. Possible values:
0N/A * <ul>
0N/A * <li><b>0</b> or not present on stream: JDK 1.1.3. This version
0N/A * has no <code>defaultCenturyStart</code> on stream.
0N/A * <li><b>1</b> JDK 1.1.4 or later. This version adds
0N/A * <code>defaultCenturyStart</code>.
0N/A * </ul>
0N/A * When streaming out this class, the most recent format
0N/A * and the highest allowable <code>serialVersionOnStream</code>
0N/A * is written.
0N/A * @serial
0N/A * @since JDK1.1.4
0N/A */
0N/A private int serialVersionOnStream = currentSerialVersion;
0N/A
0N/A /**
0N/A * The pattern string of this formatter. This is always a non-localized
0N/A * pattern. May not be null. See class documentation for details.
0N/A * @serial
0N/A */
0N/A private String pattern;
0N/A
0N/A /**
637N/A * Saved numberFormat and pattern.
637N/A * @see SimpleDateFormat#checkNegativeNumberExpression
637N/A */
637N/A transient private NumberFormat originalNumberFormat;
637N/A transient private String originalNumberPattern;
637N/A
637N/A /**
637N/A * The minus sign to be used with format and parse.
637N/A */
637N/A transient private char minusSign = '-';
637N/A
637N/A /**
637N/A * True when a negative sign follows a number.
637N/A * (True as default in Arabic.)
637N/A */
637N/A transient private boolean hasFollowingMinusSign = false;
637N/A
637N/A /**
0N/A * The compiled pattern.
0N/A */
0N/A transient private char[] compiledPattern;
0N/A
0N/A /**
0N/A * Tags for the compiled pattern.
0N/A */
0N/A private final static int TAG_QUOTE_ASCII_CHAR = 100;
0N/A private final static int TAG_QUOTE_CHARS = 101;
0N/A
0N/A /**
0N/A * Locale dependent digit zero.
0N/A * @see #zeroPaddingNumber
0N/A * @see java.text.DecimalFormatSymbols#getZeroDigit
0N/A */
0N/A transient private char zeroDigit;
0N/A
0N/A /**
0N/A * The symbols used by this formatter for week names, month names,
0N/A * etc. May not be null.
0N/A * @serial
0N/A * @see java.text.DateFormatSymbols
0N/A */
0N/A private DateFormatSymbols formatData;
0N/A
0N/A /**
0N/A * We map dates with two-digit years into the century starting at
0N/A * <code>defaultCenturyStart</code>, which may be any date. May
0N/A * not be null.
0N/A * @serial
0N/A * @since JDK1.1.4
0N/A */
0N/A private Date defaultCenturyStart;
0N/A
0N/A transient private int defaultCenturyStartYear;
0N/A
0N/A private static final int MILLIS_PER_MINUTE = 60 * 1000;
0N/A
0N/A // For time zones that have no names, use strings GMT+minutes and
0N/A // GMT-minutes. For instance, in France the time zone is GMT+60.
0N/A private static final String GMT = "GMT";
0N/A
0N/A /**
0N/A * Cache to hold the DateTimePatterns of a Locale.
0N/A */
3699N/A private static final ConcurrentMap<Locale, String[]> cachedLocaleData
3699N/A = new ConcurrentHashMap<Locale, String[]>(3);
0N/A
0N/A /**
0N/A * Cache NumberFormat instances with Locale key.
0N/A */
2962N/A private static final ConcurrentMap<Locale, NumberFormat> cachedNumberFormatData
2962N/A = new ConcurrentHashMap<Locale, NumberFormat>(3);
0N/A
0N/A /**
0N/A * The Locale used to instantiate this
0N/A * <code>SimpleDateFormat</code>. The value may be null if this object
0N/A * has been created by an older <code>SimpleDateFormat</code> and
0N/A * deserialized.
0N/A *
0N/A * @serial
0N/A * @since 1.6
0N/A */
0N/A private Locale locale;
0N/A
0N/A /**
0N/A * Indicates whether this <code>SimpleDateFormat</code> should use
0N/A * the DateFormatSymbols. If true, the format and parse methods
0N/A * use the DateFormatSymbols values. If false, the format and
0N/A * parse methods call Calendar.getDisplayName or
0N/A * Calendar.getDisplayNames.
0N/A */
0N/A transient boolean useDateFormatSymbols;
0N/A
0N/A /**
0N/A * Constructs a <code>SimpleDateFormat</code> using the default pattern and
0N/A * date format symbols for the default locale.
0N/A * <b>Note:</b> This constructor may not support all locales.
0N/A * For full coverage, use the factory methods in the {@link DateFormat}
0N/A * class.
0N/A */
0N/A public SimpleDateFormat() {
2700N/A this(SHORT, SHORT, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>SimpleDateFormat</code> using the given pattern and
0N/A * the default date format symbols for the default locale.
0N/A * <b>Note:</b> This constructor may not support all locales.
0N/A * For full coverage, use the factory methods in the {@link DateFormat}
0N/A * class.
0N/A *
0N/A * @param pattern the pattern describing the date and time format
0N/A * @exception NullPointerException if the given pattern is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A public SimpleDateFormat(String pattern)
0N/A {
2700N/A this(pattern, Locale.getDefault(Locale.Category.FORMAT));
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>SimpleDateFormat</code> using the given pattern and
0N/A * the default date format symbols for the given locale.
0N/A * <b>Note:</b> This constructor may not support all locales.
0N/A * For full coverage, use the factory methods in the {@link DateFormat}
0N/A * class.
0N/A *
0N/A * @param pattern the pattern describing the date and time format
0N/A * @param locale the locale whose date format symbols should be used
0N/A * @exception NullPointerException if the given pattern or locale is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A public SimpleDateFormat(String pattern, Locale locale)
0N/A {
0N/A if (pattern == null || locale == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A
0N/A initializeCalendar(locale);
0N/A this.pattern = pattern;
2962N/A this.formatData = DateFormatSymbols.getInstanceRef(locale);
0N/A this.locale = locale;
0N/A initialize(locale);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a <code>SimpleDateFormat</code> using the given pattern and
0N/A * date format symbols.
0N/A *
0N/A * @param pattern the pattern describing the date and time format
0N/A * @param formatSymbols the date format symbols to be used for formatting
0N/A * @exception NullPointerException if the given pattern or formatSymbols is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
0N/A {
0N/A if (pattern == null || formatSymbols == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A
0N/A this.pattern = pattern;
0N/A this.formatData = (DateFormatSymbols) formatSymbols.clone();
2700N/A this.locale = Locale.getDefault(Locale.Category.FORMAT);
0N/A initializeCalendar(this.locale);
0N/A initialize(this.locale);
0N/A useDateFormatSymbols = true;
0N/A }
0N/A
0N/A /* Package-private, called by DateFormat factory methods */
0N/A SimpleDateFormat(int timeStyle, int dateStyle, Locale loc) {
0N/A if (loc == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A
0N/A this.locale = loc;
0N/A // initialize calendar and related fields
0N/A initializeCalendar(loc);
0N/A
0N/A /* try the cache first */
3699N/A String[] dateTimePatterns = cachedLocaleData.get(loc);
0N/A if (dateTimePatterns == null) { /* cache miss */
0N/A ResourceBundle r = LocaleData.getDateFormatData(loc);
0N/A if (!isGregorianCalendar()) {
0N/A try {
0N/A dateTimePatterns = r.getStringArray(getCalendarName() + ".DateTimePatterns");
0N/A } catch (MissingResourceException e) {
0N/A }
0N/A }
0N/A if (dateTimePatterns == null) {
0N/A dateTimePatterns = r.getStringArray("DateTimePatterns");
0N/A }
0N/A /* update cache */
3699N/A cachedLocaleData.putIfAbsent(loc, dateTimePatterns);
0N/A }
2962N/A formatData = DateFormatSymbols.getInstanceRef(loc);
0N/A if ((timeStyle >= 0) && (dateStyle >= 0)) {
0N/A Object[] dateTimeArgs = {dateTimePatterns[timeStyle],
0N/A dateTimePatterns[dateStyle + 4]};
0N/A pattern = MessageFormat.format(dateTimePatterns[8], dateTimeArgs);
0N/A }
0N/A else if (timeStyle >= 0) {
0N/A pattern = dateTimePatterns[timeStyle];
0N/A }
0N/A else if (dateStyle >= 0) {
0N/A pattern = dateTimePatterns[dateStyle + 4];
0N/A }
0N/A else {
0N/A throw new IllegalArgumentException("No date or time style specified");
0N/A }
0N/A
0N/A initialize(loc);
0N/A }
0N/A
0N/A /* Initialize compiledPattern and numberFormat fields */
0N/A private void initialize(Locale loc) {
0N/A // Verify and compile the given pattern.
0N/A compiledPattern = compile(pattern);
0N/A
0N/A /* try the cache first */
0N/A numberFormat = cachedNumberFormatData.get(loc);
0N/A if (numberFormat == null) { /* cache miss */
0N/A numberFormat = NumberFormat.getIntegerInstance(loc);
0N/A numberFormat.setGroupingUsed(false);
0N/A
0N/A /* update cache */
2962N/A cachedNumberFormatData.putIfAbsent(loc, numberFormat);
0N/A }
0N/A numberFormat = (NumberFormat) numberFormat.clone();
0N/A
0N/A initializeDefaultCentury();
0N/A }
0N/A
0N/A private void initializeCalendar(Locale loc) {
0N/A if (calendar == null) {
0N/A assert loc != null;
0N/A // The format object must be constructed using the symbols for this zone.
0N/A // However, the calendar should use the current default TimeZone.
0N/A // If this is not contained in the locale zone strings, then the zone
0N/A // will be formatted using generic GMT+/-H:MM nomenclature.
0N/A calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the compiled form of the given pattern. The syntax of
0N/A * the compiled pattern is:
0N/A * <blockquote>
0N/A * CompiledPattern:
0N/A * EntryList
0N/A * EntryList:
0N/A * Entry
0N/A * EntryList Entry
0N/A * Entry:
0N/A * TagField
0N/A * TagField data
0N/A * TagField:
0N/A * Tag Length
0N/A * TaggedData
0N/A * Tag:
0N/A * pattern_char_index
0N/A * TAG_QUOTE_CHARS
0N/A * Length:
0N/A * short_length
0N/A * long_length
0N/A * TaggedData:
0N/A * TAG_QUOTE_ASCII_CHAR ascii_char
0N/A *
0N/A * </blockquote>
0N/A *
0N/A * where `short_length' is an 8-bit unsigned integer between 0 and
0N/A * 254. `long_length' is a sequence of an 8-bit integer 255 and a
0N/A * 32-bit signed integer value which is split into upper and lower
0N/A * 16-bit fields in two char's. `pattern_char_index' is an 8-bit
0N/A * integer between 0 and 18. `ascii_char' is an 7-bit ASCII
0N/A * character value. `data' depends on its Tag value.
0N/A * <p>
0N/A * If Length is short_length, Tag and short_length are packed in a
0N/A * single char, as illustrated below.
0N/A * <blockquote>
0N/A * char[0] = (Tag << 8) | short_length;
0N/A * </blockquote>
0N/A *
0N/A * If Length is long_length, Tag and 255 are packed in the first
0N/A * char and a 32-bit integer, as illustrated below.
0N/A * <blockquote>
0N/A * char[0] = (Tag << 8) | 255;
0N/A * char[1] = (char) (long_length >>> 16);
0N/A * char[2] = (char) (long_length & 0xffff);
0N/A * </blockquote>
0N/A * <p>
0N/A * If Tag is a pattern_char_index, its Length is the number of
0N/A * pattern characters. For example, if the given pattern is
0N/A * "yyyy", Tag is 1 and Length is 4, followed by no data.
0N/A * <p>
0N/A * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's
0N/A * following the TagField. For example, if the given pattern is
0N/A * "'o''clock'", Length is 7 followed by a char sequence of
0N/A * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>.
0N/A * <p>
0N/A * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
0N/A * character in place of Length. For example, if the given pattern
0N/A * is "'o'", the TaggedData entry is
0N/A * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
0N/A *
0N/A * @exception NullPointerException if the given pattern is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A private char[] compile(String pattern) {
0N/A int length = pattern.length();
0N/A boolean inQuote = false;
0N/A StringBuilder compiledPattern = new StringBuilder(length * 2);
0N/A StringBuilder tmpBuffer = null;
0N/A int count = 0;
0N/A int lastTag = -1;
0N/A
0N/A for (int i = 0; i < length; i++) {
0N/A char c = pattern.charAt(i);
0N/A
0N/A if (c == '\'') {
0N/A // '' is treated as a single quote regardless of being
0N/A // in a quoted section.
0N/A if ((i + 1) < length) {
0N/A c = pattern.charAt(i + 1);
0N/A if (c == '\'') {
0N/A i++;
0N/A if (count != 0) {
0N/A encode(lastTag, count, compiledPattern);
0N/A lastTag = -1;
0N/A count = 0;
0N/A }
0N/A if (inQuote) {
0N/A tmpBuffer.append(c);
0N/A } else {
0N/A compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
0N/A }
0N/A continue;
0N/A }
0N/A }
0N/A if (!inQuote) {
0N/A if (count != 0) {
0N/A encode(lastTag, count, compiledPattern);
0N/A lastTag = -1;
0N/A count = 0;
0N/A }
0N/A if (tmpBuffer == null) {
0N/A tmpBuffer = new StringBuilder(length);
0N/A } else {
0N/A tmpBuffer.setLength(0);
0N/A }
0N/A inQuote = true;
0N/A } else {
0N/A int len = tmpBuffer.length();
0N/A if (len == 1) {
0N/A char ch = tmpBuffer.charAt(0);
0N/A if (ch < 128) {
0N/A compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | ch));
0N/A } else {
0N/A compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | 1));
0N/A compiledPattern.append(ch);
0N/A }
0N/A } else {
0N/A encode(TAG_QUOTE_CHARS, len, compiledPattern);
0N/A compiledPattern.append(tmpBuffer);
0N/A }
0N/A inQuote = false;
0N/A }
0N/A continue;
0N/A }
0N/A if (inQuote) {
0N/A tmpBuffer.append(c);
0N/A continue;
0N/A }
0N/A if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
0N/A if (count != 0) {
0N/A encode(lastTag, count, compiledPattern);
0N/A lastTag = -1;
0N/A count = 0;
0N/A }
0N/A if (c < 128) {
0N/A // In most cases, c would be a delimiter, such as ':'.
0N/A compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
0N/A } else {
0N/A // Take any contiguous non-ASCII alphabet characters and
0N/A // put them in a single TAG_QUOTE_CHARS.
0N/A int j;
0N/A for (j = i + 1; j < length; j++) {
0N/A char d = pattern.charAt(j);
0N/A if (d == '\'' || (d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) {
0N/A break;
0N/A }
0N/A }
0N/A compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | (j - i)));
0N/A for (; i < j; i++) {
0N/A compiledPattern.append(pattern.charAt(i));
0N/A }
0N/A i--;
0N/A }
0N/A continue;
0N/A }
0N/A
0N/A int tag;
0N/A if ((tag = DateFormatSymbols.patternChars.indexOf(c)) == -1) {
0N/A throw new IllegalArgumentException("Illegal pattern character " +
0N/A "'" + c + "'");
0N/A }
0N/A if (lastTag == -1 || lastTag == tag) {
0N/A lastTag = tag;
0N/A count++;
0N/A continue;
0N/A }
0N/A encode(lastTag, count, compiledPattern);
0N/A lastTag = tag;
0N/A count = 1;
0N/A }
0N/A
0N/A if (inQuote) {
0N/A throw new IllegalArgumentException("Unterminated quote");
0N/A }
0N/A
0N/A if (count != 0) {
0N/A encode(lastTag, count, compiledPattern);
0N/A }
0N/A
0N/A // Copy the compiled pattern to a char array
0N/A int len = compiledPattern.length();
0N/A char[] r = new char[len];
0N/A compiledPattern.getChars(0, len, r, 0);
0N/A return r;
0N/A }
0N/A
0N/A /**
0N/A * Encodes the given tag and length and puts encoded char(s) into buffer.
0N/A */
0N/A private static final void encode(int tag, int length, StringBuilder buffer) {
2708N/A if (tag == PATTERN_ISO_ZONE && length >= 4) {
2708N/A throw new IllegalArgumentException("invalid ISO 8601 format: length=" + length);
2708N/A }
0N/A if (length < 255) {
0N/A buffer.append((char)(tag << 8 | length));
0N/A } else {
0N/A buffer.append((char)((tag << 8) | 0xff));
0N/A buffer.append((char)(length >>> 16));
0N/A buffer.append((char)(length & 0xffff));
0N/A }
0N/A }
0N/A
0N/A /* Initialize the fields we use to disambiguate ambiguous years. Separate
0N/A * so we can call it from readObject().
0N/A */
0N/A private void initializeDefaultCentury() {
2962N/A calendar.setTimeInMillis(System.currentTimeMillis());
0N/A calendar.add( Calendar.YEAR, -80 );
0N/A parseAmbiguousDatesAsAfter(calendar.getTime());
0N/A }
0N/A
0N/A /* Define one-century window into which to disambiguate dates using
0N/A * two-digit years.
0N/A */
0N/A private void parseAmbiguousDatesAsAfter(Date startDate) {
0N/A defaultCenturyStart = startDate;
0N/A calendar.setTime(startDate);
0N/A defaultCenturyStartYear = calendar.get(Calendar.YEAR);
0N/A }
0N/A
0N/A /**
0N/A * Sets the 100-year period 2-digit years will be interpreted as being in
0N/A * to begin on the date the user specifies.
0N/A *
0N/A * @param startDate During parsing, two digit years will be placed in the range
0N/A * <code>startDate</code> to <code>startDate + 100 years</code>.
0N/A * @see #get2DigitYearStart
0N/A * @since 1.2
0N/A */
0N/A public void set2DigitYearStart(Date startDate) {
2962N/A parseAmbiguousDatesAsAfter(new Date(startDate.getTime()));
0N/A }
0N/A
0N/A /**
0N/A * Returns the beginning date of the 100-year period 2-digit years are interpreted
0N/A * as being within.
0N/A *
0N/A * @return the start of the 100-year period into which two digit years are
0N/A * parsed
0N/A * @see #set2DigitYearStart
0N/A * @since 1.2
0N/A */
0N/A public Date get2DigitYearStart() {
2962N/A return (Date) defaultCenturyStart.clone();
0N/A }
0N/A
0N/A /**
0N/A * Formats the given <code>Date</code> into a date/time string and appends
0N/A * the result to the given <code>StringBuffer</code>.
0N/A *
0N/A * @param date the date-time value to be formatted into a date-time string.
0N/A * @param toAppendTo where the new date-time text is to be appended.
0N/A * @param pos the formatting position. On input: an alignment field,
0N/A * if desired. On output: the offsets of the alignment field.
0N/A * @return the formatted date-time string.
2702N/A * @exception NullPointerException if the given {@code date} is {@code null}.
0N/A */
0N/A public StringBuffer format(Date date, StringBuffer toAppendTo,
0N/A FieldPosition pos)
0N/A {
0N/A pos.beginIndex = pos.endIndex = 0;
0N/A return format(date, toAppendTo, pos.getFieldDelegate());
0N/A }
0N/A
0N/A // Called from Format after creating a FieldDelegate
0N/A private StringBuffer format(Date date, StringBuffer toAppendTo,
0N/A FieldDelegate delegate) {
0N/A // Convert input date to time field list
0N/A calendar.setTime(date);
0N/A
0N/A boolean useDateFormatSymbols = useDateFormatSymbols();
0N/A
0N/A for (int i = 0; i < compiledPattern.length; ) {
0N/A int tag = compiledPattern[i] >>> 8;
0N/A int count = compiledPattern[i++] & 0xff;
0N/A if (count == 255) {
0N/A count = compiledPattern[i++] << 16;
0N/A count |= compiledPattern[i++];
0N/A }
0N/A
0N/A switch (tag) {
0N/A case TAG_QUOTE_ASCII_CHAR:
0N/A toAppendTo.append((char)count);
0N/A break;
0N/A
0N/A case TAG_QUOTE_CHARS:
0N/A toAppendTo.append(compiledPattern, i, count);
0N/A i += count;
0N/A break;
0N/A
0N/A default:
0N/A subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
0N/A break;
0N/A }
0N/A }
0N/A return toAppendTo;
0N/A }
0N/A
0N/A /**
0N/A * Formats an Object producing an <code>AttributedCharacterIterator</code>.
0N/A * You can use the returned <code>AttributedCharacterIterator</code>
0N/A * to build the resulting String, as well as to determine information
0N/A * about the resulting String.
0N/A * <p>
0N/A * Each attribute key of the AttributedCharacterIterator will be of type
0N/A * <code>DateFormat.Field</code>, with the corresponding attribute value
0N/A * being the same as the attribute key.
0N/A *
0N/A * @exception NullPointerException if obj is null.
0N/A * @exception IllegalArgumentException if the Format cannot format the
0N/A * given object, or if the Format's pattern string is invalid.
0N/A * @param obj The object to format
0N/A * @return AttributedCharacterIterator describing the formatted value.
0N/A * @since 1.4
0N/A */
0N/A public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
0N/A StringBuffer sb = new StringBuffer();
0N/A CharacterIteratorFieldDelegate delegate = new
0N/A CharacterIteratorFieldDelegate();
0N/A
0N/A if (obj instanceof Date) {
0N/A format((Date)obj, sb, delegate);
0N/A }
0N/A else if (obj instanceof Number) {
0N/A format(new Date(((Number)obj).longValue()), sb, delegate);
0N/A }
0N/A else if (obj == null) {
0N/A throw new NullPointerException(
0N/A "formatToCharacterIterator must be passed non-null object");
0N/A }
0N/A else {
0N/A throw new IllegalArgumentException(
0N/A "Cannot format given Object as a Date");
0N/A }
0N/A return delegate.getIterator(sb.toString());
0N/A }
0N/A
0N/A // Map index into pattern character string to Calendar field number
0N/A private static final int[] PATTERN_INDEX_TO_CALENDAR_FIELD =
0N/A {
0N/A Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.DATE,
0N/A Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.MINUTE,
0N/A Calendar.SECOND, Calendar.MILLISECOND, Calendar.DAY_OF_WEEK,
0N/A Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK_IN_MONTH,
0N/A Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
0N/A Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR, Calendar.ZONE_OFFSET,
2702N/A Calendar.ZONE_OFFSET,
2702N/A // Pseudo Calendar fields
2702N/A CalendarBuilder.WEEK_YEAR,
2708N/A CalendarBuilder.ISO_DAY_OF_WEEK,
2708N/A Calendar.ZONE_OFFSET
0N/A };
0N/A
0N/A // Map index into pattern character string to DateFormat field number
0N/A private static final int[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD = {
0N/A DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, DateFormat.MONTH_FIELD,
0N/A DateFormat.DATE_FIELD, DateFormat.HOUR_OF_DAY1_FIELD,
0N/A DateFormat.HOUR_OF_DAY0_FIELD, DateFormat.MINUTE_FIELD,
0N/A DateFormat.SECOND_FIELD, DateFormat.MILLISECOND_FIELD,
0N/A DateFormat.DAY_OF_WEEK_FIELD, DateFormat.DAY_OF_YEAR_FIELD,
0N/A DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD, DateFormat.WEEK_OF_YEAR_FIELD,
0N/A DateFormat.WEEK_OF_MONTH_FIELD, DateFormat.AM_PM_FIELD,
0N/A DateFormat.HOUR1_FIELD, DateFormat.HOUR0_FIELD,
0N/A DateFormat.TIMEZONE_FIELD, DateFormat.TIMEZONE_FIELD,
2708N/A DateFormat.YEAR_FIELD, DateFormat.DAY_OF_WEEK_FIELD,
2708N/A DateFormat.TIMEZONE_FIELD
0N/A };
0N/A
0N/A // Maps from DecimalFormatSymbols index to Field constant
0N/A private static final Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = {
0N/A Field.ERA, Field.YEAR, Field.MONTH, Field.DAY_OF_MONTH,
0N/A Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE,
0N/A Field.SECOND, Field.MILLISECOND, Field.DAY_OF_WEEK,
0N/A Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH,
0N/A Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH,
0N/A Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE,
0N/A Field.TIME_ZONE,
2708N/A Field.YEAR, Field.DAY_OF_WEEK,
2708N/A Field.TIME_ZONE
0N/A };
0N/A
0N/A /**
0N/A * Private member function that does the real date/time formatting.
0N/A */
0N/A private void subFormat(int patternCharIndex, int count,
0N/A FieldDelegate delegate, StringBuffer buffer,
0N/A boolean useDateFormatSymbols)
0N/A {
0N/A int maxIntCount = Integer.MAX_VALUE;
0N/A String current = null;
0N/A int beginOffset = buffer.length();
0N/A
0N/A int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
2702N/A int value;
2702N/A if (field == CalendarBuilder.WEEK_YEAR) {
2702N/A if (calendar.isWeekDateSupported()) {
2702N/A value = calendar.getWeekYear();
2702N/A } else {
2702N/A // use calendar year 'y' instead
2702N/A patternCharIndex = PATTERN_YEAR;
2702N/A field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
2702N/A value = calendar.get(field);
2702N/A }
2702N/A } else if (field == CalendarBuilder.ISO_DAY_OF_WEEK) {
2702N/A value = CalendarBuilder.toISODayOfWeek(calendar.get(Calendar.DAY_OF_WEEK));
2702N/A } else {
2702N/A value = calendar.get(field);
2702N/A }
2702N/A
0N/A int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
2702N/A if (!useDateFormatSymbols && field != CalendarBuilder.ISO_DAY_OF_WEEK) {
0N/A current = calendar.getDisplayName(field, style, locale);
0N/A }
0N/A
0N/A // Note: zeroPaddingNumber() assumes that maxDigits is either
0N/A // 2 or maxIntCount. If we make any changes to this,
0N/A // zeroPaddingNumber() must be fixed.
0N/A
0N/A switch (patternCharIndex) {
2702N/A case PATTERN_ERA: // 'G'
0N/A if (useDateFormatSymbols) {
0N/A String[] eras = formatData.getEras();
0N/A if (value < eras.length)
0N/A current = eras[value];
0N/A }
0N/A if (current == null)
0N/A current = "";
0N/A break;
0N/A
2702N/A case PATTERN_WEEK_YEAR: // 'Y'
2702N/A case PATTERN_YEAR: // 'y'
0N/A if (calendar instanceof GregorianCalendar) {
1352N/A if (count != 2)
0N/A zeroPaddingNumber(value, count, maxIntCount, buffer);
1352N/A else // count == 2
0N/A zeroPaddingNumber(value, 2, 2, buffer); // clip 1996 to 96
0N/A } else {
0N/A if (current == null) {
0N/A zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
0N/A maxIntCount, buffer);
0N/A }
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_MONTH: // 'M'
0N/A if (useDateFormatSymbols) {
0N/A String[] months;
0N/A if (count >= 4) {
0N/A months = formatData.getMonths();
0N/A current = months[value];
0N/A } else if (count == 3) {
0N/A months = formatData.getShortMonths();
0N/A current = months[value];
0N/A }
0N/A } else {
0N/A if (count < 3) {
0N/A current = null;
0N/A }
0N/A }
0N/A if (current == null) {
0N/A zeroPaddingNumber(value+1, count, maxIntCount, buffer);
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_HOUR_OF_DAY1: // 'k' 1-based. eg, 23:59 + 1 hour =>> 24:59
0N/A if (current == null) {
0N/A if (value == 0)
0N/A zeroPaddingNumber(calendar.getMaximum(Calendar.HOUR_OF_DAY)+1,
0N/A count, maxIntCount, buffer);
0N/A else
0N/A zeroPaddingNumber(value, count, maxIntCount, buffer);
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_DAY_OF_WEEK: // 'E'
0N/A if (useDateFormatSymbols) {
0N/A String[] weekdays;
0N/A if (count >= 4) {
0N/A weekdays = formatData.getWeekdays();
0N/A current = weekdays[value];
0N/A } else { // count < 4, use abbreviated form if exists
0N/A weekdays = formatData.getShortWeekdays();
0N/A current = weekdays[value];
0N/A }
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_AM_PM: // 'a'
0N/A if (useDateFormatSymbols) {
0N/A String[] ampm = formatData.getAmPmStrings();
0N/A current = ampm[value];
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_HOUR1: // 'h' 1-based. eg, 11PM + 1 hour =>> 12 AM
0N/A if (current == null) {
0N/A if (value == 0)
0N/A zeroPaddingNumber(calendar.getLeastMaximum(Calendar.HOUR)+1,
0N/A count, maxIntCount, buffer);
0N/A else
0N/A zeroPaddingNumber(value, count, maxIntCount, buffer);
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_ZONE_NAME: // 'z'
0N/A if (current == null) {
0N/A if (formatData.locale == null || formatData.isZoneStringsSet) {
0N/A int zoneIndex =
0N/A formatData.getZoneIndex(calendar.getTimeZone().getID());
0N/A if (zoneIndex == -1) {
0N/A value = calendar.get(Calendar.ZONE_OFFSET) +
0N/A calendar.get(Calendar.DST_OFFSET);
0N/A buffer.append(ZoneInfoFile.toCustomID(value));
0N/A } else {
0N/A int index = (calendar.get(Calendar.DST_OFFSET) == 0) ? 1: 3;
0N/A if (count < 4) {
0N/A // Use the short name
0N/A index++;
0N/A }
0N/A String[][] zoneStrings = formatData.getZoneStringsWrapper();
0N/A buffer.append(zoneStrings[zoneIndex][index]);
0N/A }
0N/A } else {
0N/A TimeZone tz = calendar.getTimeZone();
0N/A boolean daylight = (calendar.get(Calendar.DST_OFFSET) != 0);
0N/A int tzstyle = (count < 4 ? TimeZone.SHORT : TimeZone.LONG);
0N/A buffer.append(tz.getDisplayName(daylight, tzstyle, formatData.locale));
0N/A }
0N/A }
0N/A break;
0N/A
2702N/A case PATTERN_ZONE_VALUE: // 'Z' ("-/+hhmm" form)
0N/A value = (calendar.get(Calendar.ZONE_OFFSET) +
0N/A calendar.get(Calendar.DST_OFFSET)) / 60000;
0N/A
0N/A int width = 4;
0N/A if (value >= 0) {
0N/A buffer.append('+');
0N/A } else {
0N/A width++;
0N/A }
0N/A
0N/A int num = (value / 60) * 100 + (value % 60);
0N/A CalendarUtils.sprintf0d(buffer, num, width);
0N/A break;
0N/A
2708N/A case PATTERN_ISO_ZONE: // 'X'
2708N/A value = calendar.get(Calendar.ZONE_OFFSET)
2708N/A + calendar.get(Calendar.DST_OFFSET);
2708N/A
2708N/A if (value == 0) {
2708N/A buffer.append('Z');
2708N/A break;
2708N/A }
2708N/A
2708N/A value /= 60000;
2708N/A if (value >= 0) {
2708N/A buffer.append('+');
2708N/A } else {
2708N/A buffer.append('-');
2708N/A value = -value;
2708N/A }
2708N/A
2708N/A CalendarUtils.sprintf0d(buffer, value / 60, 2);
2708N/A if (count == 1) {
2708N/A break;
2708N/A }
2708N/A
2708N/A if (count == 3) {
2708N/A buffer.append(':');
2708N/A }
2708N/A CalendarUtils.sprintf0d(buffer, value % 60, 2);
2708N/A break;
2708N/A
0N/A default:
2702N/A // case PATTERN_DAY_OF_MONTH: // 'd'
2702N/A // case PATTERN_HOUR_OF_DAY0: // 'H' 0-based. eg, 23:59 + 1 hour =>> 00:59
2702N/A // case PATTERN_MINUTE: // 'm'
2702N/A // case PATTERN_SECOND: // 's'
2702N/A // case PATTERN_MILLISECOND: // 'S'
2702N/A // case PATTERN_DAY_OF_YEAR: // 'D'
2702N/A // case PATTERN_DAY_OF_WEEK_IN_MONTH: // 'F'
2702N/A // case PATTERN_WEEK_OF_YEAR: // 'w'
2702N/A // case PATTERN_WEEK_OF_MONTH: // 'W'
2702N/A // case PATTERN_HOUR0: // 'K' eg, 11PM + 1 hour =>> 0 AM
2702N/A // case PATTERN_ISO_DAY_OF_WEEK: // 'u' pseudo field, Monday = 1, ..., Sunday = 7
0N/A if (current == null) {
0N/A zeroPaddingNumber(value, count, maxIntCount, buffer);
0N/A }
0N/A break;
0N/A } // switch (patternCharIndex)
0N/A
0N/A if (current != null) {
0N/A buffer.append(current);
0N/A }
0N/A
0N/A int fieldID = PATTERN_INDEX_TO_DATE_FORMAT_FIELD[patternCharIndex];
0N/A Field f = PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID[patternCharIndex];
0N/A
0N/A delegate.formatted(fieldID, f, f, beginOffset, buffer.length(), buffer);
0N/A }
0N/A
0N/A /**
0N/A * Formats a number with the specified minimum and maximum number of digits.
0N/A */
0N/A private final void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer buffer)
0N/A {
0N/A // Optimization for 1, 2 and 4 digit numbers. This should
0N/A // cover most cases of formatting date/time related items.
0N/A // Note: This optimization code assumes that maxDigits is
0N/A // either 2 or Integer.MAX_VALUE (maxIntCount in format()).
0N/A try {
0N/A if (zeroDigit == 0) {
0N/A zeroDigit = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getZeroDigit();
0N/A }
0N/A if (value >= 0) {
0N/A if (value < 100 && minDigits >= 1 && minDigits <= 2) {
0N/A if (value < 10) {
0N/A if (minDigits == 2) {
0N/A buffer.append(zeroDigit);
0N/A }
0N/A buffer.append((char)(zeroDigit + value));
0N/A } else {
0N/A buffer.append((char)(zeroDigit + value / 10));
0N/A buffer.append((char)(zeroDigit + value % 10));
0N/A }
0N/A return;
0N/A } else if (value >= 1000 && value < 10000) {
0N/A if (minDigits == 4) {
0N/A buffer.append((char)(zeroDigit + value / 1000));
0N/A value %= 1000;
0N/A buffer.append((char)(zeroDigit + value / 100));
0N/A value %= 100;
0N/A buffer.append((char)(zeroDigit + value / 10));
0N/A buffer.append((char)(zeroDigit + value % 10));
0N/A return;
0N/A }
0N/A if (minDigits == 2 && maxDigits == 2) {
0N/A zeroPaddingNumber(value % 100, 2, 2, buffer);
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A } catch (Exception e) {
0N/A }
0N/A
0N/A numberFormat.setMinimumIntegerDigits(minDigits);
0N/A numberFormat.setMaximumIntegerDigits(maxDigits);
0N/A numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
0N/A }
0N/A
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 *
2293N/A * <p>This parsing operation uses the {@link DateFormat#calendar
2293N/A * calendar} to produce a {@code Date}. All of the {@code
2293N/A * calendar}'s date-time fields are {@linkplain Calendar#clear()
2293N/A * cleared} before parsing, and the {@code calendar}'s default
2293N/A * values of the date-time fields are used for any missing
2293N/A * date-time information. For example, the year value of the
2293N/A * parsed {@code Date} is 1970 with {@link GregorianCalendar} if
2293N/A * no year value is given from the parsing operation. The {@code
2293N/A * TimeZone} value may be overwritten, depending on the given
2293N/A * pattern and the time zone value in {@code text}. 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.
2293N/A *
0N/A * @param text 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>text</code> or <code>pos</code> is null.
0N/A */
0N/A public Date parse(String text, ParsePosition pos)
0N/A {
637N/A checkNegativeNumberExpression();
637N/A
0N/A int start = pos.index;
0N/A int oldStart = start;
0N/A int textLength = text.length();
0N/A
0N/A boolean[] ambiguousYear = {false};
0N/A
2702N/A CalendarBuilder calb = new CalendarBuilder();
0N/A
0N/A for (int i = 0; i < compiledPattern.length; ) {
0N/A int tag = compiledPattern[i] >>> 8;
0N/A int count = compiledPattern[i++] & 0xff;
0N/A if (count == 255) {
0N/A count = compiledPattern[i++] << 16;
0N/A count |= compiledPattern[i++];
0N/A }
0N/A
0N/A switch (tag) {
0N/A case TAG_QUOTE_ASCII_CHAR:
0N/A if (start >= textLength || text.charAt(start) != (char)count) {
0N/A pos.index = oldStart;
0N/A pos.errorIndex = start;
0N/A return null;
0N/A }
0N/A start++;
0N/A break;
0N/A
0N/A case TAG_QUOTE_CHARS:
0N/A while (count-- > 0) {
0N/A if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
0N/A pos.index = oldStart;
0N/A pos.errorIndex = start;
0N/A return null;
0N/A }
0N/A start++;
0N/A }
0N/A break;
0N/A
0N/A default:
0N/A // Peek the next pattern to determine if we need to
0N/A // obey the number of pattern letters for
0N/A // parsing. It's required when parsing contiguous
0N/A // digit text (e.g., "20010704") with a pattern which
0N/A // has no delimiters between fields, like "yyyyMMdd".
0N/A boolean obeyCount = false;
637N/A
637N/A // In Arabic, a minus sign for a negative number is put after
637N/A // the number. Even in another locale, a minus sign can be
637N/A // put after a number using DateFormat.setNumberFormat().
637N/A // If both the minus sign and the field-delimiter are '-',
637N/A // subParse() needs to determine whether a '-' after a number
637N/A // in the given text is a delimiter or is a minus sign for the
637N/A // preceding number. We give subParse() a clue based on the
637N/A // information in compiledPattern.
637N/A boolean useFollowingMinusSignAsDelimiter = false;
637N/A
0N/A if (i < compiledPattern.length) {
0N/A int nextTag = compiledPattern[i] >>> 8;
637N/A if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||
637N/A nextTag == TAG_QUOTE_CHARS)) {
0N/A obeyCount = true;
0N/A }
637N/A
637N/A if (hasFollowingMinusSign &&
637N/A (nextTag == TAG_QUOTE_ASCII_CHAR ||
637N/A nextTag == TAG_QUOTE_CHARS)) {
637N/A int c;
637N/A if (nextTag == TAG_QUOTE_ASCII_CHAR) {
637N/A c = compiledPattern[i] & 0xff;
637N/A } else {
637N/A c = compiledPattern[i+1];
637N/A }
637N/A
637N/A if (c == minusSign) {
637N/A useFollowingMinusSignAsDelimiter = true;
637N/A }
637N/A }
0N/A }
0N/A start = subParse(text, start, tag, count, obeyCount,
637N/A ambiguousYear, pos,
2702N/A useFollowingMinusSignAsDelimiter, calb);
0N/A if (start < 0) {
0N/A pos.index = oldStart;
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // At this point the fields of Calendar have been set. Calendar
0N/A // will fill in default values for missing fields when the time
0N/A // is computed.
0N/A
0N/A pos.index = start;
0N/A
0N/A Date parsedDate;
0N/A try {
2702N/A parsedDate = calb.establish(calendar).getTime();
2702N/A // If the year value is ambiguous,
2702N/A // then the two-digit year == the default start year
2702N/A if (ambiguousYear[0]) {
2702N/A if (parsedDate.before(defaultCenturyStart)) {
2702N/A parsedDate = calb.addYear(100).establish(calendar).getTime();
0N/A }
0N/A }
0N/A }
0N/A // An IllegalArgumentException will be thrown by Calendar.getTime()
0N/A // if any fields are out of range, e.g., MONTH == 17.
0N/A catch (IllegalArgumentException e) {
0N/A pos.errorIndex = start;
0N/A pos.index = oldStart;
0N/A return null;
0N/A }
0N/A
0N/A return parsedDate;
0N/A }
0N/A
0N/A /**
0N/A * Private code-size reduction function used by subParse.
0N/A * @param text the time text being parsed.
0N/A * @param start where to start parsing.
0N/A * @param field the date field being parsed.
0N/A * @param data the string array to parsed.
0N/A * @return the new start position if matching succeeded; a negative number
0N/A * indicating matching failure, otherwise.
0N/A */
2702N/A private int matchString(String text, int start, int field, String[] data, CalendarBuilder calb)
0N/A {
0N/A int i = 0;
0N/A int count = data.length;
0N/A
0N/A if (field == Calendar.DAY_OF_WEEK) i = 1;
0N/A
0N/A // There may be multiple strings in the data[] array which begin with
0N/A // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
0N/A // We keep track of the longest match, and return that. Note that this
0N/A // unfortunately requires us to test all array elements.
0N/A int bestMatchLength = 0, bestMatch = -1;
0N/A for (; i<count; ++i)
0N/A {
0N/A int length = data[i].length();
0N/A // Always compare if we have no match yet; otherwise only compare
0N/A // against potentially better matches (longer strings).
0N/A if (length > bestMatchLength &&
0N/A text.regionMatches(true, start, data[i], 0, length))
0N/A {
0N/A bestMatch = i;
0N/A bestMatchLength = length;
0N/A }
0N/A }
0N/A if (bestMatch >= 0)
0N/A {
2702N/A calb.set(field, bestMatch);
0N/A return start + bestMatchLength;
0N/A }
0N/A return -start;
0N/A }
0N/A
0N/A /**
0N/A * Performs the same thing as matchString(String, int, int,
0N/A * String[]). This method takes a Map<String, Integer> instead of
0N/A * String[].
0N/A */
2702N/A private int matchString(String text, int start, int field,
2702N/A Map<String,Integer> data, CalendarBuilder calb) {
0N/A if (data != null) {
0N/A String bestMatch = null;
0N/A
0N/A for (String name : data.keySet()) {
0N/A int length = name.length();
0N/A if (bestMatch == null || length > bestMatch.length()) {
0N/A if (text.regionMatches(true, start, name, 0, length)) {
0N/A bestMatch = name;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (bestMatch != null) {
2702N/A calb.set(field, data.get(bestMatch));
0N/A return start + bestMatch.length();
0N/A }
0N/A }
0N/A return -start;
0N/A }
0N/A
0N/A private int matchZoneString(String text, int start, String[] zoneNames) {
0N/A for (int i = 1; i <= 4; ++i) {
0N/A // Checking long and short zones [1 & 2],
0N/A // and long and short daylight [3 & 4].
0N/A String zoneName = zoneNames[i];
0N/A if (text.regionMatches(true, start,
0N/A zoneName, 0, zoneName.length())) {
0N/A return i;
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A
2702N/A private boolean matchDSTString(String text, int start, int zoneIndex, int standardIndex,
2702N/A String[][] zoneStrings) {
2702N/A int index = standardIndex + 2;
2702N/A String zoneName = zoneStrings[zoneIndex][index];
2702N/A if (text.regionMatches(true, start,
2702N/A zoneName, 0, zoneName.length())) {
2702N/A return true;
2702N/A }
2702N/A return false;
2702N/A }
2702N/A
0N/A /**
0N/A * find time zone 'text' matched zoneStrings and set to internal
0N/A * calendar.
0N/A */
2702N/A private int subParseZoneString(String text, int start, CalendarBuilder calb) {
0N/A boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
0N/A TimeZone currentTimeZone = getTimeZone();
0N/A
0N/A // At this point, check for named time zones by looking through
0N/A // the locale data from the TimeZoneNames strings.
0N/A // Want to be able to parse both short and long forms.
0N/A int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
0N/A TimeZone tz = null;
0N/A String[][] zoneStrings = formatData.getZoneStringsWrapper();
0N/A String[] zoneNames = null;
0N/A int nameIndex = 0;
0N/A if (zoneIndex != -1) {
0N/A zoneNames = zoneStrings[zoneIndex];
0N/A if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
0N/A if (nameIndex <= 2) {
0N/A // Check if the standard name (abbr) and the daylight name are the same.
0N/A useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
0N/A }
0N/A tz = TimeZone.getTimeZone(zoneNames[0]);
0N/A }
0N/A }
0N/A if (tz == null) {
0N/A zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
0N/A if (zoneIndex != -1) {
0N/A zoneNames = zoneStrings[zoneIndex];
0N/A if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
0N/A if (nameIndex <= 2) {
0N/A useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
0N/A }
0N/A tz = TimeZone.getTimeZone(zoneNames[0]);
0N/A }
0N/A }
0N/A }
2702N/A
0N/A if (tz == null) {
0N/A int len = zoneStrings.length;
0N/A for (int i = 0; i < len; i++) {
0N/A zoneNames = zoneStrings[i];
0N/A if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
0N/A if (nameIndex <= 2) {
0N/A useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
0N/A }
0N/A tz = TimeZone.getTimeZone(zoneNames[0]);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A if (tz != null) { // Matched any ?
0N/A if (!tz.equals(currentTimeZone)) {
0N/A setTimeZone(tz);
0N/A }
0N/A // If the time zone matched uses the same name
0N/A // (abbreviation) for both standard and daylight time,
0N/A // let the time zone in the Calendar decide which one.
636N/A //
636N/A // Also if tz.getDSTSaving() returns 0 for DST, use tz to
636N/A // determine the local time. (6645292)
636N/A int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
636N/A if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
6073N/A calb.clear(Calendar.ZONE_OFFSET).set(Calendar.DST_OFFSET, dstAmount);
0N/A }
0N/A return (start + zoneNames[nameIndex].length());
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
3299N/A * Parses numeric forms of time zone offset, such as "hh:mm", and
3299N/A * sets calb to the parsed value.
3299N/A *
3299N/A * @param text the text to be parsed
3299N/A * @param start the character position to start parsing
3299N/A * @param sign 1: positive; -1: negative
3299N/A * @param count 0: 'Z' or "GMT+hh:mm" parsing; 1 - 3: the number of 'X's
3299N/A * @param colon true - colon required between hh and mm; false - no colon required
3299N/A * @param calb a CalendarBuilder in which the parsed value is stored
3299N/A * @return updated parsed position, or its negative value to indicate a parsing error
3299N/A */
3299N/A private int subParseNumericZone(String text, int start, int sign, int count,
3299N/A boolean colon, CalendarBuilder calb) {
3299N/A int index = start;
3299N/A
3299N/A parse:
3299N/A try {
3299N/A char c = text.charAt(index++);
3299N/A // Parse hh
3299N/A int hours;
3299N/A if (!isDigit(c)) {
3299N/A break parse;
3299N/A }
3299N/A hours = c - '0';
3299N/A c = text.charAt(index++);
3299N/A if (isDigit(c)) {
3299N/A hours = hours * 10 + (c - '0');
3299N/A } else {
3299N/A // If no colon in RFC 822 or 'X' (ISO), two digits are
3299N/A // required.
3299N/A if (count > 0 || !colon) {
3299N/A break parse;
3299N/A }
3299N/A --index;
3299N/A }
3299N/A if (hours > 23) {
3299N/A break parse;
3299N/A }
3299N/A int minutes = 0;
3299N/A if (count != 1) {
3299N/A // Proceed with parsing mm
3299N/A c = text.charAt(index++);
3299N/A if (colon) {
3299N/A if (c != ':') {
3299N/A break parse;
3299N/A }
3299N/A c = text.charAt(index++);
3299N/A }
3299N/A if (!isDigit(c)) {
3299N/A break parse;
3299N/A }
3299N/A minutes = c - '0';
3299N/A c = text.charAt(index++);
3299N/A if (!isDigit(c)) {
3299N/A break parse;
3299N/A }
3299N/A minutes = minutes * 10 + (c - '0');
3299N/A if (minutes > 59) {
3299N/A break parse;
3299N/A }
3299N/A }
3299N/A minutes += hours * 60;
3299N/A calb.set(Calendar.ZONE_OFFSET, minutes * MILLIS_PER_MINUTE * sign)
3299N/A .set(Calendar.DST_OFFSET, 0);
3299N/A return index;
3299N/A } catch (IndexOutOfBoundsException e) {
3299N/A }
3299N/A return 1 - index; // -(index - 1)
3299N/A }
3299N/A
3299N/A private boolean isDigit(char c) {
3299N/A return c >= '0' && c <= '9';
3299N/A }
3299N/A
3299N/A /**
0N/A * Private member function that converts the parsed date strings into
0N/A * timeFields. Returns -start (for ParsePosition) if failed.
0N/A * @param text the time text to be parsed.
0N/A * @param start where to start parsing.
0N/A * @param ch the pattern character for the date field text to be parsed.
0N/A * @param count the count of a pattern character.
0N/A * @param obeyCount if true, then the next field directly abuts this one,
0N/A * and we should use the count to know when to stop parsing.
0N/A * @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
0N/A * is true, then a two-digit year was parsed and may need to be readjusted.
0N/A * @param origPos origPos.errorIndex is used to return an error index
0N/A * at which a parse error occurred, if matching failure occurs.
0N/A * @return the new start position if matching succeeded; -1 indicating
0N/A * matching failure, otherwise. In case matching failure occurred,
0N/A * an error index is set to origPos.errorIndex.
0N/A */
0N/A private int subParse(String text, int start, int patternCharIndex, int count,
0N/A boolean obeyCount, boolean[] ambiguousYear,
637N/A ParsePosition origPos,
2702N/A boolean useFollowingMinusSignAsDelimiter, CalendarBuilder calb) {
0N/A Number number = null;
0N/A int value = 0;
0N/A ParsePosition pos = new ParsePosition(0);
0N/A pos.index = start;
2702N/A if (patternCharIndex == PATTERN_WEEK_YEAR && !calendar.isWeekDateSupported()) {
2702N/A // use calendar year 'y' instead
2702N/A patternCharIndex = PATTERN_YEAR;
2702N/A }
0N/A int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
0N/A
0N/A // If there are any spaces here, skip over them. If we hit the end
0N/A // of the string, then fail.
0N/A for (;;) {
0N/A if (pos.index >= text.length()) {
0N/A origPos.errorIndex = start;
0N/A return -1;
0N/A }
0N/A char c = text.charAt(pos.index);
0N/A if (c != ' ' && c != '\t') break;
0N/A ++pos.index;
0N/A }
0N/A
0N/A parsing:
0N/A {
0N/A // We handle a few special cases here where we need to parse
0N/A // a number value. We handle further, more generic cases below. We need
0N/A // to handle some of them here because some fields require extra processing on
0N/A // the parsed value.
2702N/A if (patternCharIndex == PATTERN_HOUR_OF_DAY1 ||
2702N/A patternCharIndex == PATTERN_HOUR1 ||
2702N/A (patternCharIndex == PATTERN_MONTH && count <= 2) ||
2702N/A patternCharIndex == PATTERN_YEAR ||
2702N/A patternCharIndex == PATTERN_WEEK_YEAR) {
0N/A // It would be good to unify this with the obeyCount logic below,
0N/A // but that's going to be difficult.
0N/A if (obeyCount) {
0N/A if ((start+count) > text.length()) {
0N/A break parsing;
0N/A }
0N/A number = numberFormat.parse(text.substring(0, start+count), pos);
0N/A } else {
0N/A number = numberFormat.parse(text, pos);
0N/A }
0N/A if (number == null) {
2702N/A if (patternCharIndex != PATTERN_YEAR || calendar instanceof GregorianCalendar) {
0N/A break parsing;
0N/A }
0N/A } else {
0N/A value = number.intValue();
637N/A
637N/A if (useFollowingMinusSignAsDelimiter && (value < 0) &&
637N/A (((pos.index < text.length()) &&
637N/A (text.charAt(pos.index) != minusSign)) ||
637N/A ((pos.index == text.length()) &&
637N/A (text.charAt(pos.index-1) == minusSign)))) {
637N/A value = -value;
637N/A pos.index--;
637N/A }
0N/A }
0N/A }
0N/A
0N/A boolean useDateFormatSymbols = useDateFormatSymbols();
0N/A
0N/A int index;
0N/A switch (patternCharIndex) {
2702N/A case PATTERN_ERA: // 'G'
0N/A if (useDateFormatSymbols) {
2702N/A if ((index = matchString(text, start, Calendar.ERA, formatData.getEras(), calb)) > 0) {
0N/A return index;
0N/A }
0N/A } else {
0N/A Map<String, Integer> map = calendar.getDisplayNames(field,
0N/A Calendar.ALL_STYLES,
0N/A locale);
2702N/A if ((index = matchString(text, start, field, map, calb)) > 0) {
0N/A return index;
0N/A }
0N/A }
0N/A break parsing;
0N/A
2702N/A case PATTERN_WEEK_YEAR: // 'Y'
2702N/A case PATTERN_YEAR: // 'y'
0N/A if (!(calendar instanceof GregorianCalendar)) {
0N/A // calendar might have text representations for year values,
0N/A // such as "\u5143" in JapaneseImperialCalendar.
0N/A int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
0N/A Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
0N/A if (map != null) {
2702N/A if ((index = matchString(text, start, field, map, calb)) > 0) {
0N/A return index;
0N/A }
0N/A }
2702N/A calb.set(field, value);
0N/A return pos.index;
0N/A }
0N/A
0N/A // If there are 3 or more YEAR pattern characters, this indicates
0N/A // that the year value is to be treated literally, without any
0N/A // two-digit year adjustments (e.g., from "01" to 2001). Otherwise
0N/A // we made adjustments to place the 2-digit year in the proper
0N/A // century, for parsed strings from "00" to "99". Any other string
0N/A // is treated literally: "2250", "-1", "1", "002".
0N/A if (count <= 2 && (pos.index - start) == 2
0N/A && Character.isDigit(text.charAt(start))
2702N/A && Character.isDigit(text.charAt(start+1))) {
0N/A // Assume for example that the defaultCenturyStart is 6/18/1903.
0N/A // This means that two-digit years will be forced into the range
0N/A // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
0N/A // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
0N/A // to 1904, 1905, etc. If the year is 03, then it is 2003 if the
0N/A // other fields specify a date before 6/18, or 1903 if they specify a
0N/A // date afterwards. As a result, 03 is an ambiguous year. All other
0N/A // two-digit years are unambiguous.
0N/A int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
0N/A ambiguousYear[0] = value == ambiguousTwoDigitYear;
0N/A value += (defaultCenturyStartYear/100)*100 +
0N/A (value < ambiguousTwoDigitYear ? 100 : 0);
0N/A }
2702N/A calb.set(field, value);
0N/A return pos.index;
0N/A
2702N/A case PATTERN_MONTH: // 'M'
0N/A if (count <= 2) // i.e., M or MM.
0N/A {
0N/A // Don't want to parse the month if it is a string
0N/A // while pattern uses numeric style: M or MM.
0N/A // [We computed 'value' above.]
2702N/A calb.set(Calendar.MONTH, value - 1);
0N/A return pos.index;
0N/A }
0N/A
0N/A if (useDateFormatSymbols) {
0N/A // count >= 3 // i.e., MMM or MMMM
0N/A // Want to be able to parse both short and long forms.
0N/A // Try count == 4 first:
0N/A int newStart = 0;
0N/A if ((newStart = matchString(text, start, Calendar.MONTH,
2702N/A formatData.getMonths(), calb)) > 0) {
0N/A return newStart;
0N/A }
0N/A // count == 4 failed, now try count == 3
0N/A if ((index = matchString(text, start, Calendar.MONTH,
2702N/A formatData.getShortMonths(), calb)) > 0) {
0N/A return index;
0N/A }
0N/A } else {
0N/A Map<String, Integer> map = calendar.getDisplayNames(field,
0N/A Calendar.ALL_STYLES,
0N/A locale);
2702N/A if ((index = matchString(text, start, field, map, calb)) > 0) {
0N/A return index;
0N/A }
0N/A }
0N/A break parsing;
0N/A
2702N/A case PATTERN_HOUR_OF_DAY1: // 'k' 1-based. eg, 23:59 + 1 hour =>> 24:59
3187N/A if (!isLenient()) {
3187N/A // Validate the hour value in non-lenient
3187N/A if (value < 1 || value > 24) {
3187N/A break parsing;
3187N/A }
3187N/A }
0N/A // [We computed 'value' above.]
3187N/A if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY)+1)
3187N/A value = 0;
2702N/A calb.set(Calendar.HOUR_OF_DAY, value);
0N/A return pos.index;
0N/A
2702N/A case PATTERN_DAY_OF_WEEK: // 'E'
2702N/A {
0N/A if (useDateFormatSymbols) {
0N/A // Want to be able to parse both short and long forms.
0N/A // Try count == 4 (DDDD) first:
0N/A int newStart = 0;
0N/A if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
2702N/A formatData.getWeekdays(), calb)) > 0) {
0N/A return newStart;
0N/A }
0N/A // DDDD failed, now try DDD
0N/A if ((index = matchString(text, start, Calendar.DAY_OF_WEEK,
2702N/A formatData.getShortWeekdays(), calb)) > 0) {
0N/A return index;
0N/A }
0N/A } else {
0N/A int[] styles = { Calendar.LONG, Calendar.SHORT };
0N/A for (int style : styles) {
0N/A Map<String,Integer> map = calendar.getDisplayNames(field, style, locale);
2702N/A if ((index = matchString(text, start, field, map, calb)) > 0) {
0N/A return index;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A break parsing;
0N/A
2702N/A case PATTERN_AM_PM: // 'a'
0N/A if (useDateFormatSymbols) {
2702N/A if ((index = matchString(text, start, Calendar.AM_PM,
2702N/A formatData.getAmPmStrings(), calb)) > 0) {
0N/A return index;
0N/A }
0N/A } else {
0N/A Map<String,Integer> map = calendar.getDisplayNames(field, Calendar.ALL_STYLES, locale);
2702N/A if ((index = matchString(text, start, field, map, calb)) > 0) {
0N/A return index;
0N/A }
0N/A }
0N/A break parsing;
0N/A
2702N/A case PATTERN_HOUR1: // 'h' 1-based. eg, 11PM + 1 hour =>> 12 AM
3187N/A if (!isLenient()) {
3187N/A // Validate the hour value in non-lenient
3187N/A if (value < 1 || value > 12) {
3187N/A break parsing;
3187N/A }
3187N/A }
0N/A // [We computed 'value' above.]
3187N/A if (value == calendar.getLeastMaximum(Calendar.HOUR)+1)
3187N/A value = 0;
2702N/A calb.set(Calendar.HOUR, value);
0N/A return pos.index;
0N/A
2702N/A case PATTERN_ZONE_NAME: // 'z'
2702N/A case PATTERN_ZONE_VALUE: // 'Z'
0N/A {
0N/A int sign = 0;
3299N/A try {
3299N/A char c = text.charAt(pos.index);
3299N/A if (c == '+') {
3299N/A sign = 1;
3299N/A } else if (c == '-') {
3299N/A sign = -1;
0N/A }
3299N/A if (sign == 0) {
3299N/A // Try parsing a custom time zone "GMT+hh:mm" or "GMT".
3299N/A if ((c == 'G' || c == 'g')
3299N/A && (text.length() - start) >= GMT.length()
3299N/A && text.regionMatches(true, start, GMT, 0, GMT.length())) {
3299N/A pos.index = start + GMT.length();
0N/A
3299N/A if ((text.length() - pos.index) > 0) {
3299N/A c = text.charAt(pos.index);
3299N/A if (c == '+') {
3299N/A sign = 1;
3299N/A } else if (c == '-') {
3299N/A sign = -1;
3299N/A }
0N/A }
0N/A
3299N/A if (sign == 0) { /* "GMT" without offset */
3299N/A calb.set(Calendar.ZONE_OFFSET, 0)
3299N/A .set(Calendar.DST_OFFSET, 0);
3299N/A return pos.index;
3299N/A }
0N/A
3299N/A // Parse the rest as "hh:mm"
3299N/A int i = subParseNumericZone(text, ++pos.index,
3299N/A sign, 0, true, calb);
3299N/A if (i > 0) {
0N/A return i;
0N/A }
3299N/A pos.index = -i;
3299N/A } else {
3299N/A // Try parsing the text as a time zone
3299N/A // name or abbreviation.
3299N/A int i = subParseZoneString(text, pos.index, calb);
3299N/A if (i > 0) {
3299N/A return i;
3299N/A }
3299N/A pos.index = -i;
0N/A }
3299N/A } else {
3299N/A // Parse the rest as "hhmm" (RFC 822)
3299N/A int i = subParseNumericZone(text, ++pos.index,
3299N/A sign, 0, false, calb);
3299N/A if (i > 0) {
3299N/A return i;
0N/A }
3299N/A pos.index = -i;
0N/A }
3299N/A } catch (IndexOutOfBoundsException e) {
0N/A }
0N/A }
0N/A break parsing;
0N/A
2708N/A case PATTERN_ISO_ZONE: // 'X'
2708N/A {
3299N/A if ((text.length() - pos.index) <= 0) {
3299N/A break parsing;
3299N/A }
2708N/A
3299N/A int sign = 0;
3299N/A char c = text.charAt(pos.index);
3299N/A if (c == 'Z') {
3299N/A calb.set(Calendar.ZONE_OFFSET, 0).set(Calendar.DST_OFFSET, 0);
3299N/A return ++pos.index;
2708N/A }
2708N/A
3299N/A // parse text as "+/-hh[[:]mm]" based on count
3299N/A if (c == '+') {
3299N/A sign = 1;
3299N/A } else if (c == '-') {
3299N/A sign = -1;
3299N/A } else {
3299N/A ++pos.index;
3299N/A break parsing;
2708N/A }
3299N/A int i = subParseNumericZone(text, ++pos.index, sign, count,
3299N/A count == 3, calb);
3299N/A if (i > 0) {
3299N/A return i;
3299N/A }
3299N/A pos.index = -i;
2708N/A }
2708N/A break parsing;
2708N/A
0N/A default:
2702N/A // case PATTERN_DAY_OF_MONTH: // 'd'
2702N/A // case PATTERN_HOUR_OF_DAY0: // 'H' 0-based. eg, 23:59 + 1 hour =>> 00:59
2702N/A // case PATTERN_MINUTE: // 'm'
2702N/A // case PATTERN_SECOND: // 's'
2702N/A // case PATTERN_MILLISECOND: // 'S'
2702N/A // case PATTERN_DAY_OF_YEAR: // 'D'
2702N/A // case PATTERN_DAY_OF_WEEK_IN_MONTH: // 'F'
2702N/A // case PATTERN_WEEK_OF_YEAR: // 'w'
2702N/A // case PATTERN_WEEK_OF_MONTH: // 'W'
2702N/A // case PATTERN_HOUR0: // 'K' 0-based. eg, 11PM + 1 hour =>> 0 AM
2702N/A // case PATTERN_ISO_DAY_OF_WEEK: // 'u' (pseudo field);
0N/A
0N/A // Handle "generic" fields
0N/A if (obeyCount) {
0N/A if ((start+count) > text.length()) {
0N/A break parsing;
0N/A }
0N/A number = numberFormat.parse(text.substring(0, start+count), pos);
0N/A } else {
0N/A number = numberFormat.parse(text, pos);
0N/A }
0N/A if (number != null) {
637N/A value = number.intValue();
637N/A
637N/A if (useFollowingMinusSignAsDelimiter && (value < 0) &&
637N/A (((pos.index < text.length()) &&
637N/A (text.charAt(pos.index) != minusSign)) ||
637N/A ((pos.index == text.length()) &&
637N/A (text.charAt(pos.index-1) == minusSign)))) {
637N/A value = -value;
637N/A pos.index--;
637N/A }
637N/A
2702N/A calb.set(field, value);
0N/A return pos.index;
0N/A }
0N/A break parsing;
0N/A }
0N/A }
0N/A
0N/A // Parsing failed.
0N/A origPos.errorIndex = pos.index;
0N/A return -1;
0N/A }
0N/A
0N/A private final String getCalendarName() {
0N/A return calendar.getClass().getName();
0N/A }
0N/A
0N/A private boolean useDateFormatSymbols() {
0N/A if (useDateFormatSymbols) {
0N/A return true;
0N/A }
0N/A return isGregorianCalendar() || locale == null;
0N/A }
0N/A
0N/A private boolean isGregorianCalendar() {
0N/A return "java.util.GregorianCalendar".equals(getCalendarName());
0N/A }
0N/A
0N/A /**
0N/A * Translates a pattern, mapping each character in the from string to the
0N/A * corresponding character in the to string.
0N/A *
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A private String translatePattern(String pattern, String from, String to) {
0N/A StringBuilder result = new StringBuilder();
0N/A boolean inQuote = false;
0N/A for (int i = 0; i < pattern.length(); ++i) {
0N/A char c = pattern.charAt(i);
0N/A if (inQuote) {
0N/A if (c == '\'')
0N/A inQuote = false;
0N/A }
0N/A else {
0N/A if (c == '\'')
0N/A inQuote = true;
0N/A else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
0N/A int ci = from.indexOf(c);
2702N/A if (ci >= 0) {
2702N/A // patternChars is longer than localPatternChars due
2702N/A // to serialization compatibility. The pattern letters
2702N/A // unsupported by localPatternChars pass through.
2702N/A if (ci < to.length()) {
2702N/A c = to.charAt(ci);
2702N/A }
2702N/A } else {
0N/A throw new IllegalArgumentException("Illegal pattern " +
0N/A " character '" +
0N/A c + "'");
2702N/A }
0N/A }
0N/A }
0N/A result.append(c);
0N/A }
0N/A if (inQuote)
0N/A throw new IllegalArgumentException("Unfinished quote in pattern");
0N/A return result.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns a pattern string describing this date format.
0N/A *
0N/A * @return a pattern string describing this date format.
0N/A */
0N/A public String toPattern() {
0N/A return pattern;
0N/A }
0N/A
0N/A /**
0N/A * Returns a localized pattern string describing this date format.
0N/A *
0N/A * @return a localized pattern string describing this date format.
0N/A */
0N/A public String toLocalizedPattern() {
0N/A return translatePattern(pattern,
0N/A DateFormatSymbols.patternChars,
0N/A formatData.getLocalPatternChars());
0N/A }
0N/A
0N/A /**
0N/A * Applies the given pattern string to this date format.
0N/A *
0N/A * @param pattern the new date and time pattern for this date format
0N/A * @exception NullPointerException if the given pattern is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
2708N/A public void applyPattern(String pattern)
0N/A {
0N/A compiledPattern = compile(pattern);
0N/A this.pattern = pattern;
0N/A }
0N/A
0N/A /**
0N/A * Applies the given localized pattern string to this date format.
0N/A *
0N/A * @param pattern a String to be mapped to the new date and time format
0N/A * pattern for this format
0N/A * @exception NullPointerException if the given pattern is null
0N/A * @exception IllegalArgumentException if the given pattern is invalid
0N/A */
0N/A public void applyLocalizedPattern(String pattern) {
0N/A String p = translatePattern(pattern,
0N/A formatData.getLocalPatternChars(),
0N/A DateFormatSymbols.patternChars);
0N/A compiledPattern = compile(p);
0N/A this.pattern = p;
0N/A }
0N/A
0N/A /**
0N/A * Gets a copy of the date and time format symbols of this date format.
0N/A *
0N/A * @return the date and time format symbols of this date format
0N/A * @see #setDateFormatSymbols
0N/A */
0N/A public DateFormatSymbols getDateFormatSymbols()
0N/A {
0N/A return (DateFormatSymbols)formatData.clone();
0N/A }
0N/A
0N/A /**
0N/A * Sets the date and time format symbols of this date format.
0N/A *
0N/A * @param newFormatSymbols the new date and time format symbols
0N/A * @exception NullPointerException if the given newFormatSymbols is null
0N/A * @see #getDateFormatSymbols
0N/A */
0N/A public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
0N/A {
0N/A this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
0N/A useDateFormatSymbols = true;
0N/A }
0N/A
0N/A /**
0N/A * Creates a copy of this <code>SimpleDateFormat</code>. This also
0N/A * clones the format's date format symbols.
0N/A *
0N/A * @return a clone of this <code>SimpleDateFormat</code>
0N/A */
0N/A public Object clone() {
0N/A SimpleDateFormat other = (SimpleDateFormat) super.clone();
0N/A other.formatData = (DateFormatSymbols) formatData.clone();
0N/A return other;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this <code>SimpleDateFormat</code> object.
0N/A *
0N/A * @return the hash code value for this <code>SimpleDateFormat</code> object.
0N/A */
0N/A public int hashCode()
0N/A {
0N/A return pattern.hashCode();
0N/A // just enough fields for a reasonable distribution
0N/A }
0N/A
0N/A /**
0N/A * Compares the given object with this <code>SimpleDateFormat</code> for
0N/A * equality.
0N/A *
0N/A * @return true if the given object is equal to this
0N/A * <code>SimpleDateFormat</code>
0N/A */
0N/A public boolean equals(Object obj)
0N/A {
0N/A if (!super.equals(obj)) return false; // super does class check
0N/A SimpleDateFormat that = (SimpleDateFormat) obj;
0N/A return (pattern.equals(that.pattern)
0N/A && formatData.equals(that.formatData));
0N/A }
0N/A
0N/A /**
0N/A * After reading an object from the input stream, the format
0N/A * pattern in the object is verified.
0N/A * <p>
0N/A * @exception InvalidObjectException if the pattern is invalid
0N/A */
0N/A private void readObject(ObjectInputStream stream)
0N/A throws IOException, ClassNotFoundException {
0N/A stream.defaultReadObject();
0N/A
0N/A try {
0N/A compiledPattern = compile(pattern);
0N/A } catch (Exception e) {
0N/A throw new InvalidObjectException("invalid pattern");
0N/A }
0N/A
0N/A if (serialVersionOnStream < 1) {
0N/A // didn't have defaultCenturyStart field
0N/A initializeDefaultCentury();
0N/A }
0N/A else {
0N/A // fill in dependent transient field
0N/A parseAmbiguousDatesAsAfter(defaultCenturyStart);
0N/A }
0N/A serialVersionOnStream = currentSerialVersion;
0N/A
0N/A // If the deserialized object has a SimpleTimeZone, try
0N/A // to replace it with a ZoneInfo equivalent in order to
0N/A // be compatible with the SimpleTimeZone-based
0N/A // implementation as much as possible.
0N/A TimeZone tz = getTimeZone();
0N/A if (tz instanceof SimpleTimeZone) {
0N/A String id = tz.getID();
0N/A TimeZone zi = TimeZone.getTimeZone(id);
0N/A if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
0N/A setTimeZone(zi);
0N/A }
0N/A }
0N/A }
637N/A
637N/A /**
637N/A * Analyze the negative subpattern of DecimalFormat and set/update values
637N/A * as necessary.
637N/A */
637N/A private void checkNegativeNumberExpression() {
637N/A if ((numberFormat instanceof DecimalFormat) &&
637N/A !numberFormat.equals(originalNumberFormat)) {
637N/A String numberPattern = ((DecimalFormat)numberFormat).toPattern();
637N/A if (!numberPattern.equals(originalNumberPattern)) {
637N/A hasFollowingMinusSign = false;
637N/A
637N/A int separatorIndex = numberPattern.indexOf(';');
637N/A // If the negative subpattern is not absent, we have to analayze
637N/A // it in order to check if it has a following minus sign.
637N/A if (separatorIndex > -1) {
637N/A int minusIndex = numberPattern.indexOf('-', separatorIndex);
637N/A if ((minusIndex > numberPattern.lastIndexOf('0')) &&
637N/A (minusIndex > numberPattern.lastIndexOf('#'))) {
637N/A hasFollowingMinusSign = true;
637N/A minusSign = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getMinusSign();
637N/A }
637N/A }
637N/A originalNumberPattern = numberPattern;
637N/A }
637N/A originalNumberFormat = numberFormat;
637N/A }
637N/A }
637N/A
0N/A}