API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.text. SimpleDateFormat View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127

/*
 * @(#)SimpleDateFormat.java	1.86 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
 * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
 *
 *   The original version of this source code and documentation is copyrighted
 * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
 * materials are provided under terms of a License Agreement between Taligent
 * and Sun. This technology is protected by multiple US and International
 * patents. This notice and attribution to Taligent may not be removed.
 *   Taligent is a registered trademark of Taligent, Inc.
 *
 */

package java.text;

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import sun.util.calendar.CalendarUtils;
import sun.util.calendar.ZoneInfoFile;
import sun.util.resources.LocaleData;

/**
 * <code>SimpleDateFormat</code> is a concrete class for formatting and
 * parsing dates in a locale-sensitive manner. It allows for formatting
 * (date -> text), parsing (text -> date), and normalization.
 *
 * <p>
 * <code>SimpleDateFormat</code> allows you to start by choosing
 * any user-defined patterns for date-time formatting. However, you
 * are encouraged to create a date-time formatter with either
 * <code>getTimeInstance</code>, <code>getDateInstance</code>, or
 * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each
 * of these class methods can return a date/time formatter initialized
 * with a default format pattern. You may modify the format pattern
 * using the <code>applyPattern</code> methods as desired.
 * For more information on using these methods, see
 * {@link DateFormat}.
 *
 * <h4>Date and Time Patterns</h4>
 * <p>
 * Date and time formats are specified by <em>date and time pattern</em>
 * strings.
 * Within date and time pattern strings, unquoted letters from
 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
 * <code>'z'</code> are interpreted as pattern letters representing the
 * components of a date or time string.
 * Text can be quoted using single quotes (<code>'</code>) to avoid
 * interpretation.
 * <code>"''"</code> represents a single quote.
 * All other characters are not interpreted; they're simply copied into the
 * output string during formatting or matched against the input string
 * during parsing.
 * <p>
 * The following pattern letters are defined (all other characters from
 * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
 * <code>'z'</code> are reserved):
 * <blockquote>
 * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
 *     <tr bgcolor="#ccccff">
 *         <th align=left>Letter
 *         <th align=left>Date or Time Component
 *         <th align=left>Presentation
 *         <th align=left>Examples
 *     <tr>
 *         <td><code>G</code>
 *         <td>Era designator
 *         <td><a href="#text">Text</a>
 *         <td><code>AD</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>y</code>
 *         <td>Year
 *         <td><a href="#year">Year</a>
 *         <td><code>1996</code>; <code>96</code>
 *     <tr>
 *         <td><code>M</code>
 *         <td>Month in year
 *         <td><a href="#month">Month</a>
 *         <td><code>July</code>; <code>Jul</code>; <code>07</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>w</code>
 *         <td>Week in year
 *         <td><a href="#number">Number</a>
 *         <td><code>27</code>
 *     <tr>
 *         <td><code>W</code>
 *         <td>Week in month
 *         <td><a href="#number">Number</a>
 *         <td><code>2</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>D</code>
 *         <td>Day in year
 *         <td><a href="#number">Number</a>
 *         <td><code>189</code>
 *     <tr>
 *         <td><code>d</code>
 *         <td>Day in month
 *         <td><a href="#number">Number</a>
 *         <td><code>10</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>F</code>
 *         <td>Day of week in month
 *         <td><a href="#number">Number</a>
 *         <td><code>2</code>
 *     <tr>
 *         <td><code>E</code>
 *         <td>Day in week
 *         <td><a href="#text">Text</a>
 *         <td><code>Tuesday</code>; <code>Tue</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>a</code>
 *         <td>Am/pm marker
 *         <td><a href="#text">Text</a>
 *         <td><code>PM</code>
 *     <tr>
 *         <td><code>H</code>
 *         <td>Hour in day (0-23)
 *         <td><a href="#number">Number</a>
 *         <td><code>0</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>k</code>
 *         <td>Hour in day (1-24)
 *         <td><a href="#number">Number</a>
 *         <td><code>24</code>
 *     <tr>
 *         <td><code>K</code>
 *         <td>Hour in am/pm (0-11)
 *         <td><a href="#number">Number</a>
 *         <td><code>0</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>h</code>
 *         <td>Hour in am/pm (1-12)
 *         <td><a href="#number">Number</a>
 *         <td><code>12</code>
 *     <tr>
 *         <td><code>m</code>
 *         <td>Minute in hour
 *         <td><a href="#number">Number</a>
 *         <td><code>30</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>s</code>
 *         <td>Second in minute
 *         <td><a href="#number">Number</a>
 *         <td><code>55</code>
 *     <tr>
 *         <td><code>S</code>
 *         <td>Millisecond
 *         <td><a href="#number">Number</a>
 *         <td><code>978</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>z</code>
 *         <td>Time zone
 *         <td><a href="#timezone">General time zone</a>
 *         <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
 *     <tr>
 *         <td><code>Z</code>
 *         <td>Time zone
 *         <td><a href="#rfc822timezone">RFC 822 time zone</a>
 *         <td><code>-0800</code>
 * </table>
 * </blockquote>
 * Pattern letters are usually repeated, as their number determines the
 * exact presentation:
 * <ul>
 * <li><strong><a name="text">Text:</a></strong>
 *     For formatting, if the number of pattern letters is 4 or more,
 *     the full form is used; otherwise a short or abbreviated form
 *     is used if available.
 *     For parsing, both forms are accepted, independent of the number
 *     of pattern letters.
 * <li><strong><a name="number">Number:</a></strong>
 *     For formatting, the number of pattern letters is the minimum
 *     number of digits, and shorter numbers are zero-padded to this amount.
 *     For parsing, the number of pattern letters is ignored unless
 *     it's needed to separate two adjacent fields.
 * <li><strong><a name="year">Year:</a></strong>
 *     If the formatter's {@link #getCalendar() Calendar} is the Gregorian
 *     calendar, the following rules are applied.<br>
 *     <ul>
 *     <li>For formatting, if the number of pattern letters is 2, the year
 *         is truncated to 2 digits; otherwise it is interpreted as a
 *         <a href="#number">number</a>.
 *     <li>For parsing, if the number of pattern letters is more than 2,
 *         the year is interpreted literally, regardless of the number of
 *         digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to
 *         Jan 11, 12 A.D.
 *     <li>For parsing with the abbreviated year pattern ("y" or "yy"),
 *         <code>SimpleDateFormat</code> must interpret the abbreviated year
 *         relative to some century.  It does this by adjusting dates to be
 *         within 80 years before and 20 years after the time the <code>SimpleDateFormat</code>
 *         instance is created. For example, using a pattern of "MM/dd/yy" and a
 *         <code>SimpleDateFormat</code> instance created on Jan 1, 1997,  the string
 *         "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
 *         would be interpreted as May 4, 1964.
 *         During parsing, only strings consisting of exactly two digits, as defined by
 *         {@link Character#isDigit(char)}, will be parsed into the default century.
 *         Any other numeric string, such as a one digit string, a three or more digit
 *         string, or a two digit string that isn't all digits (for example, "-1"), is
 *         interpreted literally.  So "01/02/3" or "01/02/003" are parsed, using the
 *         same pattern, as Jan 2, 3 AD.  Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
 *     </ul>
 *     Otherwise, calendar system specific forms are applied.
 *     For both formatting and parsing, if the number of pattern
 *     letters is 4 or more, a calendar specific {@linkplain
 *     Calendar#LONG long form} is used. Otherwise, a calendar
 *     specific {@linkplain Calendar#SHORT short or abbreviated form}
 *     is used.
 * <li><strong><a name="month">Month:</a></strong>
 *     If the number of pattern letters is 3 or more, the month is
 *     interpreted as <a href="#text">text</a>; otherwise,
 *     it is interpreted as a <a href="#number">number</a>.
 * <li><strong><a name="timezone">General time zone:</a></strong>
 *     Time zones are interpreted as <a href="#text">text</a> if they have
 *     names. For time zones representing a GMT offset value, the
 *     following syntax is used:
 *     <pre>
 *     <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a>
 *             <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
 *     <i>Sign:</i> one of
 *             <code>+ -</code>
 *     <i>Hours:</i>
 *             <i>Digit</i>
 *             <i>Digit</i> <i>Digit</i>
 *     <i>Minutes:</i>
 *             <i>Digit</i> <i>Digit</i>
 *     <i>Digit:</i> one of
 *             <code>0 1 2 3 4 5 6 7 8 9</code></pre>
 *     <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between
 *     00 and 59. The format is locale independent and digits must be taken
 *     from the Basic Latin block of the Unicode standard.
 *     <p>For parsing, <a href="#rfc822timezone">RFC 822 time zones</a> are also
 *     accepted.
 * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong>
 *     For formatting, the RFC 822 4-digit time zone format is used:
 *     <pre>
 *     <i>RFC822TimeZone:</i>
 *             <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
 *     <i>TwoDigitHours:</i>
 *             <i>Digit Digit</i></pre>
 *     <i>TwoDigitHours</i> must be between 00 and 23. Other definitions
 *     are as for <a href="#timezone">general time zones</a>.
 *     <p>For parsing, <a href="#timezone">general time zones</a> are also
 *     accepted.
 * </ul>
 * <code>SimpleDateFormat</code> also supports <em>localized date and time
 * pattern</em> strings. In these strings, the pattern letters described above
 * may be replaced with other, locale dependent, pattern letters.
 * <code>SimpleDateFormat</code> does not deal with the localization of text
 * other than the pattern letters; that's up to the client of the class.
 * <p>
 *
 * <h4>Examples</h4>
 *
 * The following examples show how date and time patterns are interpreted in
 * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
 * in the U.S. Pacific Time time zone.
 * <blockquote>
 * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale">
 *     <tr bgcolor="#ccccff">
 *         <th align=left>Date and Time Pattern
 *         <th align=left>Result
 *     <tr>
 *         <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
 *         <td><code>2001.07.04 AD at 12:08:56 PDT</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>"EEE, MMM d, ''yy"</code>
 *         <td><code>Wed, Jul 4, '01</code>
 *     <tr>
 *         <td><code>"h:mm a"</code>
 *         <td><code>12:08 PM</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>"hh 'o''clock' a, zzzz"</code>
 *         <td><code>12 o'clock PM, Pacific Daylight Time</code>
 *     <tr>
 *         <td><code>"K:mm a, z"</code>
 *         <td><code>0:08 PM, PDT</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
 *         <td><code>02001.July.04 AD 12:08 PM</code>
 *     <tr>
 *         <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
 *         <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
 *     <tr bgcolor="#eeeeff">
 *         <td><code>"yyMMddHHmmssZ"</code>
 *         <td><code>010704120856-0700</code>
 *     <tr>
 *         <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
 *         <td><code>2001-07-04T12:08:56.235-0700</code>
 * </table>
 * </blockquote>
 *
 * <h4><a name="synchronization">Synchronization</a></h4>
 *
 * <p>
 * Date formats are not synchronized.
 * It is recommended to create separate format instances for each thread.
 * If multiple threads access a format concurrently, it must be synchronized
 * externally.
 *
 * @see          <a href="http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
 * @see          java.util.Calendar
 * @see          java.util.TimeZone
 * @see          DateFormat
 * @see          DateFormatSymbols
 * @version      1.86, 11/17/05
 * @author       Mark Davis, Chen-Lieh Huang, Alan Liu
 */
