jdk/src/share/classes/java/text/SimpleDateFormat.java
author naoto
Tue, 31 Aug 2010 11:27:10 -0700
changeset 6489 9e7015635425
parent 5506 202f599c92aa
child 6491 cd1bcc5057f4
permissions -rw-r--r--
4700857: RFE: separating user locale and user interface locale Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
     2
 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5283
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *   The original version of this source code and documentation is copyrighted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * materials are provided under terms of a License Agreement between Taligent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * and Sun. This technology is protected by multiple US and International
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * patents. This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *   Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.InvalidObjectException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Calendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.GregorianCalendar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.SimpleTimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.util.TimeZone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.util.calendar.CalendarUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.util.calendar.ZoneInfoFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <code>SimpleDateFormat</code> is a concrete class for formatting and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * parsing dates in a locale-sensitive manner. It allows for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * (date -> text), parsing (text -> date), and normalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>SimpleDateFormat</code> allows you to start by choosing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * any user-defined patterns for date-time formatting. However, you
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * are encouraged to create a date-time formatter with either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>getTimeInstance</code>, <code>getDateInstance</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * of these class methods can return a date/time formatter initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * with a default format pattern. You may modify the format pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * using the <code>applyPattern</code> methods as desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * For more information on using these methods, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * {@link DateFormat}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <h4>Date and Time Patterns</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Date and time formats are specified by <em>date and time pattern</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * Within date and time pattern strings, unquoted letters from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <code>'z'</code> are interpreted as pattern letters representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * components of a date or time string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Text can be quoted using single quotes (<code>'</code>) to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * interpretation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>"''"</code> represents a single quote.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * All other characters are not interpreted; they're simply copied into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * output string during formatting or matched against the input string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * during parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The following pattern letters are defined (all other characters from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <code>'z'</code> are reserved):
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     <tr bgcolor="#ccccff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *         <th align=left>Letter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *         <th align=left>Date or Time Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         <th align=left>Presentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *         <th align=left>Examples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *         <td><code>G</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         <td>Era designator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *         <td><a href="#text">Text</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *         <td><code>AD</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *         <td><code>y</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *         <td>Year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *         <td><a href="#year">Year</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *         <td><code>1996</code>; <code>96</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *         <td><code>M</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *         <td>Month in year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *         <td><a href="#month">Month</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *         <td><code>July</code>; <code>Jul</code>; <code>07</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *         <td><code>w</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *         <td>Week in year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *         <td><code>27</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *         <td><code>W</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *         <td>Week in month
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *         <td><code>2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *         <td><code>D</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *         <td>Day in year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *         <td><code>189</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *         <td><code>d</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *         <td>Day in month
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *         <td><code>10</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *         <td><code>F</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *         <td>Day of week in month
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *         <td><code>2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *         <td><code>E</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *         <td>Day in week
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *         <td><a href="#text">Text</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *         <td><code>Tuesday</code>; <code>Tue</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *         <td><code>a</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *         <td>Am/pm marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *         <td><a href="#text">Text</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *         <td><code>PM</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *         <td><code>H</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *         <td>Hour in day (0-23)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *         <td><code>0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *         <td><code>k</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *         <td>Hour in day (1-24)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *         <td><code>24</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *         <td><code>K</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *         <td>Hour in am/pm (0-11)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *         <td><code>0</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *         <td><code>h</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *         <td>Hour in am/pm (1-12)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *         <td><code>12</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *         <td><code>m</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *         <td>Minute in hour
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *         <td><code>30</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *         <td><code>s</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *         <td>Second in minute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *         <td><code>55</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *         <td><code>S</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *         <td>Millisecond
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *         <td><a href="#number">Number</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *         <td><code>978</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *         <td><code>z</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *         <td>Time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *         <td><a href="#timezone">General time zone</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *         <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *         <td><code>Z</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *         <td>Time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *         <td><a href="#rfc822timezone">RFC 822 time zone</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *         <td><code>-0800</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * Pattern letters are usually repeated, as their number determines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * exact presentation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * <li><strong><a name="text">Text:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *     For formatting, if the number of pattern letters is 4 or more,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *     the full form is used; otherwise a short or abbreviated form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *     is used if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *     For parsing, both forms are accepted, independent of the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *     of pattern letters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <li><strong><a name="number">Number:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *     For formatting, the number of pattern letters is the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *     number of digits, and shorter numbers are zero-padded to this amount.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *     For parsing, the number of pattern letters is ignored unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *     it's needed to separate two adjacent fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <li><strong><a name="year">Year:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *     If the formatter's {@link #getCalendar() Calendar} is the Gregorian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *     calendar, the following rules are applied.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *     <li>For formatting, if the number of pattern letters is 2, the year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *         is truncated to 2 digits; otherwise it is interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *         <a href="#number">number</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *     <li>For parsing, if the number of pattern letters is more than 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *         the year is interpreted literally, regardless of the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *         digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *         Jan 11, 12 A.D.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *     <li>For parsing with the abbreviated year pattern ("y" or "yy"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *         <code>SimpleDateFormat</code> must interpret the abbreviated year
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *         relative to some century.  It does this by adjusting dates to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *         within 80 years before and 20 years after the time the <code>SimpleDateFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *         instance is created. For example, using a pattern of "MM/dd/yy" and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *         <code>SimpleDateFormat</code> instance created on Jan 1, 1997,  the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *         "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *         would be interpreted as May 4, 1964.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *         During parsing, only strings consisting of exactly two digits, as defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *         {@link Character#isDigit(char)}, will be parsed into the default century.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *         Any other numeric string, such as a one digit string, a three or more digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *         string, or a two digit string that isn't all digits (for example, "-1"), is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *         interpreted literally.  So "01/02/3" or "01/02/003" are parsed, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *         same pattern, as Jan 2, 3 AD.  Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *     Otherwise, calendar system specific forms are applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *     For both formatting and parsing, if the number of pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *     letters is 4 or more, a calendar specific {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *     Calendar#LONG long form} is used. Otherwise, a calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *     specific {@linkplain Calendar#SHORT short or abbreviated form}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <li><strong><a name="month">Month:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *     If the number of pattern letters is 3 or more, the month is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *     interpreted as <a href="#text">text</a>; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *     it is interpreted as a <a href="#number">number</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * <li><strong><a name="timezone">General time zone:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *     Time zones are interpreted as <a href="#text">text</a> if they have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *     names. For time zones representing a GMT offset value, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *     following syntax is used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *     <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *             <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *     <i>Sign:</i> one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *             <code>+ -</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *     <i>Hours:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *             <i>Digit</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *             <i>Digit</i> <i>Digit</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *     <i>Minutes:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *             <i>Digit</i> <i>Digit</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *     <i>Digit:</i> one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *             <code>0 1 2 3 4 5 6 7 8 9</code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *     <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *     00 and 59. The format is locale independent and digits must be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *     from the Basic Latin block of the Unicode standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *     accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *     For formatting, the RFC 822 4-digit time zone format is used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *     <i>RFC822TimeZone:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *             <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *     <i>TwoDigitHours:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *             <i>Digit Digit</i></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *     <i>TwoDigitHours</i> must be between 00 and 23. Other definitions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *     are as for <a href="#timezone">general time zones</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *     <p>For parsing, <a href="#timezone">general time zones</a> are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *     accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * <code>SimpleDateFormat</code> also supports <em>localized date and time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * pattern</em> strings. In these strings, the pattern letters described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * may be replaced with other, locale dependent, pattern letters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 * <code>SimpleDateFormat</code> does not deal with the localization of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 * other than the pattern letters; that's up to the client of the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 * <h4>Examples</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * The following examples show how date and time patterns are interpreted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * in the U.S. Pacific Time time zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *     <tr bgcolor="#ccccff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *         <th align=left>Date and Time Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *         <th align=left>Result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *         <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *         <td><code>2001.07.04 AD at 12:08:56 PDT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *         <td><code>"EEE, MMM d, ''yy"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *         <td><code>Wed, Jul 4, '01</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *         <td><code>"h:mm a"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *         <td><code>12:08 PM</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 *         <td><code>"hh 'o''clock' a, zzzz"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *         <td><code>12 o'clock PM, Pacific Daylight Time</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *         <td><code>"K:mm a, z"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *         <td><code>0:08 PM, PDT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *         <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 *         <td><code>02001.July.04 AD 12:08 PM</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *         <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *         <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 *     <tr bgcolor="#eeeeff">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 *         <td><code>"yyMMddHHmmssZ"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *         <td><code>010704120856-0700</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 *     <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *         <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *         <td><code>2001-07-04T12:08:56.235-0700</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * <h4><a name="synchronization">Synchronization</a></h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * Date formats are not synchronized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * It is recommended to create separate format instances for each thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * If multiple threads access a format concurrently, it must be synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 * externally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 * @see          <a href="http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 * @see          java.util.Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 * @see          java.util.TimeZone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * @see          DateFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 * @see          DateFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
public class SimpleDateFormat extends DateFormat {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    // the official serial version ID which says cryptically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    // which version we're compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    static final long serialVersionUID = 4774881970558875024L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // the internal serial version which says which version was written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    // - 0 (default) for version up to JDK 1.1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    // - 1 for version from JDK 1.1.4, which includes a new field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static final int currentSerialVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * The version of the serialized data on the stream.  Possible values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <li><b>0</b> or not present on stream: JDK 1.1.3.  This version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * has no <code>defaultCenturyStart</code> on stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <li><b>1</b> JDK 1.1.4 or later.  This version adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <code>defaultCenturyStart</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * When streaming out this class, the most recent format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * and the highest allowable <code>serialVersionOnStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @since JDK1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private int serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * The pattern string of this formatter.  This is always a non-localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * pattern.  May not be null.  See class documentation for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   377
     * Saved numberFormat and pattern.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   378
     * @see SimpleDateFormat#checkNegativeNumberExpression
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   379
     */
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   380
    transient private NumberFormat originalNumberFormat;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   381
    transient private String originalNumberPattern;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   382
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   383
    /**
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   384
     * The minus sign to be used with format and parse.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   385
     */
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   386
    transient private char minusSign = '-';
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   387
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   388
    /**
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   389
     * True when a negative sign follows a number.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   390
     * (True as default in Arabic.)
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   391
     */
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   392
    transient private boolean hasFollowingMinusSign = false;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   393
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
   394
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * The compiled pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    transient private char[] compiledPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Tags for the compiled pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private final static int TAG_QUOTE_ASCII_CHAR       = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private final static int TAG_QUOTE_CHARS            = 101;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Locale dependent digit zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see #zeroPaddingNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @see java.text.DecimalFormatSymbols#getZeroDigit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    transient private char zeroDigit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * The symbols used by this formatter for week names, month names,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * etc.  May not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see java.text.DateFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private DateFormatSymbols formatData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * We map dates with two-digit years into the century starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <code>defaultCenturyStart</code>, which may be any date.  May
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * not be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @since JDK1.1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private Date defaultCenturyStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    transient private int defaultCenturyStartYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private static final int MILLIS_PER_MINUTE = 60 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // For time zones that have no names, use strings GMT+minutes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    // GMT-minutes. For instance, in France the time zone is GMT+60.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static final String GMT = "GMT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Cache to hold the DateTimePatterns of a Locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private static Hashtable<String,String[]> cachedLocaleData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        = new Hashtable<String,String[]>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Cache NumberFormat instances with Locale key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private static Hashtable<Locale,NumberFormat> cachedNumberFormatData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        = new Hashtable<Locale,NumberFormat>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * The Locale used to instantiate this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <code>SimpleDateFormat</code>. The value may be null if this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * has been created by an older <code>SimpleDateFormat</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private Locale locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Indicates whether this <code>SimpleDateFormat</code> should use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * the DateFormatSymbols. If true, the format and parse methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * use the DateFormatSymbols values. If false, the format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * parse methods call Calendar.getDisplayName or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Calendar.getDisplayNames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    transient boolean useDateFormatSymbols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Constructs a <code>SimpleDateFormat</code> using the default pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * date format symbols for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <b>Note:</b> This constructor may not support all locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * For full coverage, use the factory methods in the {@link DateFormat}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    public SimpleDateFormat() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   477
        this(SHORT, SHORT, Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * the default date format symbols for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <b>Note:</b> This constructor may not support all locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * For full coverage, use the factory methods in the {@link DateFormat}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param pattern the pattern describing the date and time format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @exception NullPointerException if the given pattern is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public SimpleDateFormat(String pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   493
        this(pattern, Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * the default date format symbols for the given locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <b>Note:</b> This constructor may not support all locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * For full coverage, use the factory methods in the {@link DateFormat}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param pattern the pattern describing the date and time format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @param locale the locale whose date format symbols should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception NullPointerException if the given pattern or locale is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public SimpleDateFormat(String pattern, Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (pattern == null || locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        initializeCalendar(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        this.formatData = DateFormatSymbols.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        initialize(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * date format symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param pattern the pattern describing the date and time format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param formatSymbols the date format symbols to be used for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @exception NullPointerException if the given pattern or formatSymbols is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (pattern == null || formatSymbols == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        this.formatData = (DateFormatSymbols) formatSymbols.clone();
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   538
        this.locale = Locale.getDefault(Locale.Category.FORMAT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        initializeCalendar(this.locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        initialize(this.locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        useDateFormatSymbols = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /* Package-private, called by DateFormat factory methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    SimpleDateFormat(int timeStyle, int dateStyle, Locale loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        this.locale = loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // initialize calendar and related fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        initializeCalendar(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        /* try the cache first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        String key = getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        String[] dateTimePatterns = cachedLocaleData.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (dateTimePatterns == null) { /* cache miss */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            ResourceBundle r = LocaleData.getDateFormatData(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (!isGregorianCalendar()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    dateTimePatterns = r.getStringArray(getCalendarName() + ".DateTimePatterns");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (dateTimePatterns == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                dateTimePatterns = r.getStringArray("DateTimePatterns");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            /* update cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            cachedLocaleData.put(key, dateTimePatterns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        formatData = DateFormatSymbols.getInstance(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if ((timeStyle >= 0) && (dateStyle >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            Object[] dateTimeArgs = {dateTimePatterns[timeStyle],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                     dateTimePatterns[dateStyle + 4]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            pattern = MessageFormat.format(dateTimePatterns[8], dateTimeArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        else if (timeStyle >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            pattern = dateTimePatterns[timeStyle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        else if (dateStyle >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            pattern = dateTimePatterns[dateStyle + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            throw new IllegalArgumentException("No date or time style specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        initialize(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /* Initialize compiledPattern and numberFormat fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    private void initialize(Locale loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // Verify and compile the given pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        compiledPattern = compile(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        /* try the cache first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        numberFormat = cachedNumberFormatData.get(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (numberFormat == null) { /* cache miss */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            numberFormat = NumberFormat.getIntegerInstance(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            numberFormat.setGroupingUsed(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            /* update cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            cachedNumberFormatData.put(loc, numberFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        numberFormat = (NumberFormat) numberFormat.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        initializeDefaultCentury();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private void initializeCalendar(Locale loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (calendar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            assert loc != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            // The format object must be constructed using the symbols for this zone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            // However, the calendar should use the current default TimeZone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // If this is not contained in the locale zone strings, then the zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // will be formatted using generic GMT+/-H:MM nomenclature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private String getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        sb.append(getCalendarName()).append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        sb.append(locale.getLanguage()).append('_').append(locale.getCountry()).append('_').append(locale.getVariant());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Returns the compiled form of the given pattern. The syntax of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * the compiled pattern is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * CompiledPattern:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *     EntryList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * EntryList:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *     Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *     EntryList Entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *     TagField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *     TagField data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * TagField:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *     Tag Length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *     TaggedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Tag:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *     pattern_char_index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *     TAG_QUOTE_CHARS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * Length:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *     short_length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *     long_length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * TaggedData:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *     TAG_QUOTE_ASCII_CHAR ascii_char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * where `short_length' is an 8-bit unsigned integer between 0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * 254.  `long_length' is a sequence of an 8-bit integer 255 and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * 32-bit signed integer value which is split into upper and lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * 16-bit fields in two char's. `pattern_char_index' is an 8-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * integer between 0 and 18. `ascii_char' is an 7-bit ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * character value. `data' depends on its Tag value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * If Length is short_length, Tag and short_length are packed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * single char, as illustrated below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *     char[0] = (Tag << 8) | short_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * If Length is long_length, Tag and 255 are packed in the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * char and a 32-bit integer, as illustrated below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *     char[0] = (Tag << 8) | 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *     char[1] = (char) (long_length >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *     char[2] = (char) (long_length & 0xffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * If Tag is a pattern_char_index, its Length is the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * pattern characters. For example, if the given pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * "yyyy", Tag is 1 and Length is 4, followed by no data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * following the TagField. For example, if the given pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * "'o''clock'", Length is 7 followed by a char sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * character in place of Length. For example, if the given pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * is "'o'", the TaggedData entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @exception NullPointerException if the given pattern is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    private char[] compile(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        int length = pattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        StringBuilder compiledPattern = new StringBuilder(length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        StringBuilder tmpBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int lastTag = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        for (int i = 0; i < length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            char c = pattern.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            if (c == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                // '' is treated as a single quote regardless of being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                // in a quoted section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if ((i + 1) < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    c = pattern.charAt(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    if (c == '\'') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            encode(lastTag, count, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                            lastTag = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                            count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                            tmpBuffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                            compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                if (!inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        encode(lastTag, count, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        lastTag = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    if (tmpBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                        tmpBuffer = new StringBuilder(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        tmpBuffer.setLength(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    int len = tmpBuffer.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    if (len == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        char ch = tmpBuffer.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        if (ch < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                            compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                            compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            compiledPattern.append(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        encode(TAG_QUOTE_CHARS, len, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        compiledPattern.append(tmpBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                tmpBuffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    encode(lastTag, count, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    lastTag = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                if (c < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    // In most cases, c would be a delimiter, such as ':'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    // Take any contiguous non-ASCII alphabet characters and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    // put them in a single TAG_QUOTE_CHARS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    for (j = i + 1; j < length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        char d = pattern.charAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        if (d == '\'' || (d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | (j - i)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    for (; i < j; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        compiledPattern.append(pattern.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                    i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            int tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if ((tag = DateFormatSymbols.patternChars.indexOf(c)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                throw new IllegalArgumentException("Illegal pattern character " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                                   "'" + c + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (lastTag == -1 || lastTag == tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                lastTag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            encode(lastTag, count, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            lastTag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            count = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            throw new IllegalArgumentException("Unterminated quote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if (count != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            encode(lastTag, count, compiledPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // Copy the compiled pattern to a char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        int len = compiledPattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        char[] r = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        compiledPattern.getChars(0, len, r, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Encodes the given tag and length and puts encoded char(s) into buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    private static final void encode(int tag, int length, StringBuilder buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (length < 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            buffer.append((char)(tag << 8 | length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            buffer.append((char)((tag << 8) | 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            buffer.append((char)(length >>> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            buffer.append((char)(length & 0xffff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    /* Initialize the fields we use to disambiguate ambiguous years. Separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * so we can call it from readObject().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    private void initializeDefaultCentury() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        calendar.setTime( new Date() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        calendar.add( Calendar.YEAR, -80 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        parseAmbiguousDatesAsAfter(calendar.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /* Define one-century window into which to disambiguate dates using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * two-digit years.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    private void parseAmbiguousDatesAsAfter(Date startDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        defaultCenturyStart = startDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        calendar.setTime(startDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        defaultCenturyStartYear = calendar.get(Calendar.YEAR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Sets the 100-year period 2-digit years will be interpreted as being in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * to begin on the date the user specifies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @param startDate During parsing, two digit years will be placed in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <code>startDate</code> to <code>startDate + 100 years</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @see #get2DigitYearStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public void set2DigitYearStart(Date startDate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        parseAmbiguousDatesAsAfter(startDate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Returns the beginning date of the 100-year period 2-digit years are interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * as being within.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @return the start of the 100-year period into which two digit years are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @see #set2DigitYearStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public Date get2DigitYearStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        return defaultCenturyStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Formats the given <code>Date</code> into a date/time string and appends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * the result to the given <code>StringBuffer</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param date the date-time value to be formatted into a date-time string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @param toAppendTo where the new date-time text is to be appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @param pos the formatting position. On input: an alignment field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * if desired. On output: the offsets of the alignment field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @return the formatted date-time string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @exception NullPointerException if the given date is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    public StringBuffer format(Date date, StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                               FieldPosition pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        pos.beginIndex = pos.endIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        return format(date, toAppendTo, pos.getFieldDelegate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    // Called from Format after creating a FieldDelegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    private StringBuffer format(Date date, StringBuffer toAppendTo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                                FieldDelegate delegate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        // Convert input date to time field list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        calendar.setTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        boolean useDateFormatSymbols = useDateFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        for (int i = 0; i < compiledPattern.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            int tag = compiledPattern[i] >>> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            int count = compiledPattern[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            if (count == 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                count = compiledPattern[i++] << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                count |= compiledPattern[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            case TAG_QUOTE_ASCII_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                toAppendTo.append((char)count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            case TAG_QUOTE_CHARS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                toAppendTo.append(compiledPattern, i, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                i += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        return toAppendTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Formats an Object producing an <code>AttributedCharacterIterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * You can use the returned <code>AttributedCharacterIterator</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * to build the resulting String, as well as to determine information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * about the resulting String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Each attribute key of the AttributedCharacterIterator will be of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * <code>DateFormat.Field</code>, with the corresponding attribute value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * being the same as the attribute key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * @exception NullPointerException if obj is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @exception IllegalArgumentException if the Format cannot format the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *            given object, or if the Format's pattern string is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @param obj The object to format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @return AttributedCharacterIterator describing the formatted value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        CharacterIteratorFieldDelegate delegate = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                         CharacterIteratorFieldDelegate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (obj instanceof Date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            format((Date)obj, sb, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        else if (obj instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            format(new Date(((Number)obj).longValue()), sb, delegate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        else if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                   "formatToCharacterIterator must be passed non-null object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                             "Cannot format given Object as a Date");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return delegate.getIterator(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    // Map index into pattern character string to Calendar field number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    private static final int[] PATTERN_INDEX_TO_CALENDAR_FIELD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.DATE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.MINUTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        Calendar.SECOND, Calendar.MILLISECOND, Calendar.DAY_OF_WEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK_IN_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR, Calendar.ZONE_OFFSET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        Calendar.ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    // Map index into pattern character string to DateFormat field number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    private static final int[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, DateFormat.MONTH_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        DateFormat.DATE_FIELD, DateFormat.HOUR_OF_DAY1_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        DateFormat.HOUR_OF_DAY0_FIELD, DateFormat.MINUTE_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        DateFormat.SECOND_FIELD, DateFormat.MILLISECOND_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        DateFormat.DAY_OF_WEEK_FIELD, DateFormat.DAY_OF_YEAR_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD, DateFormat.WEEK_OF_YEAR_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        DateFormat.WEEK_OF_MONTH_FIELD, DateFormat.AM_PM_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        DateFormat.HOUR1_FIELD, DateFormat.HOUR0_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        DateFormat.TIMEZONE_FIELD, DateFormat.TIMEZONE_FIELD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    // Maps from DecimalFormatSymbols index to Field constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    private static final Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        Field.ERA, Field.YEAR, Field.MONTH, Field.DAY_OF_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        Field.SECOND, Field.MILLISECOND, Field.DAY_OF_WEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        Field.TIME_ZONE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Private member function that does the real date/time formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    private void subFormat(int patternCharIndex, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                           FieldDelegate delegate, StringBuffer buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                           boolean useDateFormatSymbols)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        int     maxIntCount = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        String  current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        int     beginOffset = buffer.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        int value = calendar.get(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        if (!useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            current = calendar.getDisplayName(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        // Note: zeroPaddingNumber() assumes that maxDigits is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        // 2 or maxIntCount. If we make any changes to this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // zeroPaddingNumber() must be fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        switch (patternCharIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        case 0: // 'G' - ERA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                String[] eras = formatData.getEras();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                if (value < eras.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    current = eras[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            if (current == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                current = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        case 1: // 'y' - YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            if (calendar instanceof GregorianCalendar) {
3098
2b04258e7975 6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents: 1639
diff changeset
  1033
                if (count != 2)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    zeroPaddingNumber(value, count, maxIntCount, buffer);
3098
2b04258e7975 6609750: [Fmt-De] SimpleDateFormat.format() doesn't handle pattern "y" correctly
peytoia
parents: 1639
diff changeset
  1035
                else // count == 2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    zeroPaddingNumber(value, 2, 2, buffer); // clip 1996 to 96
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                      maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        case 2: // 'M' - MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                String[] months;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                if (count >= 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                    months = formatData.getMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    current = months[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                } else if (count == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    months = formatData.getShortMonths();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    current = months[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                if (count < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                zeroPaddingNumber(value+1, count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        case 4: // 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                if (value == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    zeroPaddingNumber(calendar.getMaximum(Calendar.HOUR_OF_DAY)+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                                      count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    zeroPaddingNumber(value, count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        case 9: // 'E' - DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                String[] weekdays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                if (count >= 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    weekdays = formatData.getWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    current = weekdays[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                } else { // count < 4, use abbreviated form if exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    weekdays = formatData.getShortWeekdays();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    current = weekdays[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        case 14:    // 'a' - AM_PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                String[] ampm = formatData.getAmPmStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                current = ampm[value];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        case 15: // 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                if (value == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    zeroPaddingNumber(calendar.getLeastMaximum(Calendar.HOUR)+1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                      count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    zeroPaddingNumber(value, count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        case 17: // 'z' - ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                if (formatData.locale == null || formatData.isZoneStringsSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                    int zoneIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                        formatData.getZoneIndex(calendar.getTimeZone().getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    if (zoneIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                        value = calendar.get(Calendar.ZONE_OFFSET) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                            calendar.get(Calendar.DST_OFFSET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                        buffer.append(ZoneInfoFile.toCustomID(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                        int index = (calendar.get(Calendar.DST_OFFSET) == 0) ? 1: 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                        if (count < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                            // Use the short name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                        String[][] zoneStrings = formatData.getZoneStringsWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        buffer.append(zoneStrings[zoneIndex][index]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    TimeZone tz = calendar.getTimeZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    boolean daylight = (calendar.get(Calendar.DST_OFFSET) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    int tzstyle = (count < 4 ? TimeZone.SHORT : TimeZone.LONG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    buffer.append(tz.getDisplayName(daylight, tzstyle, formatData.locale));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        case 18: // 'Z' - ZONE_OFFSET ("-/+hhmm" form)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            value = (calendar.get(Calendar.ZONE_OFFSET) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                     calendar.get(Calendar.DST_OFFSET)) / 60000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            int width = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                buffer.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                width++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            int num = (value / 60) * 100 + (value % 60);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            CalendarUtils.sprintf0d(buffer, num, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            // case 3: // 'd' - DATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            // case 5: // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            // case 6: // 'm' - MINUTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            // case 7: // 's' - SECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            // case 8: // 'S' - MILLISECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            // case 10: // 'D' - DAY_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            // case 12: // 'w' - WEEK_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            // case 13: // 'W' - WEEK_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            // case 16: // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (current == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                zeroPaddingNumber(value, count, maxIntCount, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        } // switch (patternCharIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (current != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            buffer.append(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        int fieldID = PATTERN_INDEX_TO_DATE_FORMAT_FIELD[patternCharIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        Field f = PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID[patternCharIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        delegate.formatted(fieldID, f, f, beginOffset, buffer.length(), buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * Formats a number with the specified minimum and maximum number of digits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    private final void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        // Optimization for 1, 2 and 4 digit numbers. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        // cover most cases of formatting date/time related items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        // Note: This optimization code assumes that maxDigits is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        // either 2 or Integer.MAX_VALUE (maxIntCount in format()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            if (zeroDigit == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                zeroDigit = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getZeroDigit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (value >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                if (value < 100 && minDigits >= 1 && minDigits <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                    if (value < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                        if (minDigits == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                            buffer.append(zeroDigit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                        buffer.append((char)(zeroDigit + value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                        buffer.append((char)(zeroDigit + value / 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                        buffer.append((char)(zeroDigit + value % 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                } else if (value >= 1000 && value < 10000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    if (minDigits == 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                        buffer.append((char)(zeroDigit + value / 1000));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                        value %= 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                        buffer.append((char)(zeroDigit + value / 100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                        value %= 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                        buffer.append((char)(zeroDigit + value / 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                        buffer.append((char)(zeroDigit + value % 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    if (minDigits == 2 && maxDigits == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                        zeroPaddingNumber(value % 100, 2, 2, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        numberFormat.setMinimumIntegerDigits(minDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        numberFormat.setMaximumIntegerDigits(maxDigits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * Parses text from a string to produce a <code>Date</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * The method attempts to parse text starting at the index given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * <code>pos</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * If parsing succeeds, then the index of <code>pos</code> is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * to the index after the last character used (parsing does not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * use all characters up to the end of the string), and the parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * date is returned. The updated <code>pos</code> can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * indicate the starting point for the next call to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * If an error occurs, then the index of <code>pos</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * changed, the error index of <code>pos</code> is set to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * the character where the error occurred, and null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *
5283
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1238
     * <p>This parsing operation uses the {@link DateFormat#calendar
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1239
     * calendar} to produce a {@code Date}. All of the {@code
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1240
     * calendar}'s date-time fields are {@linkplain Calendar#clear()
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1241
     * cleared} before parsing, and the {@code calendar}'s default
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1242
     * values of the date-time fields are used for any missing
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1243
     * date-time information. For example, the year value of the
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1244
     * parsed {@code Date} is 1970 with {@link GregorianCalendar} if
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1245
     * no year value is given from the parsing operation.  The {@code
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1246
     * TimeZone} value may be overwritten, depending on the given
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1247
     * pattern and the time zone value in {@code text}. Any {@code
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1248
     * TimeZone} value that has previously been set by a call to
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1249
     * {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1250
     * to be restored for further operations.
c440afed922d 6609675: [Fmt-Da] DateFormat.parse() on a timezone changes its calendar's timezone
okutsu
parents: 3098
diff changeset
  1251
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * @param text  A <code>String</code>, part of which should be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param pos   A <code>ParsePosition</code> object with index and error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     *              index information as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return A <code>Date</code> parsed from the string. In case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *         error, returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * @exception NullPointerException if <code>text</code> or <code>pos</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    public Date parse(String text, ParsePosition pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    {
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1261
        checkNegativeNumberExpression();
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1262
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        int start = pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        int oldStart = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        int textLength = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        calendar.clear(); // Clears all the time fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        boolean[] ambiguousYear = {false};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        for (int i = 0; i < compiledPattern.length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            int tag = compiledPattern[i] >>> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            int count = compiledPattern[i++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            if (count == 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                count = compiledPattern[i++] << 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                count |= compiledPattern[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            switch (tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            case TAG_QUOTE_ASCII_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                if (start >= textLength || text.charAt(start) != (char)count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    pos.index = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    pos.errorIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            case TAG_QUOTE_CHARS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                while (count-- > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                        pos.index = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                        pos.errorIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    start++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                // Peek the next pattern to determine if we need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                // obey the number of pattern letters for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                // parsing. It's required when parsing contiguous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                // digit text (e.g., "20010704") with a pattern which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                // has no delimiters between fields, like "yyyyMMdd".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                boolean obeyCount = false;
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1308
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1309
                // In Arabic, a minus sign for a negative number is put after
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1310
                // the number. Even in another locale, a minus sign can be
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1311
                // put after a number using DateFormat.setNumberFormat().
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1312
                // If both the minus sign and the field-delimiter are '-',
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1313
                // subParse() needs to determine whether a '-' after a number
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1314
                // in the given text is a delimiter or is a minus sign for the
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1315
                // preceding number. We give subParse() a clue based on the
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1316
                // information in compiledPattern.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1317
                boolean useFollowingMinusSignAsDelimiter = false;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1318
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                if (i < compiledPattern.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    int nextTag = compiledPattern[i] >>> 8;
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1321
                    if (!(nextTag == TAG_QUOTE_ASCII_CHAR ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1322
                          nextTag == TAG_QUOTE_CHARS)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                        obeyCount = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    }
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1325
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1326
                    if (hasFollowingMinusSign &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1327
                        (nextTag == TAG_QUOTE_ASCII_CHAR ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1328
                         nextTag == TAG_QUOTE_CHARS)) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1329
                        int c;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1330
                        if (nextTag == TAG_QUOTE_ASCII_CHAR) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1331
                            c = compiledPattern[i] & 0xff;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1332
                        } else {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1333
                            c = compiledPattern[i+1];
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1334
                        }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1335
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1336
                        if (c == minusSign) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1337
                            useFollowingMinusSignAsDelimiter = true;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1338
                        }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1339
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                start = subParse(text, start, tag, count, obeyCount,
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1342
                                 ambiguousYear, pos,
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1343
                                 useFollowingMinusSignAsDelimiter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                if (start < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    pos.index = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        // At this point the fields of Calendar have been set.  Calendar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        // will fill in default values for missing fields when the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        // is computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        pos.index = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        // This part is a problem:  When we call parsedDate.after, we compute the time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        // Take the date April 3 2004 at 2:30 am.  When this is first set up, the year
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // will be wrong if we're parsing a 2-digit year pattern.  It will be 1904.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day.  2:30 am
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        // on that day.  It is therefore parsed out to fields as 3:30 am.  Then we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        // add 100 years, and get April 3 2004 at 3:30 am.  Note that April 3 2004 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        Date parsedDate = calendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            calendar.add(Calendar.YEAR, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            parsedDate = calendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        // Because of the above condition, save off the fields in case we need to readjust.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        // The procedure we use here is not particularly efficient, but there is no other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        // way to do this given the API restrictions present in Calendar.  We minimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        // inefficiency by only performing this computation when it might apply, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        // when the two-digit year is equal to the start year, and thus might fall at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        // front or the back of the default century.  This only works because we adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        // the year correctly to start with in other cases -- see subParse().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        Date parsedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                // We need a copy of the fields, and we need to avoid triggering a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                // complete(), which will recalculate the fields.  Since we can't access
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                // the fields[] array in Calendar, we clone the entire object.  This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                // stop working if Calendar.clone() is ever rewritten to call complete().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                Calendar savedCalendar = (Calendar)calendar.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                parsedDate = calendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                if (parsedDate.before(defaultCenturyStart))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    // We can't use add here because that does a complete() first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                    parsedDate = savedCalendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            else parsedDate = calendar.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        // An IllegalArgumentException will be thrown by Calendar.getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        // if any fields are out of range, e.g., MONTH == 17.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            pos.errorIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            pos.index = oldStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        return parsedDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * Private code-size reduction function used by subParse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @param text the time text being parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * @param start where to start parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @param field the date field being parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * @param data the string array to parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * @return the new start position if matching succeeded; a negative number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * indicating matching failure, otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    private int matchString(String text, int start, int field, String[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        int count = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        if (field == Calendar.DAY_OF_WEEK) i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        // There may be multiple strings in the data[] array which begin with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        // We keep track of the longest match, and return that.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        // unfortunately requires us to test all array elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        int bestMatchLength = 0, bestMatch = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        for (; i<count; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            int length = data[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            // Always compare if we have no match yet; otherwise only compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
            // against potentially better matches (longer strings).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            if (length > bestMatchLength &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                text.regionMatches(true, start, data[i], 0, length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                bestMatch = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                bestMatchLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        if (bestMatch >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            calendar.set(field, bestMatch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            return start + bestMatchLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        return -start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * Performs the same thing as matchString(String, int, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * String[]). This method takes a Map<String, Integer> instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * String[].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    private int matchString(String text, int start, int field, Map<String,Integer> data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            String bestMatch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            for (String name : data.keySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                int length = name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                if (bestMatch == null || length > bestMatch.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    if (text.regionMatches(true, start, name, 0, length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                        bestMatch = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            if (bestMatch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                calendar.set(field, data.get(bestMatch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                return start + bestMatch.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        return -start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    private int matchZoneString(String text, int start, String[] zoneNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        for (int i = 1; i <= 4; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            // Checking long and short zones [1 & 2],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            // and long and short daylight [3 & 4].
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            String zoneName = zoneNames[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            if (text.regionMatches(true, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                                   zoneName, 0, zoneName.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * find time zone 'text' matched zoneStrings and set to internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * calendar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    private int subParseZoneString(String text, int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        TimeZone currentTimeZone = getTimeZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        // At this point, check for named time zones by looking through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        // the locale data from the TimeZoneNames strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        // Want to be able to parse both short and long forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        int zoneIndex = formatData.getZoneIndex(currentTimeZone.getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        TimeZone tz = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        String[][] zoneStrings = formatData.getZoneStringsWrapper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        String[] zoneNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        int nameIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        if (zoneIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            zoneNames = zoneStrings[zoneIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                if (nameIndex <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    // Check if the standard name (abbr) and the daylight name are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                tz = TimeZone.getTimeZone(zoneNames[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        if (tz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            zoneIndex = formatData.getZoneIndex(TimeZone.getDefault().getID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            if (zoneIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                zoneNames = zoneStrings[zoneIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                    if (nameIndex <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                        useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                    tz = TimeZone.getTimeZone(zoneNames[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        if (tz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            int len = zoneStrings.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                zoneNames = zoneStrings[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                if ((nameIndex = matchZoneString(text, start, zoneNames)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                    if (nameIndex <= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                        useSameName = zoneNames[nameIndex].equalsIgnoreCase(zoneNames[nameIndex + 2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    tz = TimeZone.getTimeZone(zoneNames[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        if (tz != null) { // Matched any ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
            if (!tz.equals(currentTimeZone)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                setTimeZone(tz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
            // If the time zone matched uses the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            // (abbreviation) for both standard and daylight time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            // let the time zone in the Calendar decide which one.
1312
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1547
            //
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1548
            // Also if tz.getDSTSaving() returns 0 for DST, use tz to
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1549
            // determine the local time. (6645292)
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1550
            int dstAmount = (nameIndex >= 3) ? tz.getDSTSavings() : 0;
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1551
            if (!(useSameName || (nameIndex >= 3 && dstAmount == 0))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
1312
880799c54c4d 6645292: [Fmt-Da] Timezone Western Summer Time (Australia) is parsed incorrectly
peytoia
parents: 2
diff changeset
  1553
                calendar.set(Calendar.DST_OFFSET, dstAmount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            return (start + zoneNames[nameIndex].length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * Private member function that converts the parsed date strings into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * timeFields. Returns -start (for ParsePosition) if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * @param text the time text to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @param start where to start parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @param ch the pattern character for the date field text to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * @param count the count of a pattern character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @param obeyCount if true, then the next field directly abuts this one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * and we should use the count to know when to stop parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * is true, then a two-digit year was parsed and may need to be readjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * @param origPos origPos.errorIndex is used to return an error index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * at which a parse error occurred, if matching failure occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * @return the new start position if matching succeeded; -1 indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * matching failure, otherwise. In case matching failure occurred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * an error index is set to origPos.errorIndex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    private int subParse(String text, int start, int patternCharIndex, int count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                         boolean obeyCount, boolean[] ambiguousYear,
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1579
                         ParsePosition origPos,
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1580
                         boolean useFollowingMinusSignAsDelimiter) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        Number number = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        int value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        ParsePosition pos = new ParsePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        pos.index = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        // If there are any spaces here, skip over them.  If we hit the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        // of the string, then fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            if (pos.index >= text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
                origPos.errorIndex = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            char c = text.charAt(pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            if (c != ' ' && c != '\t') break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            ++pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
      parsing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            // We handle a few special cases here where we need to parse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            // a number value.  We handle further, more generic cases below.  We need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            // to handle some of them here because some fields require extra processing on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            // the parsed value.
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1605
            if (patternCharIndex == 4 /* HOUR_OF_DAY1_FIELD */ ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1606
                patternCharIndex == 15 /* HOUR1_FIELD */ ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1607
                (patternCharIndex == 2 /* MONTH_FIELD */ && count <= 2) ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1608
                patternCharIndex == 1 /* YEAR_FIELD */) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                // It would be good to unify this with the obeyCount logic below,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
                // but that's going to be difficult.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                if (obeyCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                    if ((start+count) > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                        break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                    number = numberFormat.parse(text.substring(0, start+count), pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    number = numberFormat.parse(text, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                if (number == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    if (patternCharIndex != 1 || calendar instanceof GregorianCalendar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                        break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                    value = number.intValue();
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1625
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1626
                    if (useFollowingMinusSignAsDelimiter && (value < 0) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1627
                        (((pos.index < text.length()) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1628
                         (text.charAt(pos.index) != minusSign)) ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1629
                         ((pos.index == text.length()) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1630
                          (text.charAt(pos.index-1) == minusSign)))) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1631
                        value = -value;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1632
                        pos.index--;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1633
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            boolean useDateFormatSymbols = useDateFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            switch (patternCharIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            case 0: // 'G' - ERA
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                    if ((index = matchString(text, start, Calendar.ERA, formatData.getEras())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                    Map<String, Integer> map = calendar.getDisplayNames(field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                                                                        Calendar.ALL_STYLES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                                                                        locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                    if ((index = matchString(text, start, field, map)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            case 1: // 'y' - YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                if (!(calendar instanceof GregorianCalendar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                    // calendar might have text representations for year values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    // such as "\u5143" in JapaneseImperialCalendar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                    int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                        if ((index = matchString(text, start, field, map)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                    calendar.set(field, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                // If there are 3 or more YEAR pattern characters, this indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                // that the year value is to be treated literally, without any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                // two-digit year adjustments (e.g., from "01" to 2001).  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                // we made adjustments to place the 2-digit year in the proper
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                // century, for parsed strings from "00" to "99".  Any other string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                // is treated literally:  "2250", "-1", "1", "002".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                if (count <= 2 && (pos.index - start) == 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                    && Character.isDigit(text.charAt(start))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                    && Character.isDigit(text.charAt(start+1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    // Assume for example that the defaultCenturyStart is 6/18/1903.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    // This means that two-digit years will be forced into the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                    // 6/18/1903 to 6/17/2003.  As a result, years 00, 01, and 02
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                    // correspond to 2000, 2001, and 2002.  Years 04, 05, etc. correspond
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    // to 1904, 1905, etc.  If the year is 03, then it is 2003 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                    // other fields specify a date before 6/18, or 1903 if they specify a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                    // date afterwards.  As a result, 03 is an ambiguous year.  All other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                    // two-digit years are unambiguous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                    ambiguousYear[0] = value == ambiguousTwoDigitYear;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                    value += (defaultCenturyStartYear/100)*100 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                        (value < ambiguousTwoDigitYear ? 100 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                calendar.set(Calendar.YEAR, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            case 2: // 'M' - MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                if (count <= 2) // i.e., M or MM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    // Don't want to parse the month if it is a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                    // while pattern uses numeric style: M or MM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    // [We computed 'value' above.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    calendar.set(Calendar.MONTH, value - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                    // count >= 3 // i.e., MMM or MMMM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    // Want to be able to parse both short and long forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    // Try count == 4 first:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                    int newStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    if ((newStart = matchString(text, start, Calendar.MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                                                formatData.getMonths())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                        return newStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    // count == 4 failed, now try count == 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    if ((index = matchString(text, start, Calendar.MONTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                                             formatData.getShortMonths())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    Map<String, Integer> map = calendar.getDisplayNames(field,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                                                                        Calendar.ALL_STYLES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                                                                        locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    if ((index = matchString(text, start, field, map)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            case 4: // 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                // [We computed 'value' above.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY)+1) value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                calendar.set(Calendar.HOUR_OF_DAY, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            case 9:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                { // 'E' - DAY_OF_WEEK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                    if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                        // Want to be able to parse both short and long forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                        // Try count == 4 (DDDD) first:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                        int newStart = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                        if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                                                  formatData.getWeekdays())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                            return newStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                        // DDDD failed, now try DDD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                        if ((index = matchString(text, start, Calendar.DAY_OF_WEEK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                                                 formatData.getShortWeekdays())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                        int[] styles = { Calendar.LONG, Calendar.SHORT };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                        for (int style : styles) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                            Map<String,Integer> map = calendar.getDisplayNames(field, style, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                            if ((index = matchString(text, start, field, map)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                                return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            case 14:    // 'a' - AM_PM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    if ((index = matchString(text, start, Calendar.AM_PM, formatData.getAmPmStrings())) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                    Map<String,Integer> map = calendar.getDisplayNames(field, Calendar.ALL_STYLES, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                    if ((index = matchString(text, start, field, map)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            case 15: // 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                // [We computed 'value' above.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                if (value == calendar.getLeastMaximum(Calendar.HOUR)+1) value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                calendar.set(Calendar.HOUR, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            case 17: // 'z' - ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            case 18: // 'Z' - ZONE_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                // First try to parse generic forms such as GMT-07:00. Do this first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                // in case localized TimeZoneNames contains the string "GMT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                // for a zone; in that case, we don't want to match the first three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                // characters of GMT+/-hh:mm etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                    int sign = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                    // For time zones that have no known names, look for strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                    // of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                    //    GMT[+-]hours:minutes or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                    //    GMT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                    if ((text.length() - start) >= GMT.length() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                        text.regionMatches(true, start, GMT, 0, GMT.length())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                        int num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                        calendar.set(Calendar.DST_OFFSET, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                        pos.index = start + GMT.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                        try { // try-catch for "GMT" only time zone string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                            char c = text.charAt(pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                            if (c == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                                sign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                            } else if (c == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                                sign = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                        catch(StringIndexOutOfBoundsException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                        if (sign == 0) {        /* "GMT" without offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                            calendar.set(Calendar.ZONE_OFFSET, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                            return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                        // Look for hours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                            char c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
                            num = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                            if (text.charAt(++pos.index) != ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                                c = text.charAt(pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                                if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                                    break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                                num *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                                num += c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                                pos.index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                            if (num > 23) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                                --pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                            if  (text.charAt(pos.index) != ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                            // Look for minutes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
                            offset = num * 60;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                            num = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                            num *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                            num += c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                            if (num > 59) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
                        } catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                            break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                        offset += num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
                        // Fall through for final processing below of 'offset' and 'sign'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                        // If the first character is a sign, look for numeric timezones of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                        // the form [+-]hhmm as specified by RFC 822. Otherwise, check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
                        // for named time zones by looking through the locale data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
                        // the TimeZoneNames strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                            char c = text.charAt(pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                            if (c == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                                sign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                            } else if (c == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                                sign = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
                                // Try parsing the text as a time zone name (abbr).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                                int i = subParseZoneString(text, pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                                if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
                            // Parse the text as an RFC 822 time zone string. This code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                            // actually a little more permissive than RFC 822.  It will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                            // try to do its best with numbers that aren't strictly 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                            // digits long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                            // Look for hh.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                            int hours = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
                            hours = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                            hours *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
                            hours += c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                            if (hours > 23) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                            // Look for mm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
                            int minutes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
                            minutes = c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                            c = text.charAt(++pos.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                            minutes *= 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                            minutes += c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                            if (minutes > 59) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                            offset = hours * 60 + minutes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                        } catch (StringIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                            break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                    // Do the final processing for both of the above cases.  We only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
                    // arrive here if the form GMT+/-... or an RFC 822 form was seen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                    if (sign != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                        offset *= MILLIS_PER_MINUTE * sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                        calendar.set(Calendar.ZONE_OFFSET, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                        calendar.set(Calendar.DST_OFFSET, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
                        return ++pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
                // case 3: // 'd' - DATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                // case 5: // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                // case 6: // 'm' - MINUTE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                // case 7: // 's' - SECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                // case 8: // 'S' - MILLISECOND
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                // case 10: // 'D' - DAY_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                // case 12: // 'w' - WEEK_OF_YEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                // case 13: // 'W' - WEEK_OF_MONTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                // case 16: // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
                // Handle "generic" fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                if (obeyCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                    if ((start+count) > text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                        break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                    number = numberFormat.parse(text.substring(0, start+count), pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                    number = numberFormat.parse(text, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                if (number != null) {
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1965
                    value = number.intValue();
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1966
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1967
                    if (useFollowingMinusSignAsDelimiter && (value < 0) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1968
                        (((pos.index < text.length()) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1969
                         (text.charAt(pos.index) != minusSign)) ||
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1970
                         ((pos.index == text.length()) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1971
                          (text.charAt(pos.index-1) == minusSign)))) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1972
                        value = -value;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1973
                        pos.index--;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1974
                    }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1975
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  1976
                    calendar.set(field, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                    return pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                break parsing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        // Parsing failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        origPos.errorIndex = pos.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    private final String getCalendarName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        return calendar.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    private boolean useDateFormatSymbols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        if (useDateFormatSymbols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        return isGregorianCalendar() || locale == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    private boolean isGregorianCalendar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        return "java.util.GregorianCalendar".equals(getCalendarName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * Translates a pattern, mapping each character in the from string to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * corresponding character in the to string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    private String translatePattern(String pattern, String from, String to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        boolean inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        for (int i = 0; i < pattern.length(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            char c = pattern.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                if (c == '\'')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                if (c == '\'')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                    inQuote = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
                    int ci = from.indexOf(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    if (ci == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                        throw new IllegalArgumentException("Illegal pattern " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                                                           " character '" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                                                           c + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    c = to.charAt(ci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            result.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        if (inQuote)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            throw new IllegalArgumentException("Unfinished quote in pattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * Returns a pattern string describing this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * @return a pattern string describing this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    public String toPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * Returns a localized pattern string describing this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * @return a localized pattern string describing this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    public String toLocalizedPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        return translatePattern(pattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                                DateFormatSymbols.patternChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                                formatData.getLocalPatternChars());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * Applies the given pattern string to this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * @param pattern the new date and time pattern for this date format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * @exception NullPointerException if the given pattern is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    public void applyPattern (String pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        compiledPattern = compile(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        this.pattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * Applies the given localized pattern string to this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * @param pattern a String to be mapped to the new date and time format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *        pattern for this format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * @exception NullPointerException if the given pattern is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * @exception IllegalArgumentException if the given pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    public void applyLocalizedPattern(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
         String p = translatePattern(pattern,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                                     formatData.getLocalPatternChars(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                                     DateFormatSymbols.patternChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
         compiledPattern = compile(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
         this.pattern = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * Gets a copy of the date and time format symbols of this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * @return the date and time format symbols of this date format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * @see #setDateFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    public DateFormatSymbols getDateFormatSymbols()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        return (DateFormatSymbols)formatData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * Sets the date and time format symbols of this date format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * @param newFormatSymbols the new date and time format symbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * @exception NullPointerException if the given newFormatSymbols is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @see #getDateFormatSymbols
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        useDateFormatSymbols = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * Creates a copy of this <code>SimpleDateFormat</code>. This also
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * clones the format's date format symbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * @return a clone of this <code>SimpleDateFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        SimpleDateFormat other = (SimpleDateFormat) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        other.formatData = (DateFormatSymbols) formatData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * Returns the hash code value for this <code>SimpleDateFormat</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * @return the hash code value for this <code>SimpleDateFormat</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    public int hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
        return pattern.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        // just enough fields for a reasonable distribution
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * Compares the given object with this <code>SimpleDateFormat</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * @return true if the given object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * <code>SimpleDateFormat</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
    public boolean equals(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        if (!super.equals(obj)) return false; // super does class check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
        SimpleDateFormat that = (SimpleDateFormat) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        return (pattern.equals(that.pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                && formatData.equals(that.formatData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * After reading an object from the input stream, the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * pattern in the object is verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
     * @exception InvalidObjectException if the pattern is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    private void readObject(ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                         throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        stream.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            compiledPattern = compile(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            throw new InvalidObjectException("invalid pattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        if (serialVersionOnStream < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            // didn't have defaultCenturyStart field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            initializeDefaultCentury();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            // fill in dependent transient field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            parseAmbiguousDatesAsAfter(defaultCenturyStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        serialVersionOnStream = currentSerialVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        // If the deserialized object has a SimpleTimeZone, try
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        // to replace it with a ZoneInfo equivalent in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        // be compatible with the SimpleTimeZone-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        // implementation as much as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        TimeZone tz = getTimeZone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        if (tz instanceof SimpleTimeZone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
            String id = tz.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            TimeZone zi = TimeZone.getTimeZone(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                setTimeZone(zi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    }
1313
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2187
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2188
    /**
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2189
     * Analyze the negative subpattern of DecimalFormat and set/update values
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2190
     * as necessary.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2191
     */
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2192
    private void checkNegativeNumberExpression() {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2193
        if ((numberFormat instanceof DecimalFormat) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2194
            !numberFormat.equals(originalNumberFormat)) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2195
            String numberPattern = ((DecimalFormat)numberFormat).toPattern();
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2196
            if (!numberPattern.equals(originalNumberPattern)) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2197
                hasFollowingMinusSign = false;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2198
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2199
                int separatorIndex = numberPattern.indexOf(';');
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2200
                // If the negative subpattern is not absent, we have to analayze
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2201
                // it in order to check if it has a following minus sign.
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2202
                if (separatorIndex > -1) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2203
                    int minusIndex = numberPattern.indexOf('-', separatorIndex);
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2204
                    if ((minusIndex > numberPattern.lastIndexOf('0')) &&
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2205
                        (minusIndex > numberPattern.lastIndexOf('#'))) {
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2206
                        hasFollowingMinusSign = true;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2207
                        minusSign = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getMinusSign();
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2208
                    }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2209
                }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2210
                originalNumberPattern = numberPattern;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2211
            }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2212
            originalNumberFormat = numberFormat;
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2213
        }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2214
    }
f9151e9e2f50 4823811: [Fmt-Da] SimpleDateFormat patterns don't allow embedding of some literal punctuation
peytoia
parents: 1312
diff changeset
  2215
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
}