SimpleDateFormat.java revision 636
2362N/A * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 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 * 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, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved 0N/A * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved 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 * <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 * <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 * <h4>Date and Time Patterns</h4> 0N/A * Date and time formats are specified by <em>date and time pattern</em> 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 * <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 * 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 * <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 * <th align=left>Examples * <td><a href="#text">Text</a> * <td><a href="#year">Year</a> * <td><code>1996</code>; <code>96</code> * <td><a href="#month">Month</a> * <td><code>July</code>; <code>Jul</code>; <code>07</code> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td>Day of week in month * <td><a href="#number">Number</a> * <td><a href="#text">Text</a> * <td><code>Tuesday</code>; <code>Tue</code> * <td><a href="#text">Text</a> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td>Hour in am/pm (0-11) * <td><a href="#number">Number</a> * <td>Hour in am/pm (1-12) * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td><a href="#number">Number</a> * <td><a href="#timezone">General time zone</a> * <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code> * <td><a href="#rfc822timezone">RFC 822 time zone</a> * Pattern letters are usually repeated, as their number determines the * <li><strong><a name="text">Text:</a></strong> * For formatting, if the number of pattern letters is 4 or more, * the full form is used; otherwise a short or abbreviated form * For parsing, both forms are accepted, independent of the number * <li><strong><a name="number">Number:</a></strong> * For formatting, the number of pattern letters is the minimum * number of digits, and shorter numbers are zero-padded to this amount. * For parsing, the number of pattern letters is ignored unless * it's needed to separate two adjacent fields. * <li><strong><a name="year">Year:</a></strong> * If the formatter's {@link #getCalendar() Calendar} is the Gregorian * calendar, the following rules are applied.<br> * <li>For formatting, if the number of pattern letters is 2, the year * is truncated to 2 digits; otherwise it is interpreted as a * <a href="#number">number</a>. * <li>For parsing, if the number of pattern letters is more than 2, * the year is interpreted literally, regardless of the number of * digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to * <li>For parsing with the abbreviated year pattern ("y" or "yy"), * <code>SimpleDateFormat</code> must interpret the abbreviated year * relative to some century. It does this by adjusting dates to be * within 80 years before and 20 years after the time the <code>SimpleDateFormat</code> * instance is created. For example, using a pattern of "MM/dd/yy" and a * <code>SimpleDateFormat</code> instance created on Jan 1, 1997, the string * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64" * would be interpreted as May 4, 1964. * During parsing, only strings consisting of exactly two digits, as defined by * {@link Character#isDigit(char)}, will be parsed into the default century. * Any other numeric string, such as a one digit string, a three or more digit * string, or a two digit string that isn't all digits (for example, "-1"), is * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC. * Otherwise, calendar system specific forms are applied. * For both formatting and parsing, if the number of pattern * letters is 4 or more, a calendar specific {@linkplain * Calendar#LONG long form} is used. Otherwise, a calendar * specific {@linkplain Calendar#SHORT short or abbreviated form} * <li><strong><a name="month">Month:</a></strong> * If the number of pattern letters is 3 or more, the month is * interpreted as <a href="#text">text</a>; otherwise, * it is interpreted as a <a href="#number">number</a>. * <li><strong><a name="timezone">General time zone:</a></strong> * Time zones are interpreted as <a href="#text">text</a> if they have * names. For time zones representing a GMT offset value, the * following syntax is used: * <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a> * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i> * <i>Digit</i> <i>Digit</i> * <i>Digit</i> <i>Digit</i> * <code>0 1 2 3 4 5 6 7 8 9</code></pre> * <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between * 00 and 59. The format is locale independent and digits must be taken * from the Basic Latin block of the Unicode standard. * <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong> * For formatting, the RFC 822 4-digit time zone format is used: * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i> * <i>Digit Digit</i></pre> * <i>TwoDigitHours</i> must be between 00 and 23. Other definitions * are as for <a href="#timezone">general time zones</a>. * <p>For parsing, <a href="#timezone">general time zones</a> are also * <code>SimpleDateFormat</code> also supports <em>localized date and time * pattern</em> strings. In these strings, the pattern letters described above * may be replaced with other, locale dependent, pattern letters. * <code>SimpleDateFormat</code> does not deal with the localization of text * other than the pattern letters; that's up to the client of the class. * The following examples show how date and time patterns are interpreted in * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time * in the U.S. Pacific Time time zone. * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale"> * <th align=left>Date and Time Pattern * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code> * <td><code>2001.07.04 AD at 12:08:56 PDT</code> * <td><code>"EEE, MMM d, ''yy"</code> * <td><code>Wed, Jul 4, '01</code> * <td><code>"h:mm a"</code> * <td><code>12:08 PM</code> * <td><code>"hh 'o''clock' a, zzzz"</code> * <td><code>12 o'clock PM, Pacific Daylight Time</code> * <td><code>"K:mm a, z"</code> * <td><code>0:08 PM, PDT</code> * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code> * <td><code>02001.July.04 AD 12:08 PM</code> * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code> * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code> * <td><code>"yyMMddHHmmssZ"</code> * <td><code>010704120856-0700</code> * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code> * <td><code>2001-07-04T12:08:56.235-0700</code> * <h4><a name="synchronization">Synchronization</a></h4> * Date formats are not synchronized. * It is recommended to create separate format instances for each thread. * If multiple threads access a format concurrently, it must be synchronized * @see java.util.Calendar * @see java.util.TimeZone * @author Mark Davis, Chen-Lieh Huang, Alan Liu // the official serial version ID which says cryptically // which version we're compatible with // the internal serial version which says which version was written // - 0 (default) for version up to JDK 1.1.3 // - 1 for version from JDK 1.1.4, which includes a new field * The version of the serialized data on the stream. Possible values: * <li><b>0</b> or not present on stream: JDK 1.1.3. This version * has no <code>defaultCenturyStart</code> on stream. * <li><b>1</b> JDK 1.1.4 or later. This version adds * <code>defaultCenturyStart</code>. * When streaming out this class, the most recent format * and the highest allowable <code>serialVersionOnStream</code> * The pattern string of this formatter. This is always a non-localized * pattern. May not be null. See class documentation for details. * Tags for the compiled pattern. * Locale dependent digit zero. * @see #zeroPaddingNumber * @see java.text.DecimalFormatSymbols#getZeroDigit * The symbols used by this formatter for week names, month names, * @see java.text.DateFormatSymbols * We map dates with two-digit years into the century starting at * <code>defaultCenturyStart</code>, which may be any date. May // For time zones that have no names, use strings GMT+minutes and // GMT-minutes. For instance, in France the time zone is GMT+60. * Cache to hold the DateTimePatterns of a Locale. * Cache NumberFormat instances with Locale key. * The Locale used to instantiate this * <code>SimpleDateFormat</code>. The value may be null if this object * has been created by an older <code>SimpleDateFormat</code> and * Indicates whether this <code>SimpleDateFormat</code> should use * the DateFormatSymbols. If true, the format and parse methods * use the DateFormatSymbols values. If false, the format and * parse methods call Calendar.getDisplayName or * Calendar.getDisplayNames. * Constructs a <code>SimpleDateFormat</code> using the default pattern and * date format symbols for the default locale. * <b>Note:</b> This constructor may not support all locales. * For full coverage, use the factory methods in the {@link DateFormat} * Constructs a <code>SimpleDateFormat</code> using the given pattern and * the default date format symbols for the default locale. * <b>Note:</b> This constructor may not support all locales. * For full coverage, use the factory methods in the {@link DateFormat} * @param pattern the pattern describing the date and time format * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid * Constructs a <code>SimpleDateFormat</code> using the given pattern and * the default date format symbols for the given locale. * <b>Note:</b> This constructor may not support all locales. * For full coverage, use the factory methods in the {@link DateFormat} * @param pattern the pattern describing the date and time format * @param locale the locale whose date format symbols should be used * @exception NullPointerException if the given pattern or locale is null * @exception IllegalArgumentException if the given pattern is invalid * Constructs a <code>SimpleDateFormat</code> using the given pattern and * @param pattern the pattern describing the date and time format * @param formatSymbols the date format symbols to be used for formatting * @exception NullPointerException if the given pattern or formatSymbols is null * @exception IllegalArgumentException if the given pattern is invalid /* Package-private, called by DateFormat factory methods */ // initialize calendar and related fields /* try the cache first */ /* Initialize compiledPattern and numberFormat fields */ // Verify and compile the given pattern. /* try the cache first */ // The format object must be constructed using the symbols for this zone. // However, the calendar should use the current default TimeZone. // If this is not contained in the locale zone strings, then the zone // will be formatted using generic GMT+/-H:MM nomenclature. * Returns the compiled form of the given pattern. The syntax of * the compiled pattern is: * TAG_QUOTE_ASCII_CHAR ascii_char * where `short_length' is an 8-bit unsigned integer between 0 and * 254. `long_length' is a sequence of an 8-bit integer 255 and a * 32-bit signed integer value which is split into upper and lower * 16-bit fields in two char's. `pattern_char_index' is an 8-bit * integer between 0 and 18. `ascii_char' is an 7-bit ASCII * character value. `data' depends on its Tag value. * If Length is short_length, Tag and short_length are packed in a * single char, as illustrated below. * char[0] = (Tag << 8) | short_length; * If Length is long_length, Tag and 255 are packed in the first * char and a 32-bit integer, as illustrated below. * char[0] = (Tag << 8) | 255; * char[1] = (char) (long_length >>> 16); * char[2] = (char) (long_length & 0xffff); * If Tag is a pattern_char_index, its Length is the number of * pattern characters. For example, if the given pattern is * "yyyy", Tag is 1 and Length is 4, followed by no data. * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's * following the TagField. For example, if the given pattern is * "'o''clock'", Length is 7 followed by a char sequence of * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>. * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII * character in place of Length. For example, if the given pattern * is "'o'", the TaggedData entry is * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>. * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid for (
int i =
0; i <
length; i++) {
// '' is treated as a single quote regardless of being if (!(c >=
'a' && c <=
'z' || c >=
'A' && c <=
'Z')) {
// In most cases, c would be a delimiter, such as ':'. // Take any contiguous non-ASCII alphabet characters and // put them in a single TAG_QUOTE_CHARS. for (j = i +
1; j <
length; j++) {
if (d ==
'\'' || (d >=
'a' && d <=
'z' || d >=
'A' && d <=
'Z')) {
// Copy the compiled pattern to a char array char[] r =
new char[
len];
* Encodes the given tag and length and puts encoded char(s) into buffer. /* Initialize the fields we use to disambiguate ambiguous years. Separate * so we can call it from readObject(). /* Define one-century window into which to disambiguate dates using * Sets the 100-year period 2-digit years will be interpreted as being in * to begin on the date the user specifies. * @param startDate During parsing, two digit years will be placed in the range * <code>startDate</code> to <code>startDate + 100 years</code>. * @see #get2DigitYearStart * Returns the beginning date of the 100-year period 2-digit years are interpreted * @return the start of the 100-year period into which two digit years are * @see #set2DigitYearStart * Formats the given <code>Date</code> into a date/time string and appends * the result to the given <code>StringBuffer</code>. * @param date the date-time value to be formatted into a date-time string. * @param toAppendTo where the new date-time text is to be appended. * @param pos the formatting position. On input: an alignment field, * if desired. On output: the offsets of the alignment field. * @return the formatted date-time string. * @exception NullPointerException if the given date is null // Called from Format after creating a FieldDelegate // Convert input date to time field list * Formats an Object producing an <code>AttributedCharacterIterator</code>. * You can use the returned <code>AttributedCharacterIterator</code> * to build the resulting String, as well as to determine information * about the resulting String. * Each attribute key of the AttributedCharacterIterator will be of type * <code>DateFormat.Field</code>, with the corresponding attribute value * being the same as the attribute key. * @exception NullPointerException if obj is null. * @exception IllegalArgumentException if the Format cannot format the * given object, or if the Format's pattern string is invalid. * @param obj The object to format * @return AttributedCharacterIterator describing the formatted value. "formatToCharacterIterator must be passed non-null object");
"Cannot format given Object as a Date");
// Map index into pattern character string to Calendar field number // Map index into pattern character string to DateFormat field number // Maps from DecimalFormatSymbols index to Field constant * Private member function that does the real date/time formatting. // Note: zeroPaddingNumber() assumes that maxDigits is either // 2 or maxIntCount. If we make any changes to this, // zeroPaddingNumber() must be fixed. case 4:
// 'k' - HOUR_OF_DAY: 1-based. eg, 23:59 + 1 hour =>> 24:59 case 9:
// 'E' - DAY_OF_WEEK }
else {
// count < 4, use abbreviated form if exists case 15:
// 'h' - HOUR:1-based. eg, 11PM + 1 hour =>> 12 AM case 17:
// 'z' - ZONE_OFFSET case 18:
// 'Z' - ZONE_OFFSET ("-/+hhmm" form) // case 5: // 'H' - HOUR_OF_DAY:0-based. eg, 23:59 + 1 hour =>> 00:59 // case 6: // 'm' - MINUTE // case 7: // 's' - SECOND // case 8: // 'S' - MILLISECOND // case 10: // 'D' - DAY_OF_YEAR // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH // case 12: // 'w' - WEEK_OF_YEAR // case 13: // 'W' - WEEK_OF_MONTH // case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM }
// switch (patternCharIndex) * Formats a number with the specified minimum and maximum number of digits. // Optimization for 1, 2 and 4 digit numbers. This should // cover most cases of formatting date/time related items. // Note: This optimization code assumes that maxDigits is // either 2 or Integer.MAX_VALUE (maxIntCount in format()). * Parses text from a string to produce a <code>Date</code>. * The method attempts to parse text starting at the index given by * If parsing succeeds, then the index of <code>pos</code> is updated * to the index after the last character used (parsing does not necessarily * use all characters up to the end of the string), and the parsed * date is returned. The updated <code>pos</code> can be used to * indicate the starting point for the next call to this method. * If an error occurs, then the index of <code>pos</code> is not * changed, the error index of <code>pos</code> is set to the index of * the character where the error occurred, and null is returned. * @param text A <code>String</code>, part of which should be parsed. * @param pos A <code>ParsePosition</code> object with index and error * index information as described above. * @return A <code>Date</code> parsed from the string. In case of * @exception NullPointerException if <code>text</code> or <code>pos</code> is null. // Peek the next pattern to determine if we need to // obey the number of pattern letters for // parsing. It's required when parsing contiguous // digit text (e.g., "20010704") with a pattern which // has no delimiters between fields, like "yyyyMMdd". // At this point the fields of Calendar have been set. Calendar // will fill in default values for missing fields when the time // This part is a problem: When we call parsedDate.after, we compute the time. // Take the date April 3 2004 at 2:30 am. When this is first set up, the year // will be wrong if we're parsing a 2-digit year pattern. It will be 1904. // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am // on that day. It is therefore parsed out to fields as 3:30 am. Then we // add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is // a Saturday, so it can have a 2:30 am -- and it should. [LIU] Date parsedDate = calendar.getTime(); if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) { calendar.add(Calendar.YEAR, 100); parsedDate = calendar.getTime(); // Because of the above condition, save off the fields in case we need to readjust. // The procedure we use here is not particularly efficient, but there is no other // way to do this given the API restrictions present in Calendar. We minimize // inefficiency by only performing this computation when it might apply, that is, // when the two-digit year is equal to the start year, and thus might fall at the // front or the back of the default century. This only works because we adjust // the year correctly to start with in other cases -- see subParse(). if (
ambiguousYear[
0])
// If this is true then the two-digit year == the default start year // We need a copy of the fields, and we need to avoid triggering a call to // complete(), which will recalculate the fields. Since we can't access // the fields[] array in Calendar, we clone the entire object. This will // stop working if Calendar.clone() is ever rewritten to call complete(). // We can't use add here because that does a complete() first. // An IllegalArgumentException will be thrown by Calendar.getTime() // if any fields are out of range, e.g., MONTH == 17. * Private code-size reduction function used by subParse. * @param text the time text being parsed. * @param start where to start parsing. * @param field the date field being parsed. * @param data the string array to parsed. * @return the new start position if matching succeeded; a negative number * indicating matching failure, otherwise. // There may be multiple strings in the data[] array which begin with // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech). // We keep track of the longest match, and return that. Note that this // unfortunately requires us to test all array elements. // Always compare if we have no match yet; otherwise only compare // against potentially better matches (longer strings). * Performs the same thing as matchString(String, int, int, * String[]). This method takes a Map<String, Integer> instead of for (
int i =
1; i <=
4; ++i) {
// Checking long and short zones [1 & 2], // and long and short daylight [3 & 4]. * find time zone 'text' matched zoneStrings and set to internal boolean useSameName =
false;
// true if standard and daylight time use the same abbreviation. // At this point, check for named time zones by looking through // the locale data from the TimeZoneNames strings. // Want to be able to parse both short and long forms. // Check if the standard name (abbr) and the daylight name are the same. for (
int i =
0; i <
len; i++) {
if (
tz !=
null) {
// Matched any ? // If the time zone matched uses the same name // (abbreviation) for both standard and daylight time, // let the time zone in the Calendar decide which one. // Also if tz.getDSTSaving() returns 0 for DST, use tz to // determine the local time. (6645292) * Private member function that converts the parsed date strings into * timeFields. Returns -start (for ParsePosition) if failed. * @param text the time text to be parsed. * @param start where to start parsing. * @param ch the pattern character for the date field text to be parsed. * @param count the count of a pattern character. * @param obeyCount if true, then the next field directly abuts this one, * and we should use the count to know when to stop parsing. * @param ambiguousYear return parameter; upon return, if ambiguousYear[0] * is true, then a two-digit year was parsed and may need to be readjusted. * @param origPos origPos.errorIndex is used to return an error index * at which a parse error occurred, if matching failure occurs. * @return the new start position if matching succeeded; -1 indicating * matching failure, otherwise. In case matching failure occurred, * an error index is set to origPos.errorIndex. // If there are any spaces here, skip over them. If we hit the end // of the string, then fail. if (c !=
' ' && c !=
'\t')
break;
// We handle a few special cases here where we need to parse // a number value. We handle further, more generic cases below. We need // to handle some of them here because some fields require extra processing on // It would be good to unify this with the obeyCount logic below, // but that's going to be difficult. // calendar might have text representations for year values, // such as "\u5143" in JapaneseImperialCalendar. // If there are 3 or more YEAR pattern characters, this indicates // that the year value is to be treated literally, without any // two-digit year adjustments (e.g., from "01" to 2001). Otherwise // we made adjustments to place the 2-digit year in the proper // century, for parsed strings from "00" to "99". Any other string // is treated literally: "2250", "-1", "1", "002". // Assume for example that the defaultCenturyStart is 6/18/1903. // This means that two-digit years will be forced into the range // 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02 // correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond // to 1904, 1905, etc. If the year is 03, then it is 2003 if the // other fields specify a date before 6/18, or 1903 if they specify a // date afterwards. As a result, 03 is an ambiguous year. All other // two-digit years are unambiguous. if (
count <=
2)
// i.e., M or MM. // Don't want to parse the month if it is a string // while pattern uses numeric style: M or MM. // [We computed 'value' above.] // count >= 3 // i.e., MMM or MMMM // Want to be able to parse both short and long forms. // count == 4 failed, now try count == 3 case 4:
// 'k' - HOUR_OF_DAY: 1-based. eg, 23:59 + 1 hour =>> 24:59 // [We computed 'value' above.] // Want to be able to parse both short and long forms. // Try count == 4 (DDDD) first: // DDDD failed, now try DDD case 15:
// 'h' - HOUR:1-based. eg, 11PM + 1 hour =>> 12 AM // [We computed 'value' above.] case 17:
// 'z' - ZONE_OFFSET case 18:
// 'Z' - ZONE_OFFSET // First try to parse generic forms such as GMT-07:00. Do this first // in case localized TimeZoneNames contains the string "GMT" // for a zone; in that case, we don't want to match the first three // characters of GMT+/-hh:mm etc. // For time zones that have no known names, look for strings // GMT[+-]hours:minutes or try {
// try-catch for "GMT" only time zone string if (
sign ==
0) {
/* "GMT" without offset */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ // Fall through for final processing below of 'offset' and 'sign'. // If the first character is a sign, look for numeric timezones of // the form [+-]hhmm as specified by RFC 822. Otherwise, check // for named time zones by looking through the locale data from // the TimeZoneNames strings. // Try parsing the text as a time zone name (abbr). // Parse the text as an RFC 822 time zone string. This code is // actually a little more permissive than RFC 822. It will // try to do its best with numbers that aren't strictly 4 if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ if (c <
'0' || c >
'9') {
/* must be from '0' to '9'. */ // Do the final processing for both of the above cases. We only // arrive here if the form GMT+/-... or an RFC 822 form was seen. // case 5: // 'H' - HOUR_OF_DAY:0-based. eg, 23:59 + 1 hour =>> 00:59 // case 6: // 'm' - MINUTE // case 7: // 's' - SECOND // case 8: // 'S' - MILLISECOND // case 10: // 'D' - DAY_OF_YEAR // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH // case 12: // 'w' - WEEK_OF_YEAR // case 13: // 'W' - WEEK_OF_MONTH // case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM // Handle "generic" fields * Translates a pattern, mapping each character in the from string to the * corresponding character in the to string. * @exception IllegalArgumentException if the given pattern is invalid else if ((c >=
'a' && c <=
'z') || (c >=
'A' && c <=
'Z')) {
* Returns a pattern string describing this date format. * @return a pattern string describing this date format. * Returns a localized pattern string describing this date format. * @return a localized pattern string describing this date format. * Applies the given pattern string to this date format. * @param pattern the new date and time pattern for this date format * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid * Applies the given localized pattern string to this date format. * @param pattern a String to be mapped to the new date and time format * pattern for this format * @exception NullPointerException if the given pattern is null * @exception IllegalArgumentException if the given pattern is invalid * Gets a copy of the date and time format symbols of this date format. * @return the date and time format symbols of this date format * @see #setDateFormatSymbols * Sets the date and time format symbols of this date format. * @param newFormatSymbols the new date and time format symbols * @exception NullPointerException if the given newFormatSymbols is null * @see #getDateFormatSymbols * Creates a copy of this <code>SimpleDateFormat</code>. This also * clones the format's date format symbols. * @return a clone of this <code>SimpleDateFormat</code> * Returns the hash code value for this <code>SimpleDateFormat</code> object. * @return the hash code value for this <code>SimpleDateFormat</code> object. // just enough fields for a reasonable distribution * Compares the given object with this <code>SimpleDateFormat</code> for * @return true if the given object is equal to this * <code>SimpleDateFormat</code> if (!
super.
equals(
obj))
return false;
// super does class check * After reading an object from the input stream, the format * pattern in the object is verified. * @exception InvalidObjectException if the pattern is invalid // didn't have defaultCenturyStart field // fill in dependent transient field // If the deserialized object has a SimpleTimeZone, try // to replace it with a ZoneInfo equivalent in order to // be compatible with the SimpleTimeZone-based // implementation as much as possible.