public class SimpleDateFormat extends DateFormat {

    // the official serial version ID which says cryptically
    // which version we're compatible with
    static final long serialVersionUID = 4774881970558875024L;

    // the internal serial version which says which version was written
    // - 0 (default) for version up to JDK 1.1.3
    // - 1 for version from JDK 1.1.4, which includes a new field
    static final int currentSerialVersion = 1;

    /**
     * The version of the serialized data on the stream.  Possible values:
     * <ul>
     * <li><b>0</b> or not present on stream: JDK 1.1.3.  This version
     * has no <code>defaultCenturyStart</code> on stream.
     * <li><b>1</b> JDK 1.1.4 or later.  This version adds
     * <code>defaultCenturyStart</code>.
     * </ul>
     * When streaming out this class, the most recent format
     * and the highest allowable <code>serialVersionOnStream</code>
     * is written.
     * @serial
     * @since JDK1.1.4
     */
    private int serialVersionOnStream = currentSerialVersion;

    /**
     * The pattern string of this formatter.  This is always a non-localized
     * pattern.  May not be null.  See class documentation for details.
     * @serial
     */
    private String pattern;

    /**
     * The compiled pattern.
     */
    transient private char[] compiledPattern;

    /**
     * Tags for the compiled pattern.
     */
    private final static int TAG_QUOTE_ASCII_CHAR	= 100;
    private final static int TAG_QUOTE_CHARS		= 101;

    /**
     * Locale dependent digit zero.
     * @see #zeroPaddingNumber
     * @see java.text.DecimalFormatSymbols#getZeroDigit
     */
    transient private char zeroDigit;

    /**
     * The symbols used by this formatter for week names, month names,
     * etc.  May not be null.
     * @serial
     * @see java.text.DateFormatSymbols
     */
    private DateFormatSymbols formatData;

    /**
     * We map dates with two-digit years into the century starting at
     * <code>defaultCenturyStart</code>, which may be any date.  May
     * not be null.
     * @serial
     * @since JDK1.1.4
     */
    private Date defaultCenturyStart;

    transient private int defaultCenturyStartYear;

    private static final int millisPerHour = 60 * 60 * 1000;
    private static final int millisPerMinute = 60 * 1000;

    // For time zones that have no names, use strings GMT+minutes and
    // GMT-minutes. For instance, in France the time zone is GMT+60.
    private static final String GMT = "GMT";

    /**
     * Cache to hold the DateTimePatterns of a Locale.
     */
    private static Hashtable<String,String[]> cachedLocaleData
	= new Hashtable<String,String[]>(3);

    /**
     * Cache NumberFormat instances with Locale key.
     */
    private static Hashtable<Locale,NumberFormat> cachedNumberFormatData
	= new Hashtable<Locale,NumberFormat>(3);

    /**
     * The Locale used to instantiate this
     * <code>SimpleDateFormat</code>. The value may be null if this object
     * has been created by an older <code>SimpleDateFormat</code> and
     * deserialized.
     *
     * @serial
     * @since 1.6
     */
    private Locale locale;

    /**
     * Indicates whether this <code>SimpleDateFormat</code> should use
     * the DateFormatSymbols. If true, the format and parse methods
     * use the DateFormatSymbols values. If false, the format and
     * parse methods call Calendar.getDisplayName or
     * Calendar.getDisplayNames.
     */
    transient boolean useDateFormatSymbols;

    /**
     * Constructs a <code>SimpleDateFormat</code> using the default pattern and
     * date format symbols for the default locale.
     * <b>Note:</b> This constructor may not support all locales.
     * For full coverage, use the factory methods in the {@link DateFormat}
     * class.
     */
    public SimpleDateFormat() {
        this(SHORT, SHORT, Locale.getDefault());
    }

    /**
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
     * the default date format symbols for the default locale.
     * <b>Note:</b> This constructor may not support all locales.
     * For full coverage, use the factory methods in the {@link DateFormat}
     * class.
     *
     * @param pattern the pattern describing the date and time format
     * @exception NullPointerException if the given pattern is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public SimpleDateFormat(String pattern)
    {
        this(pattern, Locale.getDefault());
    }

    /**
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
     * the default date format symbols for the given locale.
     * <b>Note:</b> This constructor may not support all locales.
     * For full coverage, use the factory methods in the {@link DateFormat}
     * class.
     *
     * @param pattern the pattern describing the date and time format
     * @param locale the locale whose date format symbols should be used
     * @exception NullPointerException if the given pattern or locale is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public SimpleDateFormat(String pattern, Locale locale)
    {
	if (pattern == null || locale == null) {
	    throw new NullPointerException();
	}

	initializeCalendar(locale);
        this.pattern = pattern;
        this.formatData = DateFormatSymbols.getInstance(locale);
	this.locale = locale;
        initialize(locale);
    }

    /**
     * Constructs a <code>SimpleDateFormat</code> using the given pattern and
     * date format symbols.
     *
     * @param pattern the pattern describing the date and time format
     * @param formatSymbols the date format symbols to be used for formatting
     * @exception NullPointerException if the given pattern or formatSymbols is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public SimpleDateFormat(String pattern, DateFormatSymbols formatSymbols)
    {
	if (pattern == null || formatSymbols == null) {
	    throw new NullPointerException();
	}

        this.pattern = pattern;
        this.formatData = (DateFormatSymbols) formatSymbols.clone();
	this.locale = Locale.getDefault();
	initializeCalendar(this.locale);
        initialize(this.locale);
	useDateFormatSymbols = true;
    }

    /* Package-private, called by DateFormat factory methods */
    SimpleDateFormat(int timeStyle, int dateStyle, Locale loc) {
	if (loc == null) {
	    throw new NullPointerException();
	}

	this.locale = loc;
	// initialize calendar and related fields
	initializeCalendar(loc);

        /* try the cache first */
	String key = getKey();
        String[] dateTimePatterns = cachedLocaleData.get(key);
        if (dateTimePatterns == null) { /* cache miss */
            ResourceBundle r = LocaleData.getDateFormatData(loc);
	    if (!isGregorianCalendar()) {
		try {
		    dateTimePatterns = r.getStringArray(getCalendarName() + ".DateTimePatterns");
		} catch (MissingResourceException e) {
		}
	    }
	    if (dateTimePatterns == null) {
		dateTimePatterns = r.getStringArray("DateTimePatterns");
	    }
            /* update cache */
            cachedLocaleData.put(key, dateTimePatterns);
        }
	formatData = DateFormatSymbols.getInstance(loc);
	if ((timeStyle >= 0) && (dateStyle >= 0)) {
	    Object[] dateTimeArgs = {dateTimePatterns[timeStyle],
				     dateTimePatterns[dateStyle + 4]};
	    pattern = MessageFormat.format(dateTimePatterns[8], dateTimeArgs);
	}
	else if (timeStyle >= 0) {
	    pattern = dateTimePatterns[timeStyle];
	}
	else if (dateStyle >= 0) {
            pattern = dateTimePatterns[dateStyle + 4];
	}
	else {
	    throw new IllegalArgumentException("No date or time style specified");
	}

	initialize(loc);
    }

