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