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