    /* Initialize compiledPattern and numberFormat fields */
    private void initialize(Locale loc) {
	// Verify and compile the given pattern.
	compiledPattern = compile(pattern);

        /* try the cache first */
        numberFormat = cachedNumberFormatData.get(loc);
        if (numberFormat == null) { /* cache miss */
            numberFormat = NumberFormat.getIntegerInstance(loc);
            numberFormat.setGroupingUsed(false);

            /* update cache */
            cachedNumberFormatData.put(loc, numberFormat);
        }
        numberFormat = (NumberFormat) numberFormat.clone();

        initializeDefaultCentury();
    }

    private void initializeCalendar(Locale loc) {
	if (calendar == null) {
	    assert loc != null;
	    // The format object must be constructed using the symbols for this zone.
	    // However, the calendar should use the current default TimeZone.
	    // If this is not contained in the locale zone strings, then the zone
	    // will be formatted using generic GMT+/-H:MM nomenclature.
	    calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
	}
    }

    private String getKey() {
	StringBuilder sb = new StringBuilder();
	sb.append(getCalendarName()).append('.');
	sb.append(locale.getLanguage()).append('_').append(locale.getCountry()).append('_').append(locale.getVariant());
	return sb.toString();
    }

    /**
     * Returns the compiled form of the given pattern. The syntax of
     * the compiled pattern is:
     * <blockquote>
     * CompiledPattern:
     *     EntryList
     * EntryList:
     *     Entry
     *     EntryList Entry
     * Entry:
     *     TagField
     *     TagField data
     * TagField:
     *     Tag Length
     *     TaggedData
     * Tag:
     *     pattern_char_index
     *     TAG_QUOTE_CHARS
     * Length:
     *     short_length
     *     long_length
     * TaggedData:
     *     TAG_QUOTE_ASCII_CHAR ascii_char
     * 
     * </blockquote>
     * 
     * where `short_length' is an 8-bit unsigned integer between 0 and
     * 254.  `long_length' is a sequence of an 8-bit integer 255 and a
     * 32-bit signed integer value which is split into upper and lower
     * 16-bit fields in two char's. `pattern_char_index' is an 8-bit
     * integer between 0 and 18. `ascii_char' is an 7-bit ASCII
     * character value. `data' depends on its Tag value.
     * <p>
     * If Length is short_length, Tag and short_length are packed in a
     * single char, as illustrated below.
     * <blockquote>
     *     char[0] = (Tag << 8) | short_length;
     * </blockquote>
     * 
     * If Length is long_length, Tag and 255 are packed in the first
     * char and a 32-bit integer, as illustrated below.
     * <blockquote>
     *     char[0] = (Tag << 8) | 255;
     *     char[1] = (char) (long_length >>> 16);
     *     char[2] = (char) (long_length & 0xffff);
     * </blockquote>
     * <p>
     * If Tag is a pattern_char_index, its Length is the number of
     * pattern characters. For example, if the given pattern is
     * "yyyy", Tag is 1 and Length is 4, followed by no data.
     * <p>
     * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's
     * following the TagField. For example, if the given pattern is
     * "'o''clock'", Length is 7 followed by a char sequence of
     * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>.
     * <p>
     * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
     * character in place of Length. For example, if the given pattern
     * is "'o'", the TaggedData entry is
     * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
     *
     * @exception NullPointerException if the given pattern is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    private char[] compile(String pattern) {
	int length = pattern.length();
	boolean inQuote = false;
	StringBuilder compiledPattern = new StringBuilder(length * 2);
	StringBuilder tmpBuffer = null;
	int count = 0;
	int lastTag = -1;

	for (int i = 0; i < length; i++) {
	    char c = pattern.charAt(i);

	    if (c == '\'') {
		// '' is treated as a single quote regardless of being
		// in a quoted section.
		if ((i + 1) < length) {
		    c = pattern.charAt(i + 1);
		    if (c == '\'') {
			i++;
			if (count != 0) {
			    encode(lastTag, count, compiledPattern);
			    lastTag = -1;
			    count = 0;
			}
			if (inQuote) {
			    tmpBuffer.append(c);
			} else {
			    compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
			}
			continue;
		    }
		}
		if (!inQuote) {
		    if (count != 0) {
			encode(lastTag, count, compiledPattern);
			lastTag = -1;
			count = 0;
		    }
		    if (tmpBuffer == null) {
			tmpBuffer = new StringBuilder(length);
		    } else {
			tmpBuffer.setLength(0);
		    }
		    inQuote = true;
		} else {
		    int len = tmpBuffer.length();
		    if (len == 1) {
			char ch = tmpBuffer.charAt(0);
			if (ch < 128) {
			    compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | ch));
			} else {
			    compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | 1));
			    compiledPattern.append(ch);
			}
		    } else {
			encode(TAG_QUOTE_CHARS, len, compiledPattern);
			compiledPattern.append(tmpBuffer);
		    }
		    inQuote = false;
		}
		continue;
	    }
	    if (inQuote) {
		tmpBuffer.append(c);
		continue;
	    }
	    if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
		if (count != 0) {
		    encode(lastTag, count, compiledPattern);
		    lastTag = -1;
		    count = 0;
		}
		if (c < 128) {
		    // In most cases, c would be a delimiter, such as ':'.
		    compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
		} else {
		    // Take any contiguous non-ASCII alphabet characters and
		    // put them in a single TAG_QUOTE_CHARS.
		    int j;
		    for (j = i + 1; j < length; j++) {
			char d = pattern.charAt(j);
			if (d == '\'' || (d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) {
			    break;
			}
		    }
		    compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | (j - i)));
		    for (; i < j; i++) {
			compiledPattern.append(pattern.charAt(i));
		    }
		    i--; 
		}
		continue;
	    }

	    int tag;
	    if ((tag = DateFormatSymbols.patternChars.indexOf(c)) == -1) {
		throw new IllegalArgumentException("Illegal pattern character " +
						   "'" + c + "'");
	    }
	    if (lastTag == -1 || lastTag == tag) {
		lastTag = tag;
		count++;
		continue;
	    }
	    encode(lastTag, count, compiledPattern);
	    lastTag = tag;
	    count = 1;
	}

	if (inQuote) {
	    throw new IllegalArgumentException("Unterminated quote");
	}

	if (count != 0) {
	    encode(lastTag, count, compiledPattern);
	}

	// Copy the compiled pattern to a char array
	int len = compiledPattern.length();
	char[] r = new char[len];
	compiledPattern.getChars(0, len, r, 0);
	return r;
    }

    /**
     * Encodes the given tag and length and puts encoded char(s) into buffer.
     */
    private static final void encode(int tag, int length, StringBuilder buffer) {
	if (length < 255) {
	    buffer.append((char)(tag << 8 | length));
	} else {
	    buffer.append((char)((tag << 8) | 0xff));
	    buffer.append((char)(length >>> 16));
	    buffer.append((char)(length & 0xffff));
	}
    }

