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