    /* Initialize the fields we use to disambiguate ambiguous years. Separate
     * so we can call it from readObject().
     */
    private void initializeDefaultCentury() {
        calendar.setTime( new Date() );
        calendar.add( Calendar.YEAR, -80 );
        parseAmbiguousDatesAsAfter(calendar.getTime());
    }

    /* Define one-century window into which to disambiguate dates using
     * two-digit years.
     */
    private void parseAmbiguousDatesAsAfter(Date startDate) {
        defaultCenturyStart = startDate;
        calendar.setTime(startDate);
        defaultCenturyStartYear = calendar.get(Calendar.YEAR);
    }

    /**
     * Sets the 100-year period 2-digit years will be interpreted as being in
     * to begin on the date the user specifies.
     *
     * @param startDate During parsing, two digit years will be placed in the range
     * <code>startDate</code> to <code>startDate + 100 years</code>.
     * @see #get2DigitYearStart
     * @since 1.2
     */
    public void set2DigitYearStart(Date startDate) {
        parseAmbiguousDatesAsAfter(startDate);
    }

    /**
     * Returns the beginning date of the 100-year period 2-digit years are interpreted
     * as being within.
     *
     * @return the start of the 100-year period into which two digit years are
     * parsed
     * @see #set2DigitYearStart
     * @since 1.2
     */
    public Date get2DigitYearStart() {
        return defaultCenturyStart;
    }

    /**
     * Formats the given <code>Date</code> into a date/time string and appends
     * the result to the given <code>StringBuffer</code>.
     *
     * @param date the date-time value to be formatted into a date-time string.
     * @param toAppendTo where the new date-time text is to be appended.
     * @param pos the formatting position. On input: an alignment field,
     * if desired. On output: the offsets of the alignment field.
     * @return the formatted date-time string.
     * @exception NullPointerException if the given date is null
     */
    public StringBuffer format(Date date, StringBuffer toAppendTo,
                               FieldPosition pos)
    {
        pos.beginIndex = pos.endIndex = 0;
        return format(date, toAppendTo, pos.getFieldDelegate());
    }

    // Called from Format after creating a FieldDelegate
    private StringBuffer format(Date date, StringBuffer toAppendTo,
                                FieldDelegate delegate) {
        // Convert input date to time field list
        calendar.setTime(date);

	boolean useDateFormatSymbols = useDateFormatSymbols();

        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
	    int count = compiledPattern[i++] & 0xff;
	    if (count == 255) {
		count = compiledPattern[i++] << 16;
		count |= compiledPattern[i++];
	    }

	    switch (tag) {
	    case TAG_QUOTE_ASCII_CHAR:
		toAppendTo.append((char)count);
		break;

	    case TAG_QUOTE_CHARS:
		toAppendTo.append(compiledPattern, i, count);
		i += count;
		break;

	    default:
                subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
		break;
	    }
	}
        return toAppendTo;
    }

    /**
     * Formats an Object producing an <code>AttributedCharacterIterator</code>.
     * You can use the returned <code>AttributedCharacterIterator</code>
     * to build the resulting String, as well as to determine information
     * about the resulting String.
     * <p>
     * Each attribute key of the AttributedCharacterIterator will be of type
     * <code>DateFormat.Field</code>, with the corresponding attribute value
     * being the same as the attribute key.
     *
     * @exception NullPointerException if obj is null.
     * @exception IllegalArgumentException if the Format cannot format the
     *            given object, or if the Format's pattern string is invalid.
     * @param obj The object to format
     * @return AttributedCharacterIterator describing the formatted value.
     * @since 1.4
     */
    public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
        StringBuffer sb = new StringBuffer();
        CharacterIteratorFieldDelegate delegate = new
                         CharacterIteratorFieldDelegate();

        if (obj instanceof Date) {
            format((Date)obj, sb, delegate);
        }
        else if (obj instanceof Number) {
            format(new Date(((Number)obj).longValue()), sb, delegate);
        }
        else if (obj == null) {
            throw new NullPointerException(
                   "formatToCharacterIterator must be passed non-null object");
        }
        else {
            throw new IllegalArgumentException(
                             "Cannot format given Object as a Date");
        }
        return delegate.getIterator(sb.toString());
    }

    // Map index into pattern character string to Calendar field number
    private static final int[] PATTERN_INDEX_TO_CALENDAR_FIELD =
    {
        Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.DATE,
        Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.MINUTE,
        Calendar.SECOND, Calendar.MILLISECOND, Calendar.DAY_OF_WEEK,
        Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK_IN_MONTH,
        Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
        Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR, Calendar.ZONE_OFFSET,
        Calendar.ZONE_OFFSET
    };

    // Map index into pattern character string to DateFormat field number
    private static final int[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD = {
        DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, DateFormat.MONTH_FIELD,
        DateFormat.DATE_FIELD, DateFormat.HOUR_OF_DAY1_FIELD,
        DateFormat.HOUR_OF_DAY0_FIELD, DateFormat.MINUTE_FIELD,
        DateFormat.SECOND_FIELD, DateFormat.MILLISECOND_FIELD,
        DateFormat.DAY_OF_WEEK_FIELD, DateFormat.DAY_OF_YEAR_FIELD,
        DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD, DateFormat.WEEK_OF_YEAR_FIELD,
        DateFormat.WEEK_OF_MONTH_FIELD, DateFormat.AM_PM_FIELD,
        DateFormat.HOUR1_FIELD, DateFormat.HOUR0_FIELD,
        DateFormat.TIMEZONE_FIELD, DateFormat.TIMEZONE_FIELD,
    };

    // Maps from DecimalFormatSymbols index to Field constant
    private static final Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = {
        Field.ERA, Field.YEAR, Field.MONTH, Field.DAY_OF_MONTH,
        Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE,
        Field.SECOND, Field.MILLISECOND, Field.DAY_OF_WEEK,
        Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH,
        Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH,
        Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE,
        Field.TIME_ZONE,
    };

    /**
     * Private member function that does the real date/time formatting.
     */
    private void subFormat(int patternCharIndex, int count,
			   FieldDelegate delegate, StringBuffer buffer,
			   boolean useDateFormatSymbols)
    {
        int     maxIntCount = Integer.MAX_VALUE;
        String  current = null;
        int     beginOffset = buffer.length();

        int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
        int value = calendar.get(field);
	int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
	if (!useDateFormatSymbols) {
	    current = calendar.getDisplayName(field, style, locale);
	}

	// Note: zeroPaddingNumber() assumes that maxDigits is either
	// 2 or maxIntCount. If we make any changes to this,
	// zeroPaddingNumber() must be fixed.

        switch (patternCharIndex) {
        case 0: // 'G' - ERA
	    if (useDateFormatSymbols) {
		String[] eras = formatData.getEras();
		if (value < eras.length)
		    current = eras[value];
	    }
	    if (current == null)
		current = "";
            break;

        case 1: // 'y' - YEAR
	    if (calendar instanceof GregorianCalendar) {
		if (count >= 4)
		    zeroPaddingNumber(value, count, maxIntCount, buffer);
		else // count < 4
		    zeroPaddingNumber(value, 2, 2, buffer); // clip 1996 to 96
	    } else {
		if (current == null) {
		    zeroPaddingNumber(value, style == Calendar.LONG ? 1 : count,
				      maxIntCount, buffer);
		}
	    }
            break;

        case 2: // 'M' - MONTH
	    if (useDateFormatSymbols) {
		String[] months;
		if (count >= 4) {
		    months = formatData.getMonths();
		    current = months[value];
		} else if (count == 3) {
		    months = formatData.getShortMonths();
		    current = months[value];
		}
	    } else {
		if (count < 3) {
		    current = null;
		}
	    }
	    if (current == null) {
		zeroPaddingNumber(value+1, count, maxIntCount, buffer);
	    }
            break;

        case 4: // 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
	    if (current == null) {
		if (value == 0)
		    zeroPaddingNumber(calendar.getMaximum(Calendar.HOUR_OF_DAY)+1,
				      count, maxIntCount, buffer);
		else
		    zeroPaddingNumber(value, count, maxIntCount, buffer);
	    }
            break;

        case 9: // 'E' - DAY_OF_WEEK
	    if (useDateFormatSymbols) {
		String[] weekdays;
		if (count >= 4) {
		    weekdays = formatData.getWeekdays();
		    current = weekdays[value];
		} else { // count < 4, use abbreviated form if exists
		    weekdays = formatData.getShortWeekdays();
		    current = weekdays[value];
		}
	    }
            break;

        case 14:    // 'a' - AM_PM
	    if (useDateFormatSymbols) {
		String[] ampm = formatData.getAmPmStrings();
		current = ampm[value];
	    }
            break;

        case 15: // 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
	    if (current == null) {
		if (value == 0)
		    zeroPaddingNumber(calendar.getLeastMaximum(Calendar.HOUR)+1,
				      count, maxIntCount, buffer);
		else
		    zeroPaddingNumber(value, count, maxIntCount, buffer);
	    }
            break;

        case 17: // 'z' - ZONE_OFFSET
	    if (current == null) {
		if (formatData.locale == null || formatData.isZoneStringsSet) {
		    int zoneIndex =
			formatData.getZoneIndex(calendar.getTimeZone().getID());
		    if (zoneIndex == -1) {
			value = calendar.get(Calendar.ZONE_OFFSET) +
			    calendar.get(Calendar.DST_OFFSET);
			buffer.append(ZoneInfoFile.toCustomID(value));
		    } else {
			int index = (calendar.get(Calendar.DST_OFFSET) == 0) ? 1: 3;
			if (count < 4) {
			    // Use the short name
			    index++;
			}
			String[][] zoneStrings = formatData.getZoneStringsWrapper();
			buffer.append(zoneStrings[zoneIndex][index]);
		    }
		} else {
		    TimeZone tz = calendar.getTimeZone();
		    boolean daylight = (calendar.get(Calendar.DST_OFFSET) != 0);
		    int tzstyle = (count < 4 ? TimeZone.SHORT : TimeZone.LONG);
		    buffer.append(tz.getDisplayName(daylight, tzstyle, formatData.locale));
		}
	    }
            break;

        case 18: // 'Z' - ZONE_OFFSET ("-/+hhmm" form)
            value = (calendar.get(Calendar.ZONE_OFFSET) +
		     calendar.get(Calendar.DST_OFFSET)) / 60000;

	    int width = 4;
            if (value >= 0) {
		buffer.append('+');
	    } else {
                width++;
            }

            int num = (value / 60) * 100 + (value % 60);
	    CalendarUtils.sprintf0d(buffer, num, width);
            break;

        default:
            // case 3: // 'd' - DATE
            // case 5: // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
            // case 6: // 'm' - MINUTE
            // case 7: // 's' - SECOND
            // case 8: // 'S' - MILLISECOND
            // case 10: // 'D' - DAY_OF_YEAR
            // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
            // case 12: // 'w' - WEEK_OF_YEAR
            // case 13: // 'W' - WEEK_OF_MONTH
            // case 16: // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM
	    if (current == null) {
		zeroPaddingNumber(value, count, maxIntCount, buffer);
	    }
            break;
        } // switch (patternCharIndex)

	if (current != null) {
	    buffer.append(current);
	}

        int fieldID = PATTERN_INDEX_TO_DATE_FORMAT_FIELD[patternCharIndex];
        Field f = PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID[patternCharIndex];

        delegate.formatted(fieldID, f, f, beginOffset, buffer.length(), buffer);
    }

    /**
     * Formats a number with the specified minimum and maximum number of digits.
     */
    private final void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer buffer)
    {
	// Optimization for 1, 2 and 4 digit numbers. This should
	// cover most cases of formatting date/time related items.
	// Note: This optimization code assumes that maxDigits is
	// either 2 or Integer.MAX_VALUE (maxIntCount in format()).
	try {
	    if (zeroDigit == 0) {
		zeroDigit = ((DecimalFormat)numberFormat).getDecimalFormatSymbols().getZeroDigit();
	    }
	    if (value >= 0) {
		if (value < 100 && minDigits >= 1 && minDigits <= 2) {
		    if (value < 10) {
			if (minDigits == 2) {
			    buffer.append(zeroDigit);
			}
			buffer.append((char)(zeroDigit + value));
		    } else {
			buffer.append((char)(zeroDigit + value / 10));
			buffer.append((char)(zeroDigit + value % 10));
		    }
		    return;
		} else if (value >= 1000 && value < 10000) {
		    if (minDigits == 4) {
			buffer.append((char)(zeroDigit + value / 1000));
			value %= 1000;
			buffer.append((char)(zeroDigit + value / 100));
			value %= 100;
			buffer.append((char)(zeroDigit + value / 10));
			buffer.append((char)(zeroDigit + value % 10));
			return;
		    }
		    if (minDigits == 2 && maxDigits == 2) {
			zeroPaddingNumber(value % 100, 2, 2, buffer);
			return;
		    }
		}
	    }
	} catch (Exception e) {
	}

        numberFormat.setMinimumIntegerDigits(minDigits);
        numberFormat.setMaximumIntegerDigits(maxDigits);
	numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
    }


    /**
     * Parses text from a string to produce a <code>Date</code>.
     * <p>
     * The method attempts to parse text starting at the index given by
     * <code>pos</code>.
     * If parsing succeeds, then the index of <code>pos</code> is updated
     * to the index after the last character used (parsing does not necessarily
     * use all characters up to the end of the string), and the parsed
     * date is returned. The updated <code>pos</code> can be used to
     * indicate the starting point for the next call to this method.
     * If an error occurs, then the index of <code>pos</code> is not
     * changed, the error index of <code>pos</code> is set to the index of
     * the character where the error occurred, and null is returned.
     *
     * @param text  A <code>String</code>, part of which should be parsed.
     * @param pos   A <code>ParsePosition</code> object with index and error
     *              index information as described above.
     * @return A <code>Date</code> parsed from the string. In case of
     *         error, returns null.
     * @exception NullPointerException if <code>text</code> or <code>pos</code> is null.
     */
    public Date parse(String text, ParsePosition pos)
    {
        int start = pos.index;
        int oldStart = start;
	int textLength = text.length();

        calendar.clear(); // Clears all the time fields

        boolean[] ambiguousYear = {false};


        for (int i = 0; i < compiledPattern.length; ) {
            int tag = compiledPattern[i] >>> 8;
	    int count = compiledPattern[i++] & 0xff;
	    if (count == 255) {
		count = compiledPattern[i++] << 16;
		count |= compiledPattern[i++];
	    }

	    switch (tag) {
	    case TAG_QUOTE_ASCII_CHAR:
		if (start >= textLength || text.charAt(start) != (char)count) {
		    pos.index = oldStart;
		    pos.errorIndex = start;
		    return null;
		}
		start++;
		break;

	    case TAG_QUOTE_CHARS:
		while (count-- > 0) {
		    if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
			pos.index = oldStart;
			pos.errorIndex = start;
			return null;
		    }
		    start++;
		}
		break;

	    default:
		// Peek the next pattern to determine if we need to
		// obey the number of pattern letters for
		// parsing. It's required when parsing contiguous
		// digit text (e.g., "20010704") with a pattern which
		// has no delimiters between fields, like "yyyyMMdd".
		boolean obeyCount = false;
		if (i < compiledPattern.length) {
		    int nextTag = compiledPattern[i] >>> 8;
		    if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
			obeyCount = true;
		    }
		}
		start = subParse(text, start, tag, count, obeyCount,
				 ambiguousYear, pos);
		if (start < 0) {
		    pos.index = oldStart;
		    return null;
		}
	    }
	}

        // At this point the fields of Calendar have been set.  Calendar
        // will fill in default values for missing fields when the time
        // is computed.

        pos.index = start;

        // This part is a problem:  When we call parsedDate.after, we compute the time.
        // Take the date April 3 2004 at 2:30 am.  When this is first set up, the year
        // will be wrong if we're parsing a 2-digit year pattern.  It will be 1904.
        // April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day.  2:30 am
        // is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
        // on that day.  It is therefore parsed out to fields as 3:30 am.  Then we
        // add 100 years, and get April 3 2004 at 3:30 am.  Note that April 3 2004 is
        // a Saturday, so it can have a 2:30 am -- and it should. [LIU]
        /*
        Date parsedDate = calendar.getTime();
        if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
            calendar.add(Calendar.YEAR, 100);
            parsedDate = calendar.getTime();
        }
        */
        // Because of the above condition, save off the fields in case we need to readjust.
        // The procedure we use here is not particularly efficient, but there is no other
        // way to do this given the API restrictions present in Calendar.  We minimize
        // inefficiency by only performing this computation when it might apply, that is,
        // when the two-digit year is equal to the start year, and thus might fall at the
        // front or the back of the default century.  This only works because we adjust
        // the year correctly to start with in other cases -- see subParse().
        Date parsedDate;
        try {
            if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
            {
                // We need a copy of the fields, and we need to avoid triggering a call to
                // complete(), which will recalculate the fields.  Since we can't access
                // the fields[] array in Calendar, we clone the entire object.  This will
                // stop working if Calendar.clone() is ever rewritten to call complete().
                Calendar savedCalendar = (Calendar)calendar.clone();
                parsedDate = calendar.getTime();
                if (parsedDate.before(defaultCenturyStart))
                {
                    // We can't use add here because that does a complete() first.
                    savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
                    parsedDate = savedCalendar.getTime();
                }
            }
            else parsedDate = calendar.getTime();
        }
        // An IllegalArgumentException will be thrown by Calendar.getTime()
        // if any fields are out of range, e.g., MONTH == 17.
        catch (IllegalArgumentException e) {
            pos.errorIndex = start;
            pos.index = oldStart;
            return null;
        }

        return parsedDate;
    }

    /**
     * Private code-size reduction function used by subParse.
     * @param text the time text being parsed.
     * @param start where to start parsing.
     * @param field the date field being parsed.
     * @param data the string array to parsed.
     * @return the new start position if matching succeeded; a negative number
     * indicating matching failure, otherwise.
     */
    private int matchString(String text, int start, int field, String[] data)
    {
        int i = 0;
        int count = data.length;

        if (field == Calendar.DAY_OF_WEEK) i = 1;

        // There may be multiple strings in the data[] array which begin with
        // the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
        // We keep track of the longest match, and return that.  Note that this
        // unfortunately requires us to test all array elements.
        int bestMatchLength = 0, bestMatch = -1;
        for (; i<count; ++i)
        {
            int length = data[i].length();
            // Always compare if we have no match yet; otherwise only compare
            // against potentially better matches (longer strings).
            if (length > bestMatchLength &&
                text.regionMatches(true, start, data[i], 0, length))
            {
                bestMatch = i;
                bestMatchLength = length;
            }
        }
        if (bestMatch >= 0)
        {
            calendar.set(field, bestMatch);
            return start + bestMatchLength;
        }
        return -start;
    }

    /**
     * Performs the same thing as matchString(String, int, int,
     * String[]). This method takes a Map<String, Integer> instead of
     * String[].
     */
    private int matchString(String text, int start, int field, Map<String,Integer> data) {
	if (data != null) {
	    String bestMatch = null;

	    for (String name : data.keySet()) {
		int length = name.length();
		if (bestMatch == null || length > bestMatch.length()) {
		    if (text.regionMatches(true, start, name, 0, length)) {
			bestMatch = name;
		    }
		}
	    }

	    if (bestMatch != null) {
		calendar.set(field, data.get(bestMatch));
		return start + bestMatch.length();
	    }
	}
	return -start;
    }

    private int matchZoneString(String text, int start, int zoneIndex) {
	for (int j = 1; j <= 4; ++j) {
	    // Checking long and short zones [1 & 2],
	    // and long and short daylight [3 & 4].
            String[][] zoneStrings = formatData.getZoneStringsWrapper();
	    String zoneName = zoneStrings[zoneIndex][j];
	    if (text.regionMatches(true, start,
				   zoneName, 0, zoneName.length())) {
		return j;
	    }
	}
	return -1;
    }

    private boolean matchDSTString(String text, int start, int zoneIndex, int standardIndex) {
	int index = standardIndex + 2;
        String[][] zoneStrings = formatData.getZoneStringsWrapper();
	String zoneName  = zoneStrings[zoneIndex][index];
	if (text.regionMatches(true, start,
			       zoneName, 0, zoneName.length())) {
	    return true;
	}
	return false;
    }

    /**
     * find time zone 'text' matched zoneStrings and set to internal
     * calendar.
     */
    private int subParseZoneString(String text, int start) {
	boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
	TimeZone currentTimeZone = getTimeZone();

	// At this point, check for named time zones by looking through
	// the locale data from the TimeZoneNames strings.
	// Want to be able to parse both short and long forms.
	int zoneIndex = 
	    formatData.getZoneIndex (currentTimeZone.getID());
	TimeZone tz = null;
        String[][] zoneStrings = formatData.getZoneStringsWrapper();
	int j = 0, i = 0;
	if ((zoneIndex != -1) && ((j = matchZoneString(text, start, zoneIndex)) > 0)) {
	    if (j <= 2) {
		useSameName = matchDSTString(text, start, zoneIndex, j);
	    }
	    tz = TimeZone.getTimeZone(zoneStrings[zoneIndex][0]);
	    i = zoneIndex;
	}
	if (tz == null) {
	    zoneIndex = 
		formatData.getZoneIndex (TimeZone.getDefault().getID());
	    if ((zoneIndex != -1) && ((j = matchZoneString(text, start, zoneIndex)) > 0)) {
		if (j <= 2) {
		    useSameName = matchDSTString(text, start, zoneIndex, j);
		}
		tz = TimeZone.getTimeZone(zoneStrings[zoneIndex][0]);
		i = zoneIndex;
	    }
	}	    

	if (tz == null) {
	    for (i = 0; i < zoneStrings.length; i++) {
		if ((j = matchZoneString(text, start, i)) > 0) {
		    if (j <= 2) {
			useSameName = matchDSTString(text, start, i, j);
		    }
		    tz = TimeZone.getTimeZone(zoneStrings[i][0]);
		    break;
		}
	    }
	}
	if (tz != null) { // Matched any ?
	    if (!tz.equals(currentTimeZone)) {
		setTimeZone(tz);
	    }
	    // If the time zone matched uses the same name
	    // (abbreviation) for both standard and daylight time,
	    // let the time zone in the Calendar decide which one.
	    if (!useSameName) {
		calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
		calendar.set(Calendar.DST_OFFSET, 
			     j >= 3 ? tz.getDSTSavings() : 0);
	    }
	    return (start + zoneStrings[i][j].length());
	}
	return 0;
    }

    /**
     * Private member function that converts the parsed date strings into
     * timeFields. Returns -start (for ParsePosition) if failed.
     * @param text the time text to be parsed.
     * @param start where to start parsing.
     * @param ch the pattern character for the date field text to be parsed.
     * @param count the count of a pattern character.
     * @param obeyCount if true, then the next field directly abuts this one,
     * and we should use the count to know when to stop parsing.
     * @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
     * is true, then a two-digit year was parsed and may need to be readjusted.
     * @param origPos origPos.errorIndex is used to return an error index
     * at which a parse error occurred, if matching failure occurs.
     * @return the new start position if matching succeeded; -1 indicating
     * matching failure, otherwise. In case matching failure occurred,
     * an error index is set to origPos.errorIndex.
     */
    private int subParse(String text, int start, int patternCharIndex, int count,
                         boolean obeyCount, boolean[] ambiguousYear,
                         ParsePosition origPos)
    {
        Number number = null;
        int value = 0;
        ParsePosition pos = new ParsePosition(0);
        pos.index = start;
        int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];

        // If there are any spaces here, skip over them.  If we hit the end
        // of the string, then fail.
        for (;;) {
            if (pos.index >= text.length()) {
                origPos.errorIndex = start;
                return -1;
            }
            char c = text.charAt(pos.index);
            if (c != ' ' && c != '\t') break;
            ++pos.index;
        }

        // We handle a few special cases here where we need to parse
        // a number value.  We handle further, more generic cases below.  We need
        // to handle some of them here because some fields require extra processing on
        // the parsed value.
        if (patternCharIndex == 4 /*HOUR_OF_DAY1_FIELD*/ ||
            patternCharIndex == 15 /*HOUR1_FIELD*/ ||
            (patternCharIndex == 2 /*MONTH_FIELD*/ && count <= 2) ||
            patternCharIndex == 1)
        {
            // It would be good to unify this with the obeyCount logic below,
            // but that's going to be difficult.
            if (obeyCount)
            {
                if ((start+count) > text.length()) {
                    origPos.errorIndex = start;
                    return -1;
                }
                number = numberFormat.parse(text.substring(0, start+count), pos);
            }
            else number = numberFormat.parse(text, pos);
            if (number == null) {
		if (patternCharIndex != 1 || calendar instanceof GregorianCalendar) {
		    origPos.errorIndex = pos.index;
		    return -1;
		}
            } else {
		value = number.intValue();
	    }
        }

	boolean useDateFormatSymbols = useDateFormatSymbols();

        int index;
        switch (patternCharIndex)
        {
        case 0: // 'G' - ERA
	    if (useDateFormatSymbols) {
		if ((index = matchString(text, start, Calendar.ERA, formatData.getEras())) > 0) {
		    return index;
		}
	    } else {
		Map<String, Integer> map = calendar.getDisplayNames(field,
								    Calendar.ALL_STYLES,
								    locale);
		if ((index = matchString(text, start, field, map)) > 0) {
		    return index;
		}
	    }
	    origPos.errorIndex = pos.index;
	    return -1;

        case 1: // 'y' - YEAR
	    if (!(calendar instanceof GregorianCalendar)) {
		// calendar might have text representations for year values,
		// such as "\u5143" in JapaneseImperialCalendar.
		int style = (count >= 4) ? Calendar.LONG : Calendar.SHORT;
		Map<String, Integer> map = calendar.getDisplayNames(field, style, locale);
		if (map != null) {
		    if ((index = matchString(text, start, field, map)) > 0) {
			return index;
		    }
		}
		calendar.set(field, value);
		return pos.index;
	    }

            // If there are 3 or more YEAR pattern characters, this indicates
            // that the year value is to be treated literally, without any
            // two-digit year adjustments (e.g., from "01" to 2001).  Otherwise
            // we made adjustments to place the 2-digit year in the proper
            // century, for parsed strings from "00" to "99".  Any other string
            // is treated literally:  "2250", "-1", "1", "002".
            if (count <= 2 && (pos.index - start) == 2
                && Character.isDigit(text.charAt(start))
                && Character.isDigit(text.charAt(start+1)))
            {
                // Assume for example that the defaultCenturyStart is 6/18/1903.
                // This means that two-digit years will be forced into the range
                // 6/18/1903 to 6/17/2003.  As a result, years 00, 01, and 02
                // correspond to 2000, 2001, and 2002.  Years 04, 05, etc. correspond
                // to 1904, 1905, etc.  If the year is 03, then it is 2003 if the
                // other fields specify a date before 6/18, or 1903 if they specify a
                // date afterwards.  As a result, 03 is an ambiguous year.  All other
                // two-digit years are unambiguous.
                int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
                ambiguousYear[0] = value == ambiguousTwoDigitYear;
                value += (defaultCenturyStartYear/100)*100 +
                    (value < ambiguousTwoDigitYear ? 100 : 0);
            }
            calendar.set(Calendar.YEAR, value);
            return pos.index;

        case 2: // 'M' - MONTH
            if (count <= 2) // i.e., M or MM.
            {
                // Don't want to parse the month if it is a string
                // while pattern uses numeric style: M or MM.
                // [We computed 'value' above.]
                calendar.set(Calendar.MONTH, value - 1);
                return pos.index;
            }
            else
            {
		if (useDateFormatSymbols) {
		    // count >= 3 // i.e., MMM or MMMM
		    // Want to be able to parse both short and long forms.
		    // Try count == 4 first:
		    int newStart = 0;
		    if ((newStart=matchString(text, start, Calendar.MONTH,
					      formatData.getMonths())) > 0)
			return newStart;
		    else // count == 4 failed, now try count == 3
			if ((index = matchString(text, start, Calendar.MONTH,
						 formatData.getShortMonths())) > 0) {
			    return index;
			}
		} else {
		    Map<String, Integer> map = calendar.getDisplayNames(field,
									Calendar.ALL_STYLES,
									locale);
		    if ((index = matchString(text, start, field, map)) > 0) {
			return index;
		    }
		}
	    }
	    origPos.errorIndex = pos.index;
	    return -1;

        case 4: // 'k' - HOUR_OF_DAY: 1-based.  eg, 23:59 + 1 hour =>> 24:59
            // [We computed 'value' above.]
            if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY)+1) value = 0;
            calendar.set(Calendar.HOUR_OF_DAY, value);
            return pos.index;

        case 9:
	    { // 'E' - DAY_OF_WEEK
		if (useDateFormatSymbols) {
		    // Want to be able to parse both short and long forms.
		    // Try count == 4 (DDDD) first:
		    int newStart = 0;
		    if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
					      formatData.getWeekdays())) > 0)
			return newStart;
		    else // DDDD failed, now try DDD
			if ((index = matchString(text, start, Calendar.DAY_OF_WEEK,
						 formatData.getShortWeekdays())) > 0) {
			    return index;
			}
		} else {
		    int[] styles = { Calendar.LONG, Calendar.SHORT };
		    for (int style : styles) {
			Map<String,Integer> map = calendar.getDisplayNames(field, style, locale);
			if ((index = matchString(text, start, field, map)) > 0) {
			    return index;
			}
		    }
		}
		origPos.errorIndex = pos.index;
		return -1;
	    }

        case 14:    // 'a' - AM_PM
	    if (useDateFormatSymbols) {
		if ((index = matchString(text, start, Calendar.AM_PM, formatData.getAmPmStrings())) > 0) {
		    return index;
		}
	    } else {
		Map<String,Integer> map = calendar.getDisplayNames(field, Calendar.ALL_STYLES, locale);
		if ((index = matchString(text, start, field, map)) > 0) {
		    return index;
		}
	    }
	    origPos.errorIndex = pos.index;
	    return -1;

        case 15: // 'h' - HOUR:1-based.  eg, 11PM + 1 hour =>> 12 AM
            // [We computed 'value' above.]
            if (value == calendar.getLeastMaximum(Calendar.HOUR)+1) value = 0;
            calendar.set(Calendar.HOUR, value);
            return pos.index;

        case 17: // 'z' - ZONE_OFFSET
        case 18: // 'Z' - ZONE_OFFSET
            // First try to parse generic forms such as GMT-07:00. Do this first
            // in case localized TimeZoneNames contains the string "GMT"
            // for a zone; in that case, we don't want to match the first three
            // characters of GMT+/-hh:mm etc.
            {
                int sign = 0;
                int offset;

                // For time zones that have no known names, look for strings
                // of the form:
                //    GMT[+-]hours:minutes or
                //    GMT.
                if ((text.length() - start) >= GMT.length() &&
                    text.regionMatches(true, start, GMT, 0, GMT.length())) {
                    int num;
                    calendar.set(Calendar.DST_OFFSET, 0);
                    pos.index = start + GMT.length();

                    try { // try-catch for "GMT" only time zone string
                        if( text.charAt(pos.index) == '+' ) {
                            sign = 1;
                        } else if( text.charAt(pos.index) == '-' ) {
                            sign = -1;
                        } 
                    }
                    catch(StringIndexOutOfBoundsException e) {}

                    if (sign == 0) {	/* "GMT" without offset */
                        calendar.set(Calendar.ZONE_OFFSET, 0 );
                        return pos.index;
                    }

                    // Look for hours.
                    try {
                        char c = text.charAt(++pos.index);
                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        } else {
                            num = c - '0';
                        }
                        if (text.charAt(++pos.index) != ':') {
                            c = text.charAt(pos.index);
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                                origPos.errorIndex = pos.index;
                                return -1;   // Wasn't actually a number.
                            } else {
                                num *= 10;
                                num += c - '0';
                                pos.index++;
                            }
                        }
                        if (num > 23) {
                            origPos.errorIndex = pos.index - 1;
                            return -1;   // Wasn't actually a number.
                        }
                        if  (text.charAt(pos.index) != ':') {
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        }
                    }
                    catch(StringIndexOutOfBoundsException e) {
                        origPos.errorIndex = pos.index;
                        return -1;   // Wasn't actually a number.
                    }

                    // Look for minutes.
                    offset = num * 60;
                    try {
                        char c = text.charAt(++pos.index);
                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        } else {
                            num = c - '0';
                            c = text.charAt(++pos.index);
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                                origPos.errorIndex = pos.index;
                                return -1;   // Wasn't actually a number.
                            } else {
                                num *= 10;
                                num += c - '0';
                            }
                        }

                        if (num > 59) {
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        }
                    }
                    catch(StringIndexOutOfBoundsException e) {
                        origPos.errorIndex = pos.index;
                        return -1;   // Wasn't actually a number.
                    }
                    offset += num;

                    // Fall through for final processing below of 'offset' and 'sign'.
                }
                else {
                    // At this point, check for named time zones by looking through
                    // the locale data from the TimeZoneNames strings.
                    // Want to be able to parse both short and long forms.
                    int i = subParseZoneString(text, pos.index);
                    if (i != 0) {
                        return i;
                    }

                    // As a last resort, look for numeric timezones of the form
                    // [+-]hhmm as specified by RFC 822.  This code is actually
                    // a little more permissive than RFC 822.  It will try to do
                    // its best with numbers that aren't strictly 4 digits long.
                    try {
                        if( text.charAt(pos.index) == '+' ) {
                            sign = 1;
                        } else if( text.charAt(pos.index) == '-' ) {
                            sign = -1;
                        } 
                        if (sign == 0) {
                            origPos.errorIndex = pos.index;
                            return -1;
                        }

                        // Look for hh.
                        int hours = 0;
                        char c = text.charAt(++pos.index);
                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        } else {
                            hours = c - '0';
                            c = text.charAt(++pos.index);
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                                origPos.errorIndex = pos.index;
                                return -1;   // Wasn't actually a number.
                            } else {
                                hours *= 10;
                                hours += c - '0';
                            }
                        }
                        if (hours > 23) {
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        }

                        // Look for mm.
                        int minutes = 0;
                        c = text.charAt(++pos.index);
                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        } else {
                            minutes = c - '0';
                            c = text.charAt(++pos.index);
                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
                                origPos.errorIndex = pos.index;
                                return -1;   // Wasn't actually a number.
                            } else {
                                minutes *= 10;
                                minutes += c - '0';
                            }
                        }

                        if (minutes > 59) {
                            origPos.errorIndex = pos.index;
                            return -1;   // Wasn't actually a number.
                        }

                        offset = hours * 60 + minutes;
                    } catch(StringIndexOutOfBoundsException e) {
                        origPos.errorIndex = pos.index;
                        return -1;   // Wasn't actually a number.
                    }
                }

                // Do the final processing for both of the above cases.  We only
                // arrive here if the form GMT+/-... or an RFC 822 form was seen.
                if (sign != 0)
                {
                    offset *= millisPerMinute * sign;
                    calendar.set(Calendar.ZONE_OFFSET, offset);
                    calendar.set(Calendar.DST_OFFSET, 0);
                    return ++pos.index;
                }
            }

            // All efforts to parse a zone failed.
            origPos.errorIndex = pos.index;
            return -1;

        default:
            // case 3: // 'd' - DATE
            // case 5: // 'H' - HOUR_OF_DAY:0-based.  eg, 23:59 + 1 hour =>> 00:59
            // case 6: // 'm' - MINUTE
            // case 7: // 's' - SECOND
            // case 8: // 'S' - MILLISECOND
            // case 10: // 'D' - DAY_OF_YEAR
            // case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
            // case 12: // 'w' - WEEK_OF_YEAR
            // case 13: // 'W' - WEEK_OF_MONTH
            // case 16: // 'K' - HOUR: 0-based.  eg, 11PM + 1 hour =>> 0 AM

            // Handle "generic" fields
            if (obeyCount)
            {
                if ((start+count) > text.length()) {
                    origPos.errorIndex = pos.index;
                    return -1;
                }
                number = numberFormat.parse(text.substring(0, start+count), pos);
            }
            else number = numberFormat.parse(text, pos);
            if (number != null) {
                calendar.set(field, number.intValue());
                return pos.index;
            }
            origPos.errorIndex = pos.index;
            return -1;
        }
    }

    private final String getCalendarName() {
	return calendar.getClass().getName();
    }

    private boolean useDateFormatSymbols() {
	if (useDateFormatSymbols) {
	    return true;
	}
	return isGregorianCalendar() || locale == null;
    }

    private boolean isGregorianCalendar() {
	return "java.util.GregorianCalendar".equals(getCalendarName());
    }

    /**
     * Translates a pattern, mapping each character in the from string to the
     * corresponding character in the to string.
     *
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    private String translatePattern(String pattern, String from, String to) {
        StringBuilder result = new StringBuilder();
        boolean inQuote = false;
        for (int i = 0; i < pattern.length(); ++i) {
            char c = pattern.charAt(i);
            if (inQuote) {
                if (c == '\'')
                    inQuote = false;
            }
            else {
                if (c == '\'')
                    inQuote = true;
                else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    int ci = from.indexOf(c);
                    if (ci == -1)
                        throw new IllegalArgumentException("Illegal pattern " +
                                                           " character '" +
                                                           c + "'");
                    c = to.charAt(ci);
                }
            }
            result.append(c);
        }
        if (inQuote)
            throw new IllegalArgumentException("Unfinished quote in pattern");
        return result.toString();
    }

    /**
     * Returns a pattern string describing this date format.
     *
     * @return a pattern string describing this date format.
     */
    public String toPattern() {
        return pattern;
    }

    /**
     * Returns a localized pattern string describing this date format.
     *
     * @return a localized pattern string describing this date format.
     */
    public String toLocalizedPattern() {
        return translatePattern(pattern,
                                DateFormatSymbols.patternChars,
                                formatData.getLocalPatternChars());
    }

    /**
     * Applies the given pattern string to this date format.
     *
     * @param pattern the new date and time pattern for this date format
     * @exception NullPointerException if the given pattern is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public void applyPattern (String pattern)
    {
	compiledPattern = compile(pattern);
        this.pattern = pattern;
    }

    /**
     * Applies the given localized pattern string to this date format.
     *
     * @param pattern a String to be mapped to the new date and time format
     *        pattern for this format
     * @exception NullPointerException if the given pattern is null
     * @exception IllegalArgumentException if the given pattern is invalid
     */
    public void applyLocalizedPattern(String pattern) {
         String p = translatePattern(pattern,
				     formatData.getLocalPatternChars(),
				     DateFormatSymbols.patternChars);
	 compiledPattern = compile(p);
	 this.pattern = p;
    }

    /**
     * Gets a copy of the date and time format symbols of this date format.
     *
     * @return the date and time format symbols of this date format
     * @see #setDateFormatSymbols
     */
    public DateFormatSymbols getDateFormatSymbols()
    {
        return (DateFormatSymbols)formatData.clone();
    }

    /**
     * Sets the date and time format symbols of this date format.
     *
     * @param newFormatSymbols the new date and time format symbols
     * @exception NullPointerException if the given newFormatSymbols is null
     * @see #getDateFormatSymbols
     */
    public void setDateFormatSymbols(DateFormatSymbols newFormatSymbols)
    {
        this.formatData = (DateFormatSymbols)newFormatSymbols.clone();
	useDateFormatSymbols = true;
    }

    /**
     * Creates a copy of this <code>SimpleDateFormat</code>. This also
     * clones the format's date format symbols.
     *
     * @return a clone of this <code>SimpleDateFormat</code>
     */
    public Object clone() {
        SimpleDateFormat other = (SimpleDateFormat) super.clone();
        other.formatData = (DateFormatSymbols) formatData.clone();
        return other;
    }

    /**
     * Returns the hash code value for this <code>SimpleDateFormat</code> object.
     *
     * @return the hash code value for this <code>SimpleDateFormat</code> object.
     */
    public int hashCode()
    {
        return pattern.hashCode();
        // just enough fields for a reasonable distribution
    }

    /**
     * Compares the given object with this <code>SimpleDateFormat</code> for
     * equality.
     *
     * @return true if the given object is equal to this
     * <code>SimpleDateFormat</code>
     */
    public boolean equals(Object obj)
    {
        if (!super.equals(obj)) return false; // super does class check
        SimpleDateFormat that = (SimpleDateFormat) obj;
        return (pattern.equals(that.pattern)
                && formatData.equals(that.formatData));
    }

    /**
     * After reading an object from the input stream, the format
     * pattern in the object is verified.
     * <p>
     * @exception InvalidObjectException if the pattern is invalid
     */
    private void readObject(ObjectInputStream stream)
		         throws IOException, ClassNotFoundException {
	stream.defaultReadObject();

	try {
	    compiledPattern = compile(pattern);
	} catch (Exception e) {
	    throw new InvalidObjectException("invalid pattern");
	}

	if (serialVersionOnStream < 1) {
	    // didn't have defaultCenturyStart field
	    initializeDefaultCentury();
	}
	else {
	    // fill in dependent transient field
	    parseAmbiguousDatesAsAfter(defaultCenturyStart);
	}
	serialVersionOnStream = currentSerialVersion;

	// If the deserialized object has a SimpleTimeZone, try
	// to replace it with a ZoneInfo equivalent in order to
	// be compatible with the SimpleTimeZone-based
	// implementation as much as possible.
	TimeZone tz = getTimeZone();
	if (tz instanceof SimpleTimeZone) {
	    String id = tz.getID();
	    TimeZone zi = TimeZone.getTimeZone(id);
	    if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
		setTimeZone(zi);
	    }
	}
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar