KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > SimpleDateFormat


1 /*
2  * @(#)SimpleDateFormat.java 1.77 04/01/22
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

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

20
21 package java.text;
22
23 import java.util.TimeZone JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import java.util.SimpleTimeZone JavaDoc;
29 import java.util.GregorianCalendar JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.InvalidObjectException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.lang.ClassNotFoundException JavaDoc;
34 import java.util.Hashtable JavaDoc;
35 import java.lang.StringIndexOutOfBoundsException JavaDoc;
36 import sun.text.resources.LocaleData;
37 import sun.util.calendar.CalendarUtils;
38 import sun.util.calendar.ZoneInfoFile;
39
40 /**
41  * <code>SimpleDateFormat</code> is a concrete class for formatting and
42  * parsing dates in a locale-sensitive manner. It allows for formatting
43  * (date -> text), parsing (text -> date), and normalization.
44  *
45  * <p>
46  * <code>SimpleDateFormat</code> allows you to start by choosing
47  * any user-defined patterns for date-time formatting. However, you
48  * are encouraged to create a date-time formatter with either
49  * <code>getTimeInstance</code>, <code>getDateInstance</code>, or
50  * <code>getDateTimeInstance</code> in <code>DateFormat</code>. Each
51  * of these class methods can return a date/time formatter initialized
52  * with a default format pattern. You may modify the format pattern
53  * using the <code>applyPattern</code> methods as desired.
54  * For more information on using these methods, see
55  * {@link DateFormat}.
56  *
57  * <h4>Date and Time Patterns</h4>
58  * <p>
59  * Date and time formats are specified by <em>date and time pattern</em>
60  * strings.
61  * Within date and time pattern strings, unquoted letters from
62  * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
63  * <code>'z'</code> are interpreted as pattern letters representing the
64  * components of a date or time string.
65  * Text can be quoted using single quotes (<code>'</code>) to avoid
66  * interpretation.
67  * <code>"''"</code> represents a single quote.
68  * All other characters are not interpreted; they're simply copied into the
69  * output string during formatting or matched against the input string
70  * during parsing.
71  * <p>
72  * The following pattern letters are defined (all other characters from
73  * <code>'A'</code> to <code>'Z'</code> and from <code>'a'</code> to
74  * <code>'z'</code> are reserved):
75  * <blockquote>
76  * <table border=0 cellspacing=3 cellpadding=0 summary="Chart shows pattern letters, date/time component, presentation, and examples.">
77  * <tr bgcolor="#ccccff">
78  * <th align=left>Letter
79  * <th align=left>Date or Time Component
80  * <th align=left>Presentation
81  * <th align=left>Examples
82  * <tr>
83  * <td><code>G</code>
84  * <td>Era designator
85  * <td><a HREF="#text">Text</a>
86  * <td><code>AD</code>
87  * <tr bgcolor="#eeeeff">
88  * <td><code>y</code>
89  * <td>Year
90  * <td><a HREF="#year">Year</a>
91  * <td><code>1996</code>; <code>96</code>
92  * <tr>
93  * <td><code>M</code>
94  * <td>Month in year
95  * <td><a HREF="#month">Month</a>
96  * <td><code>July</code>; <code>Jul</code>; <code>07</code>
97  * <tr bgcolor="#eeeeff">
98  * <td><code>w</code>
99  * <td>Week in year
100  * <td><a HREF="#number">Number</a>
101  * <td><code>27</code>
102  * <tr>
103  * <td><code>W</code>
104  * <td>Week in month
105  * <td><a HREF="#number">Number</a>
106  * <td><code>2</code>
107  * <tr bgcolor="#eeeeff">
108  * <td><code>D</code>
109  * <td>Day in year
110  * <td><a HREF="#number">Number</a>
111  * <td><code>189</code>
112  * <tr>
113  * <td><code>d</code>
114  * <td>Day in month
115  * <td><a HREF="#number">Number</a>
116  * <td><code>10</code>
117  * <tr bgcolor="#eeeeff">
118  * <td><code>F</code>
119  * <td>Day of week in month
120  * <td><a HREF="#number">Number</a>
121  * <td><code>2</code>
122  * <tr>
123  * <td><code>E</code>
124  * <td>Day in week
125  * <td><a HREF="#text">Text</a>
126  * <td><code>Tuesday</code>; <code>Tue</code>
127  * <tr bgcolor="#eeeeff">
128  * <td><code>a</code>
129  * <td>Am/pm marker
130  * <td><a HREF="#text">Text</a>
131  * <td><code>PM</code>
132  * <tr>
133  * <td><code>H</code>
134  * <td>Hour in day (0-23)
135  * <td><a HREF="#number">Number</a>
136  * <td><code>0</code>
137  * <tr bgcolor="#eeeeff">
138  * <td><code>k</code>
139  * <td>Hour in day (1-24)
140  * <td><a HREF="#number">Number</a>
141  * <td><code>24</code>
142  * <tr>
143  * <td><code>K</code>
144  * <td>Hour in am/pm (0-11)
145  * <td><a HREF="#number">Number</a>
146  * <td><code>0</code>
147  * <tr bgcolor="#eeeeff">
148  * <td><code>h</code>
149  * <td>Hour in am/pm (1-12)
150  * <td><a HREF="#number">Number</a>
151  * <td><code>12</code>
152  * <tr>
153  * <td><code>m</code>
154  * <td>Minute in hour
155  * <td><a HREF="#number">Number</a>
156  * <td><code>30</code>
157  * <tr bgcolor="#eeeeff">
158  * <td><code>s</code>
159  * <td>Second in minute
160  * <td><a HREF="#number">Number</a>
161  * <td><code>55</code>
162  * <tr>
163  * <td><code>S</code>
164  * <td>Millisecond
165  * <td><a HREF="#number">Number</a>
166  * <td><code>978</code>
167  * <tr bgcolor="#eeeeff">
168  * <td><code>z</code>
169  * <td>Time zone
170  * <td><a HREF="#timezone">General time zone</a>
171  * <td><code>Pacific Standard Time</code>; <code>PST</code>; <code>GMT-08:00</code>
172  * <tr>
173  * <td><code>Z</code>
174  * <td>Time zone
175  * <td><a HREF="#rfc822timezone">RFC 822 time zone</a>
176  * <td><code>-0800</code>
177  * </table>
178  * </blockquote>
179  * Pattern letters are usually repeated, as their number determines the
180  * exact presentation:
181  * <ul>
182  * <li><strong><a name="text">Text:</a></strong>
183  * For formatting, if the number of pattern letters is 4 or more,
184  * the full form is used; otherwise a short or abbreviated form
185  * is used if available.
186  * For parsing, both forms are accepted, independent of the number
187  * of pattern letters.
188  * <li><strong><a name="number">Number:</a></strong>
189  * For formatting, the number of pattern letters is the minimum
190  * number of digits, and shorter numbers are zero-padded to this amount.
191  * For parsing, the number of pattern letters is ignored unless
192  * it's needed to separate two adjacent fields.
193  * <li><strong><a name="year">Year:</a></strong>
194  * For formatting, if the number of pattern letters is 2, the year
195  * is truncated to 2 digits; otherwise it is interpreted as a
196  * <a HREF="#number">number</a>.
197  * <p>For parsing, if the number of pattern letters is more than 2,
198  * the year is interpreted literally, regardless of the number of
199  * digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to
200  * Jan 11, 12 A.D.
201  * <p>For parsing with the abbreviated year pattern ("y" or "yy"),
202  * <code>SimpleDateFormat</code> must interpret the abbreviated year
203  * relative to some century. It does this by adjusting dates to be
204  * within 80 years before and 20 years after the time the <code>SimpleDateFormat</code>
205  * instance is created. For example, using a pattern of "MM/dd/yy" and a
206  * <code>SimpleDateFormat</code> instance created on Jan 1, 1997, the string
207  * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
208  * would be interpreted as May 4, 1964.
209  * During parsing, only strings consisting of exactly two digits, as defined by
210  * {@link Character#isDigit(char)}, will be parsed into the default century.
211  * Any other numeric string, such as a one digit string, a three or more digit
212  * string, or a two digit string that isn't all digits (for example, "-1"), is
213  * interpreted literally. So "01/02/3" or "01/02/003" are parsed, using the
214  * same pattern, as Jan 2, 3 AD. Likewise, "01/02/-3" is parsed as Jan 2, 4 BC.
215  * <li><strong><a name="month">Month:</a></strong>
216  * If the number of pattern letters is 3 or more, the month is
217  * interpreted as <a HREF="#text">text</a>; otherwise,
218  * it is interpreted as a <a HREF="#number">number</a>.
219  * <li><strong><a name="timezone">General time zone:</a></strong>
220  * Time zones are interpreted as <a HREF="#text">text</a> if they have
221  * names. For time zones representing a GMT offset value, the
222  * following syntax is used:
223  * <pre>
224  * <a name="GMTOffsetTimeZone"><i>GMTOffsetTimeZone:</i></a>
225  * <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
226  * <i>Sign:</i> one of
227  * <code>+ -</code>
228  * <i>Hours:</i>
229  * <i>Digit</i>
230  * <i>Digit</i> <i>Digit</i>
231  * <i>Minutes:</i>
232  * <i>Digit</i> <i>Digit</i>
233  * <i>Digit:</i> one of
234  * <code>0 1 2 3 4 5 6 7 8 9</code></pre>
235  * <i>Hours</i> must be between 0 and 23, and <i>Minutes</i> must be between
236  * 00 and 59. The format is locale independent and digits must be taken
237  * from the Basic Latin block of the Unicode standard.
238  * <p>For parsing, <a HREF="#rfc822timezone">RFC 822 time zones</a> are also
239  * accepted.
240  * <li><strong><a name="rfc822timezone">RFC 822 time zone:</a></strong>
241  * For formatting, the RFC 822 4-digit time zone format is used:
242  * <pre>
243  * <i>RFC822TimeZone:</i>
244  * <i>Sign</i> <i>TwoDigitHours</i> <i>Minutes</i>
245  * <i>TwoDigitHours:</i>
246  * <i>Digit Digit</i></pre>
247  * <i>TwoDigitHours</i> must be between 00 and 23. Other definitions
248  * are as for <a HREF="#timezone">general time zones</a>.
249  * <p>For parsing, <a HREF="#timezone">general time zones</a> are also
250  * accepted.
251  * </ul>
252  * <code>SimpleDateFormat</code> also supports <em>localized date and time
253  * pattern</em> strings. In these strings, the pattern letters described above
254  * may be replaced with other, locale dependent, pattern letters.
255  * <code>SimpleDateFormat</code> does not deal with the localization of text
256  * other than the pattern letters; that's up to the client of the class.
257  * <p>
258  *
259  * <h4>Examples</h4>
260  *
261  * The following examples show how date and time patterns are interpreted in
262  * the U.S. locale. The given date and time are 2001-07-04 12:08:56 local time
263  * in the U.S. Pacific Time time zone.
264  * <blockquote>
265  * <table border=0 cellspacing=3 cellpadding=0 summary="Examples of date and time patterns interpreted in the U.S. locale">
266  * <tr bgcolor="#ccccff">
267  * <th align=left>Date and Time Pattern
268  * <th align=left>Result
269  * <tr>
270  * <td><code>"yyyy.MM.dd G 'at' HH:mm:ss z"</code>
271  * <td><code>2001.07.04 AD at 12:08:56 PDT</code>
272  * <tr bgcolor="#eeeeff">
273  * <td><code>"EEE, MMM d, ''yy"</code>
274  * <td><code>Wed, Jul 4, '01</code>
275  * <tr>
276  * <td><code>"h:mm a"</code>
277  * <td><code>12:08 PM</code>
278  * <tr bgcolor="#eeeeff">
279  * <td><code>"hh 'o''clock' a, zzzz"</code>
280  * <td><code>12 o'clock PM, Pacific Daylight Time</code>
281  * <tr>
282  * <td><code>"K:mm a, z"</code>
283  * <td><code>0:08 PM, PDT</code>
284  * <tr bgcolor="#eeeeff">
285  * <td><code>"yyyyy.MMMMM.dd GGG hh:mm aaa"</code>
286  * <td><code>02001.July.04 AD 12:08 PM</code>
287  * <tr>
288  * <td><code>"EEE, d MMM yyyy HH:mm:ss Z"</code>
289  * <td><code>Wed, 4 Jul 2001 12:08:56 -0700</code>
290  * <tr bgcolor="#eeeeff">
291  * <td><code>"yyMMddHHmmssZ"</code>
292  * <td><code>010704120856-0700</code>
293  * <tr>
294  * <td><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"</code>
295  * <td><code>2001-07-04T12:08:56.235-0700</code>
296  * </table>
297  * </blockquote>
298  *
299  * <h4><a name="synchronization">Synchronization</a></h4>
300  *
301  * <p>
302  * Date formats are not synchronized.
303  * It is recommended to create separate format instances for each thread.
304  * If multiple threads access a format concurrently, it must be synchronized
305  * externally.
306  *
307  * @see <a HREF="http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html">Java Tutorial</a>
308  * @see java.util.Calendar
309  * @see java.util.TimeZone
310  * @see DateFormat
311  * @see DateFormatSymbols
312  * @version 1.77, 01/22/04
313  * @author Mark Davis, Chen-Lieh Huang, Alan Liu
314  */

315 public class SimpleDateFormat extends DateFormat JavaDoc {
316
317     // the official serial version ID which says cryptically
318
// which version we're compatible with
319
static final long serialVersionUID = 4774881970558875024L;
320
321     // the internal serial version which says which version was written
322
// - 0 (default) for version up to JDK 1.1.3
323
// - 1 for version from JDK 1.1.4, which includes a new field
324
static final int currentSerialVersion = 1;
325
326     /**
327      * The version of the serialized data on the stream. Possible values:
328      * <ul>
329      * <li><b>0</b> or not present on stream: JDK 1.1.3. This version
330      * has no <code>defaultCenturyStart</code> on stream.
331      * <li><b>1</b> JDK 1.1.4 or later. This version adds
332      * <code>defaultCenturyStart</code>.
333      * </ul>
334      * When streaming out this class, the most recent format
335      * and the highest allowable <code>serialVersionOnStream</code>
336      * is written.
337      * @serial
338      * @since JDK1.1.4
339      */

340     private int serialVersionOnStream = currentSerialVersion;
341
342     /**
343      * The pattern string of this formatter. This is always a non-localized
344      * pattern. May not be null. See class documentation for details.
345      * @serial
346      */

347     private String JavaDoc pattern;
348
349     /**
350      * The compiled pattern.
351      */

352     transient private char[] compiledPattern;
353
354     /**
355      * Tags for the compiled pattern.
356      */

357     private final static int TAG_QUOTE_ASCII_CHAR = 100;
358     private final static int TAG_QUOTE_CHARS = 101;
359
360     /**
361      * Locale dependent digit zero.
362      * @see #zeroPaddingNumber
363      * @see java.text.DecimalFormatSymbols#getZeroDigit
364      */

365     transient private char zeroDigit;
366
367     /**
368      * The symbols used by this formatter for week names, month names,
369      * etc. May not be null.
370      * @serial
371      * @see java.text.DateFormatSymbols
372      */

373     private DateFormatSymbols JavaDoc formatData;
374
375     /**
376      * We map dates with two-digit years into the century starting at
377      * <code>defaultCenturyStart</code>, which may be any date. May
378      * not be null.
379      * @serial
380      * @since JDK1.1.4
381      */

382     private Date JavaDoc defaultCenturyStart;
383
384     transient private int defaultCenturyStartYear;
385
386     private static final int millisPerHour = 60 * 60 * 1000;
387     private static final int millisPerMinute = 60 * 1000;
388
389     // For time zones that have no names, use strings GMT+minutes and
390
// GMT-minutes. For instance, in France the time zone is GMT+60.
391
private static final String JavaDoc GMT = "GMT";
392
393     /**
394      * Cache to hold the DateTimePatterns of a Locale.
395      */

396     private static Hashtable JavaDoc cachedLocaleData = new Hashtable JavaDoc(3);
397
398     /**
399      * Cache NumberFormat instances with Locale key.
400      */

401     private static Hashtable JavaDoc cachedNumberFormatData = new Hashtable JavaDoc(3);
402
403     /**
404      * Constructs a <code>SimpleDateFormat</code> using the default pattern and
405      * date format symbols for the default locale.
406      * <b>Note:</b> This constructor may not support all locales.
407      * For full coverage, use the factory methods in the {@link DateFormat}
408      * class.
409      */

410     public SimpleDateFormat() {
411         this(SHORT, SHORT, Locale.getDefault());
412     }
413
414     /**
415      * Constructs a <code>SimpleDateFormat</code> using the given pattern and
416      * the default date format symbols for the default locale.
417      * <b>Note:</b> This constructor may not support all locales.
418      * For full coverage, use the factory methods in the {@link DateFormat}
419      * class.
420      *
421      * @param pattern the pattern describing the date and time format
422      * @exception NullPointerException if the given pattern is null
423      * @exception IllegalArgumentException if the given pattern is invalid
424      */

425     public SimpleDateFormat(String JavaDoc pattern)
426     {
427         this(pattern, Locale.getDefault());
428     }
429
430     /**
431      * Constructs a <code>SimpleDateFormat</code> using the given pattern and
432      * the default date format symbols for the given locale.
433      * <b>Note:</b> This constructor may not support all locales.
434      * For full coverage, use the factory methods in the {@link DateFormat}
435      * class.
436      *
437      * @param pattern the pattern describing the date and time format
438      * @param locale the locale whose date format symbols should be used
439      * @exception NullPointerException if the given pattern is null
440      * @exception IllegalArgumentException if the given pattern is invalid
441      */

442     public SimpleDateFormat(String JavaDoc pattern, Locale JavaDoc locale)
443     {
444         this.pattern = pattern;
445         this.formatData = new DateFormatSymbols JavaDoc(locale);
446         initialize(locale);
447     }
448
449     /**
450      * Constructs a <code>SimpleDateFormat</code> using the given pattern and
451      * date format symbols.
452      *
453      * @param pattern the pattern describing the date and time format
454      * @param formatSymbols the date format symbols to be used for formatting
455      * @exception NullPointerException if the given pattern or formatSymbols is null
456      * @exception IllegalArgumentException if the given pattern is invalid
457      */

458     public SimpleDateFormat(String JavaDoc pattern, DateFormatSymbols JavaDoc formatSymbols)
459     {
460         this.pattern = pattern;
461         this.formatData = (DateFormatSymbols JavaDoc) formatSymbols.clone();
462         initialize(Locale.getDefault());
463     }
464
465     /* Package-private, called by DateFormat factory methods */
466     SimpleDateFormat(int timeStyle, int dateStyle, Locale JavaDoc loc) {
467         /* try the cache first */
468         String JavaDoc[] dateTimePatterns = (String JavaDoc[]) cachedLocaleData.get(loc);
469         if (dateTimePatterns == null) { /* cache miss */
470             ResourceBundle JavaDoc r = LocaleData.getLocaleElements(loc);
471             dateTimePatterns = r.getStringArray("DateTimePatterns");
472             /* update cache */
473             cachedLocaleData.put(loc, dateTimePatterns);
474         }
475     formatData = new DateFormatSymbols JavaDoc(loc);
476     if ((timeStyle >= 0) && (dateStyle >= 0)) {
477         Object JavaDoc[] dateTimeArgs = {dateTimePatterns[timeStyle],
478                      dateTimePatterns[dateStyle + 4]};
479         pattern = MessageFormat.format(dateTimePatterns[8], dateTimeArgs);
480     }
481     else if (timeStyle >= 0) {
482         pattern = dateTimePatterns[timeStyle];
483     }
484     else if (dateStyle >= 0) {
485             pattern = dateTimePatterns[dateStyle + 4];
486     }
487     else {
488         throw new IllegalArgumentException JavaDoc("No date or time style specified");
489     }
490
491     initialize(loc);
492     }
493
494     /* Initialize calendar and numberFormat fields */
495     private void initialize(Locale JavaDoc loc) {
496     // Verify and compile the given pattern.
497
compiledPattern = compile(pattern);
498
499         // The format object must be constructed using the symbols for this zone.
500
// However, the calendar should use the current default TimeZone.
501
// If this is not contained in the locale zone strings, then the zone
502
// will be formatted using generic GMT+/-H:MM nomenclature.
503
calendar = Calendar.getInstance(TimeZone.getDefault(), loc);
504
505         /* try the cache first */
506         numberFormat = (NumberFormat JavaDoc) cachedNumberFormatData.get(loc);
507         if (numberFormat == null) { /* cache miss */
508             numberFormat = NumberFormat.getIntegerInstance(loc);
509             numberFormat.setGroupingUsed(false);
510
511             /* update cache */
512             cachedNumberFormatData.put(loc, numberFormat);
513         }
514         numberFormat = (NumberFormat JavaDoc) numberFormat.clone();
515
516         initializeDefaultCentury();
517     }
518
519     /**
520      * Returns the compiled form of the given pattern. The syntax of
521      * the compiled pattern is:
522      * <blockquote>
523      * CompiledPattern:
524      * EntryList
525      * EntryList:
526      * Entry
527      * EntryList Entry
528      * Entry:
529      * TagField
530      * TagField data
531      * TagField:
532      * Tag Length
533      * TaggedData
534      * Tag:
535      * pattern_char_index
536      * TAG_QUOTE_CHARS
537      * Length:
538      * short_length
539      * long_length
540      * TaggedData:
541      * TAG_QUOTE_ASCII_CHAR ascii_char
542      *
543      * </blockquote>
544      *
545      * where `short_length' is an 8-bit unsigned integer between 0 and
546      * 254. `long_length' is a sequence of an 8-bit integer 255 and a
547      * 32-bit signed integer value which is split into upper and lower
548      * 16-bit fields in two char's. `pattern_char_index' is an 8-bit
549      * integer between 0 and 18. `ascii_char' is an 7-bit ASCII
550      * character value. `data' depends on its Tag value.
551      * <p>
552      * If Length is short_length, Tag and short_length are packed in a
553      * single char, as illustrated below.
554      * <blockquote>
555      * char[0] = (Tag << 8) | short_length;
556      * </blockquote>
557      *
558      * If Length is long_length, Tag and 255 are packed in the first
559      * char and a 32-bit integer, as illustrated below.
560      * <blockquote>
561      * char[0] = (Tag << 8) | 255;
562      * char[1] = (char) (long_length >>> 16);
563      * char[2] = (char) (long_length & 0xffff);
564      * </blockquote>
565      * <p>
566      * If Tag is a pattern_char_index, its Length is the number of
567      * pattern characters. For example, if the given pattern is
568      * "yyyy", Tag is 1 and Length is 4, followed by no data.
569      * <p>
570      * If Tag is TAG_QUOTE_CHARS, its Length is the number of char's
571      * following the TagField. For example, if the given pattern is
572      * "'o''clock'", Length is 7 followed by a char sequence of
573      * <code>o&nbs;'&nbs;c&nbs;l&nbs;o&nbs;c&nbs;k</code>.
574      * <p>
575      * TAG_QUOTE_ASCII_CHAR is a special tag and has an ASCII
576      * character in place of Length. For example, if the given pattern
577      * is "'o'", the TaggedData entry is
578      * <code>((TAG_QUOTE_ASCII_CHAR&nbs;<<&nbs;8)&nbs;|&nbs;'o')</code>.
579      *
580      * @exception NullPointerException if the given pattern is null
581      * @exception IllegalArgumentException if the given pattern is invalid
582      */

583     private char[] compile(String JavaDoc pattern) {
584     int length = pattern.length();
585     boolean inQuote = false;
586     StringBuilder JavaDoc compiledPattern = new StringBuilder JavaDoc(length * 2);
587     StringBuilder JavaDoc tmpBuffer = null;
588     int count = 0;
589     int lastTag = -1;
590
591     for (int i = 0; i < length; i++) {
592         char c = pattern.charAt(i);
593
594         if (c == '\'') {
595         // '' is treated as a single quote regardless of being
596
// in a quoted section.
597
if ((i + 1) < length) {
598             c = pattern.charAt(i + 1);
599             if (c == '\'') {
600             i++;
601             if (count != 0) {
602                 encode(lastTag, count, compiledPattern);
603                 lastTag = -1;
604                 count = 0;
605             }
606             if (inQuote) {
607                 tmpBuffer.append(c);
608             } else {
609                 compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
610             }
611             continue;
612             }
613         }
614         if (!inQuote) {
615             if (count != 0) {
616             encode(lastTag, count, compiledPattern);
617             lastTag = -1;
618             count = 0;
619             }
620             if (tmpBuffer == null) {
621             tmpBuffer = new StringBuilder JavaDoc(length);
622             } else {
623             tmpBuffer.setLength(0);
624             }
625             inQuote = true;
626         } else {
627             int len = tmpBuffer.length();
628             if (len == 1) {
629             char ch = tmpBuffer.charAt(0);
630             if (ch < 128) {
631                 compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | ch));
632             } else {
633                 compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | 1));
634                 compiledPattern.append(ch);
635             }
636             } else {
637             encode(TAG_QUOTE_CHARS, len, compiledPattern);
638             compiledPattern.append(tmpBuffer);
639             }
640             inQuote = false;
641         }
642         continue;
643         }
644         if (inQuote) {
645         tmpBuffer.append(c);
646         continue;
647         }
648         if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
649         if (count != 0) {
650             encode(lastTag, count, compiledPattern);
651             lastTag = -1;
652             count = 0;
653         }
654         if (c < 128) {
655             // In most cases, c would be a delimiter, such as ':'.
656
compiledPattern.append((char)(TAG_QUOTE_ASCII_CHAR << 8 | c));
657         } else {
658             // Take any contiguous non-ASCII alphabet characters and
659
// put them in a single TAG_QUOTE_CHARS.
660
int j;
661             for (j = i + 1; j < length; j++) {
662             char d = pattern.charAt(j);
663             if (d == '\'' || (d >= 'a' && d <= 'z' || d >= 'A' && d <= 'Z')) {
664                 break;
665             }
666             }
667             compiledPattern.append((char)(TAG_QUOTE_CHARS << 8 | (j - i)));
668             for (; i < j; i++) {
669             compiledPattern.append(pattern.charAt(i));
670             }
671             i--;
672         }
673         continue;
674         }
675
676         int tag;
677         if ((tag = formatData.patternChars.indexOf(c)) == -1) {
678         throw new IllegalArgumentException JavaDoc("Illegal pattern character " +
679                            "'" + c + "'");
680         }
681         if (lastTag == -1 || lastTag == tag) {
682         lastTag = tag;
683         count++;
684         continue;
685         }
686         encode(lastTag, count, compiledPattern);
687         lastTag = tag;
688         count = 1;
689     }
690
691     if (inQuote) {
692         throw new IllegalArgumentException JavaDoc("Unterminated quote");
693     }
694
695     if (count != 0) {
696         encode(lastTag, count, compiledPattern);
697     }
698
699     // Copy the compiled pattern to a char array
700
int len = compiledPattern.length();
701     char[] r = new char[len];
702     compiledPattern.getChars(0, len, r, 0);
703     return r;
704     }
705
706     /**
707      * Encodes the given tag and length and puts encoded char(s) into buffer.
708      */

709     private static final void encode(int tag, int length, StringBuilder JavaDoc buffer) {
710     if (length < 255) {
711         buffer.append((char)(tag << 8 | length));
712     } else {
713         buffer.append((char)((tag << 8) | 0xff));
714         buffer.append((char)(length >>> 16));
715         buffer.append((char)(length & 0xffff));
716     }
717     }
718
719     /* Initialize the fields we use to disambiguate ambiguous years. Separate
720      * so we can call it from readObject().
721      */

722     private void initializeDefaultCentury() {
723         calendar.setTime( new Date JavaDoc() );
724         calendar.add( Calendar.YEAR, -80 );
725         parseAmbiguousDatesAsAfter(calendar.getTime());
726     }
727
728     /* Define one-century window into which to disambiguate dates using
729      * two-digit years.
730      */

731     private void parseAmbiguousDatesAsAfter(Date JavaDoc startDate) {
732         defaultCenturyStart = startDate;
733         calendar.setTime(startDate);
734         defaultCenturyStartYear = calendar.get(Calendar.YEAR);
735     }
736
737     /**
738      * Sets the 100-year period 2-digit years will be interpreted as being in
739      * to begin on the date the user specifies.
740      *
741      * @param startDate During parsing, two digit years will be placed in the range
742      * <code>startDate</code> to <code>startDate + 100 years</code>.
743      * @see #get2DigitYearStart
744      * @since 1.2
745      */

746     public void set2DigitYearStart(Date JavaDoc startDate) {
747         parseAmbiguousDatesAsAfter(startDate);
748     }
749
750     /**
751      * Returns the beginning date of the 100-year period 2-digit years are interpreted
752      * as being within.
753      *
754      * @return the start of the 100-year period into which two digit years are
755      * parsed
756      * @see #set2DigitYearStart
757      * @since 1.2
758      */

759     public Date JavaDoc get2DigitYearStart() {
760         return defaultCenturyStart;
761     }
762
763     /**
764      * Formats the given <code>Date</code> into a date/time string and appends
765      * the result to the given <code>StringBuffer</code>.
766      *
767      * @param date the date-time value to be formatted into a date-time string.
768      * @param toAppendTo where the new date-time text is to be appended.
769      * @param pos the formatting position. On input: an alignment field,
770      * if desired. On output: the offsets of the alignment field.
771      * @return the formatted date-time string.
772      * @exception NullPointerException if the given date is null
773      */

774     public StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo,
775                                FieldPosition JavaDoc pos)
776     {
777         pos.beginIndex = pos.endIndex = 0;
778         return format(date, toAppendTo, pos.getFieldDelegate());
779     }
780
781     // Called from Format after creating a FieldDelegate
782
private StringBuffer JavaDoc format(Date JavaDoc date, StringBuffer JavaDoc toAppendTo,
783                                 FieldDelegate delegate) {
784         // Convert input date to time field list
785
calendar.setTime(date);
786
787         for (int i = 0; i < compiledPattern.length; ) {
788             int tag = compiledPattern[i] >>> 8;
789         int count = compiledPattern[i++] & 0xff;
790         if (count == 255) {
791         count = compiledPattern[i++] << 16;
792         count |= compiledPattern[i++];
793         }
794
795         switch (tag) {
796         case TAG_QUOTE_ASCII_CHAR:
797         toAppendTo.append((char)count);
798         break;
799
800         case TAG_QUOTE_CHARS:
801         toAppendTo.append(compiledPattern, i, count);
802         i += count;
803         break;
804
805         default:
806                 subFormat(tag, count, delegate, toAppendTo);
807         break;
808         }
809     }
810         return toAppendTo;
811     }
812
813     /**
814      * Formats an Object producing an <code>AttributedCharacterIterator</code>.
815      * You can use the returned <code>AttributedCharacterIterator</code>
816      * to build the resulting String, as well as to determine information
817      * about the resulting String.
818      * <p>
819      * Each attribute key of the AttributedCharacterIterator will be of type
820      * <code>DateFormat.Field</code>, with the corresponding attribute value
821      * being the same as the attribute key.
822      *
823      * @exception NullPointerException if obj is null.
824      * @exception IllegalArgumentException if the Format cannot format the
825      * given object, or if the Format's pattern string is invalid.
826      * @param obj The object to format
827      * @return AttributedCharacterIterator describing the formatted value.
828      * @since 1.4
829      */

830     public AttributedCharacterIterator JavaDoc formatToCharacterIterator(Object JavaDoc obj) {
831         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
832         CharacterIteratorFieldDelegate JavaDoc delegate = new
833                          CharacterIteratorFieldDelegate JavaDoc();
834
835         if (obj instanceof Date JavaDoc) {
836             format((Date JavaDoc)obj, sb, delegate);
837         }
838         else if (obj instanceof Number JavaDoc) {
839             format(new Date JavaDoc(((Number JavaDoc)obj).longValue()), sb, delegate);
840         }
841         else if (obj == null) {
842             throw new NullPointerException JavaDoc(
843                    "formatToCharacterIterator must be passed non-null object");
844         }
845         else {
846             throw new IllegalArgumentException JavaDoc(
847                              "Cannot format given Object as a Date");
848         }
849         return delegate.getIterator(sb.toString());
850     }
851
852     // Map index into pattern character string to Calendar field number
853
private static final int[] PATTERN_INDEX_TO_CALENDAR_FIELD =
854     {
855         Calendar.ERA, Calendar.YEAR, Calendar.MONTH, Calendar.DATE,
856         Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY, Calendar.MINUTE,
857         Calendar.SECOND, Calendar.MILLISECOND, Calendar.DAY_OF_WEEK,
858         Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK_IN_MONTH,
859         Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
860         Calendar.AM_PM, Calendar.HOUR, Calendar.HOUR, Calendar.ZONE_OFFSET,
861         Calendar.ZONE_OFFSET
862     };
863
864     // Map index into pattern character string to DateFormat field number
865
private static final int[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD = {
866         DateFormat.ERA_FIELD, DateFormat.YEAR_FIELD, DateFormat.MONTH_FIELD,
867         DateFormat.DATE_FIELD, DateFormat.HOUR_OF_DAY1_FIELD,
868         DateFormat.HOUR_OF_DAY0_FIELD, DateFormat.MINUTE_FIELD,
869         DateFormat.SECOND_FIELD, DateFormat.MILLISECOND_FIELD,
870         DateFormat.DAY_OF_WEEK_FIELD, DateFormat.DAY_OF_YEAR_FIELD,
871         DateFormat.DAY_OF_WEEK_IN_MONTH_FIELD, DateFormat.WEEK_OF_YEAR_FIELD,
872         DateFormat.WEEK_OF_MONTH_FIELD, DateFormat.AM_PM_FIELD,
873         DateFormat.HOUR1_FIELD, DateFormat.HOUR0_FIELD,
874         DateFormat.TIMEZONE_FIELD, DateFormat.TIMEZONE_FIELD,
875     };
876
877     // Maps from DecimalFormatSymbols index to Field constant
878
private static final Field[] PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID = {
879         Field.ERA, Field.YEAR, Field.MONTH, Field.DAY_OF_MONTH,
880         Field.HOUR_OF_DAY1, Field.HOUR_OF_DAY0, Field.MINUTE,
881         Field.SECOND, Field.MILLISECOND, Field.DAY_OF_WEEK,
882         Field.DAY_OF_YEAR, Field.DAY_OF_WEEK_IN_MONTH,
883         Field.WEEK_OF_YEAR, Field.WEEK_OF_MONTH,
884         Field.AM_PM, Field.HOUR1, Field.HOUR0, Field.TIME_ZONE,
885         Field.TIME_ZONE,
886     };
887
888     /**
889      * Private member function that does the real date/time formatting.
890      */

891     private void subFormat(int patternCharIndex, int count,
892                FieldDelegate delegate, StringBuffer JavaDoc buffer)
893     {
894         int maxIntCount = Integer.MAX_VALUE;
895         String JavaDoc current = null;
896         int beginOffset = buffer.length();
897
898         int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
899         int value = calendar.get(field);
900
901     // Note: zeroPaddingNumber() assumes that maxDigits is either
902
// 2 or maxIntCount. If we make any changes to this,
903
// zeroPaddingNumber() must be fixed.
904

905         switch (patternCharIndex) {
906         case 0: // 'G' - ERA
907
current = formatData.eras[value];
908             break;
909         case 1: // 'y' - YEAR
910
if (count >= 4)
911         zeroPaddingNumber(value, count, maxIntCount, buffer);
912             else // count < 4
913
zeroPaddingNumber(value, 2, 2, buffer); // clip 1996 to 96
914
break;
915         case 2: // 'M' - MONTH
916
if (count >= 4)
917                 current = formatData.months[value];
918             else if (count == 3)
919                 current = formatData.shortMonths[value];
920             else
921         zeroPaddingNumber(value+1, count, maxIntCount, buffer);
922             break;
923         case 4: // 'k' - HOUR_OF_DAY: 1-based. eg, 23:59 + 1 hour =>> 24:59
924
if (value == 0)
925         zeroPaddingNumber(calendar.getMaximum(Calendar.HOUR_OF_DAY)+1,
926                                             count, maxIntCount, buffer);
927             else
928         zeroPaddingNumber(value, count, maxIntCount, buffer);
929             break;
930         case 9: // 'E' - DAY_OF_WEEK
931
if (count >= 4)
932                 current = formatData.weekdays[value];
933             else // count < 4, use abbreviated form if exists
934
current = formatData.shortWeekdays[value];
935             break;
936         case 14: // 'a' - AM_PM
937
current = formatData.ampms[value];
938             break;
939         case 15: // 'h' - HOUR:1-based. eg, 11PM + 1 hour =>> 12 AM
940
if (value == 0)
941         zeroPaddingNumber(calendar.getLeastMaximum(Calendar.HOUR)+1,
942                   count, maxIntCount, buffer);
943             else
944         zeroPaddingNumber(value, count, maxIntCount, buffer);
945             break;
946         case 17: // 'z' - ZONE_OFFSET
947
int zoneIndex =
948                 formatData.getZoneIndex(calendar.getTimeZone().getID());
949             if (zoneIndex == -1) {
950                 value = calendar.get(Calendar.ZONE_OFFSET) +
951                     calendar.get(Calendar.DST_OFFSET);
952         buffer.append(ZoneInfoFile.toCustomID(value));
953             } else {
954         int index = (calendar.get(Calendar.DST_OFFSET) == 0) ? 1: 3;
955                 if (count < 4) {
956             // Use the short name
957
index++;
958         }
959         buffer.append(formatData.zoneStrings[zoneIndex][index]);
960             }
961             break;
962         case 18: // 'Z' - ZONE_OFFSET ("-/+hhmm" form)
963
value = (calendar.get(Calendar.ZONE_OFFSET) +
964              calendar.get(Calendar.DST_OFFSET)) / 60000;
965
966         int width = 4;
967             if (value >= 0) {
968         buffer.append('+');
969         } else {
970                 width++;
971             }
972
973             int num = (value / 60) * 100 + (value % 60);
974         CalendarUtils.sprintf0d(buffer, num, width);
975             break;
976         default:
977             // case 3: // 'd' - DATE
978
// case 5: // 'H' - HOUR_OF_DAY:0-based. eg, 23:59 + 1 hour =>> 00:59
979
// case 6: // 'm' - MINUTE
980
// case 7: // 's' - SECOND
981
// case 8: // 'S' - MILLISECOND
982
// case 10: // 'D' - DAY_OF_YEAR
983
// case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
984
// case 12: // 'w' - WEEK_OF_YEAR
985
// case 13: // 'W' - WEEK_OF_MONTH
986
// case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM
987
zeroPaddingNumber(value, count, maxIntCount, buffer);
988             break;
989         } // switch (patternCharIndex)
990

991     if (current != null) {
992         buffer.append(current);
993     }
994
995         int fieldID = PATTERN_INDEX_TO_DATE_FORMAT_FIELD[patternCharIndex];
996         Field f = PATTERN_INDEX_TO_DATE_FORMAT_FIELD_ID[patternCharIndex];
997
998         delegate.formatted(fieldID, f, f, beginOffset, buffer.length(), buffer);
999     }
1000
1001    /**
1002     * Formats a number with the specified minimum and maximum number of digits.
1003     */

1004    private final void zeroPaddingNumber(int value, int minDigits, int maxDigits, StringBuffer JavaDoc buffer)
1005    {
1006    // Optimization for 1, 2 and 4 digit numbers. This should
1007
// cover most cases of formatting date/time related items.
1008
// Note: This optimization code assumes that maxDigits is
1009
// either 2 or Integer.MAX_VALUE (maxIntCount in format()).
1010
try {
1011        if (zeroDigit == 0) {
1012        zeroDigit = ((DecimalFormat JavaDoc)numberFormat).getDecimalFormatSymbols().getZeroDigit();
1013        }
1014        if (value >= 0) {
1015        if (value < 100 && minDigits >= 1 && minDigits <= 2) {
1016            if (value < 10) {
1017            if (minDigits == 2) {
1018                buffer.append(zeroDigit);
1019            }
1020            buffer.append((char)(zeroDigit + value));
1021            } else {
1022            buffer.append((char)(zeroDigit + value / 10));
1023            buffer.append((char)(zeroDigit + value % 10));
1024            }
1025            return;
1026        } else if (value >= 1000 && value < 10000) {
1027            if (minDigits == 4) {
1028            buffer.append((char)(zeroDigit + value / 1000));
1029            value %= 1000;
1030            buffer.append((char)(zeroDigit + value / 100));
1031            value %= 100;
1032            buffer.append((char)(zeroDigit + value / 10));
1033            buffer.append((char)(zeroDigit + value % 10));
1034            return;
1035            }
1036            if (minDigits == 2 && maxDigits == 2) {
1037            zeroPaddingNumber(value % 100, 2, 2, buffer);
1038            return;
1039            }
1040        }
1041        }
1042    } catch (Exception JavaDoc e) {
1043    }
1044
1045        numberFormat.setMinimumIntegerDigits(minDigits);
1046        numberFormat.setMaximumIntegerDigits(maxDigits);
1047    numberFormat.format((long)value, buffer, DontCareFieldPosition.INSTANCE);
1048    }
1049
1050
1051    /**
1052     * Parses text from a string to produce a <code>Date</code>.
1053     * <p>
1054     * The method attempts to parse text starting at the index given by
1055     * <code>pos</code>.
1056     * If parsing succeeds, then the index of <code>pos</code> is updated
1057     * to the index after the last character used (parsing does not necessarily
1058     * use all characters up to the end of the string), and the parsed
1059     * date is returned. The updated <code>pos</code> can be used to
1060     * indicate the starting point for the next call to this method.
1061     * If an error occurs, then the index of <code>pos</code> is not
1062     * changed, the error index of <code>pos</code> is set to the index of
1063     * the character where the error occurred, and null is returned.
1064     *
1065     * @param text A <code>String</code>, part of which should be parsed.
1066     * @param pos A <code>ParsePosition</code> object with index and error
1067     * index information as described above.
1068     * @return A <code>Date</code> parsed from the string. In case of
1069     * error, returns null.
1070     * @exception NullPointerException if <code>text</code> or <code>pos</code> is null.
1071     */

1072    public Date JavaDoc parse(String JavaDoc text, ParsePosition JavaDoc pos)
1073    {
1074        int start = pos.index;
1075        int oldStart = start;
1076    int textLength = text.length();
1077
1078        boolean[] ambiguousYear = {false};
1079
1080        calendar.clear(); // Clears all the time fields
1081

1082        for (int i = 0; i < compiledPattern.length; ) {
1083            int tag = compiledPattern[i] >>> 8;
1084        int count = compiledPattern[i++] & 0xff;
1085        if (count == 255) {
1086        count = compiledPattern[i++] << 16;
1087        count |= compiledPattern[i++];
1088        }
1089
1090        switch (tag) {
1091        case TAG_QUOTE_ASCII_CHAR:
1092        if (start >= textLength || text.charAt(start) != (char)count) {
1093            pos.index = oldStart;
1094            pos.errorIndex = start;
1095            return null;
1096        }
1097        start++;
1098        break;
1099
1100        case TAG_QUOTE_CHARS:
1101        while (count-- > 0) {
1102            if (start >= textLength || text.charAt(start) != compiledPattern[i++]) {
1103            pos.index = oldStart;
1104            pos.errorIndex = start;
1105            return null;
1106            }
1107            start++;
1108        }
1109        break;
1110
1111        default:
1112        // Peek the next pattern to determine if we need to
1113
// obey the number of pattern letters for
1114
// parsing. It's required when parsing contiguous
1115
// digit text (e.g., "20010704") with a pattern which
1116
// has no delimiters between fields, like "yyyyMMdd".
1117
boolean obeyCount = false;
1118        if (i < compiledPattern.length) {
1119            int nextTag = compiledPattern[i] >>> 8;
1120            if (!(nextTag == TAG_QUOTE_ASCII_CHAR || nextTag == TAG_QUOTE_CHARS)) {
1121            obeyCount = true;
1122            }
1123        }
1124        start = subParse(text, start, tag, count, obeyCount,
1125                 ambiguousYear, pos);
1126        if (start < 0) {
1127            pos.index = oldStart;
1128            return null;
1129        }
1130        }
1131    }
1132
1133        // At this point the fields of Calendar have been set. Calendar
1134
// will fill in default values for missing fields when the time
1135
// is computed.
1136

1137        pos.index = start;
1138
1139        // This part is a problem: When we call parsedDate.after, we compute the time.
1140
// Take the date April 3 2004 at 2:30 am. When this is first set up, the year
1141
// will be wrong if we're parsing a 2-digit year pattern. It will be 1904.
1142
// April 3 1904 is a Sunday (unlike 2004) so it is the DST onset day. 2:30 am
1143
// is therefore an "impossible" time, since the time goes from 1:59 to 3:00 am
1144
// on that day. It is therefore parsed out to fields as 3:30 am. Then we
1145
// add 100 years, and get April 3 2004 at 3:30 am. Note that April 3 2004 is
1146
// a Saturday, so it can have a 2:30 am -- and it should. [LIU]
1147
/*
1148        Date parsedDate = calendar.getTime();
1149        if( ambiguousYear[0] && !parsedDate.after(defaultCenturyStart) ) {
1150            calendar.add(Calendar.YEAR, 100);
1151            parsedDate = calendar.getTime();
1152        }
1153        */

1154        // Because of the above condition, save off the fields in case we need to readjust.
1155
// The procedure we use here is not particularly efficient, but there is no other
1156
// way to do this given the API restrictions present in Calendar. We minimize
1157
// inefficiency by only performing this computation when it might apply, that is,
1158
// when the two-digit year is equal to the start year, and thus might fall at the
1159
// front or the back of the default century. This only works because we adjust
1160
// the year correctly to start with in other cases -- see subParse().
1161
Date JavaDoc parsedDate;
1162        try {
1163            if (ambiguousYear[0]) // If this is true then the two-digit year == the default start year
1164
{
1165                // We need a copy of the fields, and we need to avoid triggering a call to
1166
// complete(), which will recalculate the fields. Since we can't access
1167
// the fields[] array in Calendar, we clone the entire object. This will
1168
// stop working if Calendar.clone() is ever rewritten to call complete().
1169
Calendar JavaDoc savedCalendar = (Calendar JavaDoc)calendar.clone();
1170                parsedDate = calendar.getTime();
1171                if (parsedDate.before(defaultCenturyStart))
1172                {
1173                    // We can't use add here because that does a complete() first.
1174
savedCalendar.set(Calendar.YEAR, defaultCenturyStartYear + 100);
1175                    parsedDate = savedCalendar.getTime();
1176                }
1177            }
1178            else parsedDate = calendar.getTime();
1179        }
1180        // An IllegalArgumentException will be thrown by Calendar.getTime()
1181
// if any fields are out of range, e.g., MONTH == 17.
1182
catch (IllegalArgumentException JavaDoc e) {
1183            pos.errorIndex = start;
1184            pos.index = oldStart;
1185            return null;
1186        }
1187
1188        return parsedDate;
1189    }
1190
1191    /**
1192     * Private code-size reduction function used by subParse.
1193     * @param text the time text being parsed.
1194     * @param start where to start parsing.
1195     * @param field the date field being parsed.
1196     * @param data the string array to parsed.
1197     * @return the new start position if matching succeeded; a negative number
1198     * indicating matching failure, otherwise.
1199     */

1200    private int matchString(String JavaDoc text, int start, int field, String JavaDoc[] data)
1201    {
1202        int i = 0;
1203        int count = data.length;
1204
1205        if (field == Calendar.DAY_OF_WEEK) i = 1;
1206
1207        // There may be multiple strings in the data[] array which begin with
1208
// the same prefix (e.g., Cerven and Cervenec (June and July) in Czech).
1209
// We keep track of the longest match, and return that. Note that this
1210
// unfortunately requires us to test all array elements.
1211
int bestMatchLength = 0, bestMatch = -1;
1212        for (; i<count; ++i)
1213        {
1214            int length = data[i].length();
1215            // Always compare if we have no match yet; otherwise only compare
1216
// against potentially better matches (longer strings).
1217
if (length > bestMatchLength &&
1218                text.regionMatches(true, start, data[i], 0, length))
1219            {
1220                bestMatch = i;
1221                bestMatchLength = length;
1222            }
1223        }
1224        if (bestMatch >= 0)
1225        {
1226            calendar.set(field, bestMatch);
1227            return start + bestMatchLength;
1228        }
1229        return -start;
1230    }
1231
1232    private int matchZoneString(String JavaDoc text, int start, int zoneIndex) {
1233    for (int j = 1; j <= 4; ++j) {
1234        // Checking long and short zones [1 & 2],
1235
// and long and short daylight [3 & 4].
1236
String JavaDoc zoneName = formatData.zoneStrings[zoneIndex][j];
1237        if (text.regionMatches(true, start,
1238                   zoneName, 0, zoneName.length())) {
1239        return j;
1240        }
1241    }
1242    return -1;
1243    }
1244
1245    private boolean matchDSTString(String JavaDoc text, int start, int zoneIndex, int standardIndex) {
1246    int index = standardIndex + 2;
1247    String JavaDoc zoneName = formatData.zoneStrings[zoneIndex][index];
1248    if (text.regionMatches(true, start,
1249                   zoneName, 0, zoneName.length())) {
1250        return true;
1251    }
1252    return false;
1253    }
1254
1255    /**
1256     * find time zone 'text' matched zoneStrings and set to internal
1257     * calendar.
1258     */

1259    private int subParseZoneString(String JavaDoc text, int start) {
1260    boolean useSameName = false; // true if standard and daylight time use the same abbreviation.
1261
TimeZone JavaDoc currentTimeZone = getTimeZone();
1262
1263    // At this point, check for named time zones by looking through
1264
// the locale data from the DateFormatZoneData strings.
1265
// Want to be able to parse both short and long forms.
1266
int zoneIndex =
1267        formatData.getZoneIndex (currentTimeZone.getID());
1268    TimeZone JavaDoc tz = null;
1269    int j = 0, i = 0;
1270    if ((zoneIndex != -1) && ((j = matchZoneString(text, start, zoneIndex)) > 0)) {
1271        if (j <= 2) {
1272        useSameName = matchDSTString(text, start, zoneIndex, j);
1273        }
1274        tz = TimeZone.getTimeZone(formatData.zoneStrings[zoneIndex][0]);
1275        i = zoneIndex;
1276    }
1277    if (tz == null) {
1278        zoneIndex =
1279        formatData.getZoneIndex (TimeZone.getDefault().getID());
1280        if ((zoneIndex != -1) && ((j = matchZoneString(text, start, zoneIndex)) > 0)) {
1281        if (j <= 2) {
1282            useSameName = matchDSTString(text, start, zoneIndex, j);
1283        }
1284        tz = TimeZone.getTimeZone(formatData.zoneStrings[zoneIndex][0]);
1285        i = zoneIndex;
1286        }
1287    }
1288
1289    if (tz == null) {
1290        for (i = 0; i < formatData.zoneStrings.length; i++) {
1291        if ((j = matchZoneString(text, start, i)) > 0) {
1292            if (j <= 2) {
1293            useSameName = matchDSTString(text, start, i, j);
1294            }
1295            tz = TimeZone.getTimeZone(formatData.zoneStrings[i][0]);
1296            break;
1297        }
1298        }
1299    }
1300    if (tz != null) { // Matched any ?
1301
if (!tz.equals(currentTimeZone)) {
1302        setTimeZone(tz);
1303        }
1304        // If the time zone matched uses the same name
1305
// (abbreviation) for both standard and daylight time,
1306
// let the time zone in the Calendar decide which one.
1307
if (!useSameName) {
1308        calendar.set(Calendar.ZONE_OFFSET, tz.getRawOffset());
1309        calendar.set(Calendar.DST_OFFSET,
1310                 j >= 3 ? tz.getDSTSavings() : 0);
1311        }
1312        return (start + formatData.zoneStrings[i][j].length());
1313    }
1314    return 0;
1315    }
1316
1317    /**
1318     * Private member function that converts the parsed date strings into
1319     * timeFields. Returns -start (for ParsePosition) if failed.
1320     * @param text the time text to be parsed.
1321     * @param start where to start parsing.
1322     * @param ch the pattern character for the date field text to be parsed.
1323     * @param count the count of a pattern character.
1324     * @param obeyCount if true, then the next field directly abuts this one,
1325     * and we should use the count to know when to stop parsing.
1326     * @param ambiguousYear return parameter; upon return, if ambiguousYear[0]
1327     * is true, then a two-digit year was parsed and may need to be readjusted.
1328     * @param origPos origPos.errorIndex is used to return an error index
1329     * at which a parse error occurred, if matching failure occurs.
1330     * @return the new start position if matching succeeded; -1 indicating
1331     * matching failure, otherwise. In case matching failure occurred,
1332     * an error index is set to origPos.errorIndex.
1333     */

1334    private int subParse(String JavaDoc text, int start, int patternCharIndex, int count,
1335                         boolean obeyCount, boolean[] ambiguousYear,
1336                         ParsePosition JavaDoc origPos)
1337    {
1338        Number JavaDoc number = null;
1339        int value = 0;
1340        ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
1341        pos.index = start;
1342        int field = PATTERN_INDEX_TO_CALENDAR_FIELD[patternCharIndex];
1343
1344        // If there are any spaces here, skip over them. If we hit the end
1345
// of the string, then fail.
1346
for (;;) {
1347            if (pos.index >= text.length()) {
1348                origPos.errorIndex = start;
1349                return -1;
1350            }
1351            char c = text.charAt(pos.index);
1352            if (c != ' ' && c != '\t') break;
1353            ++pos.index;
1354        }
1355
1356        // We handle a few special cases here where we need to parse
1357
// a number value. We handle further, more generic cases below. We need
1358
// to handle some of them here because some fields require extra processing on
1359
// the parsed value.
1360
if (patternCharIndex == 4 /*HOUR_OF_DAY1_FIELD*/ ||
1361            patternCharIndex == 15 /*HOUR1_FIELD*/ ||
1362            (patternCharIndex == 2 /*MONTH_FIELD*/ && count <= 2) ||
1363            patternCharIndex == 1)
1364        {
1365            // It would be good to unify this with the obeyCount logic below,
1366
// but that's going to be difficult.
1367
if (obeyCount)
1368            {
1369                if ((start+count) > text.length()) {
1370                    origPos.errorIndex = start;
1371                    return -1;
1372                }
1373                number = numberFormat.parse(text.substring(0, start+count), pos);
1374            }
1375            else number = numberFormat.parse(text, pos);
1376            if (number == null) {
1377                origPos.errorIndex = pos.index;
1378                return -1;
1379            }
1380            value = number.intValue();
1381        }
1382
1383        int index;
1384        switch (patternCharIndex)
1385        {
1386        case 0: // 'G' - ERA
1387
if ((index = matchString(text, start, Calendar.ERA, formatData.eras)) > 0) {
1388                return index;
1389            } else {
1390                origPos.errorIndex = pos.index;
1391                return -1;
1392            }
1393        case 1: // 'y' - YEAR
1394
// If there are 3 or more YEAR pattern characters, this indicates
1395
// that the year value is to be treated literally, without any
1396
// two-digit year adjustments (e.g., from "01" to 2001). Otherwise
1397
// we made adjustments to place the 2-digit year in the proper
1398
// century, for parsed strings from "00" to "99". Any other string
1399
// is treated literally: "2250", "-1", "1", "002".
1400
if (count <= 2 && (pos.index - start) == 2
1401                && Character.isDigit(text.charAt(start))
1402                && Character.isDigit(text.charAt(start+1)))
1403            {
1404                // Assume for example that the defaultCenturyStart is 6/18/1903.
1405
// This means that two-digit years will be forced into the range
1406
// 6/18/1903 to 6/17/2003. As a result, years 00, 01, and 02
1407
// correspond to 2000, 2001, and 2002. Years 04, 05, etc. correspond
1408
// to 1904, 1905, etc. If the year is 03, then it is 2003 if the
1409
// other fields specify a date before 6/18, or 1903 if they specify a
1410
// date afterwards. As a result, 03 is an ambiguous year. All other
1411
// two-digit years are unambiguous.
1412
int ambiguousTwoDigitYear = defaultCenturyStartYear % 100;
1413                ambiguousYear[0] = value == ambiguousTwoDigitYear;
1414                value += (defaultCenturyStartYear/100)*100 +
1415                    (value < ambiguousTwoDigitYear ? 100 : 0);
1416            }
1417            calendar.set(Calendar.YEAR, value);
1418            return pos.index;
1419        case 2: // 'M' - MONTH
1420
if (count <= 2) // i.e., M or MM.
1421
{
1422                // Don't want to parse the month if it is a string
1423
// while pattern uses numeric style: M or MM.
1424
// [We computed 'value' above.]
1425
calendar.set(Calendar.MONTH, value - 1);
1426                return pos.index;
1427            }
1428            else
1429            {
1430                // count >= 3 // i.e., MMM or MMMM
1431
// Want to be able to parse both short and long forms.
1432
// Try count == 4 first:
1433
int newStart = 0;
1434                if ((newStart=matchString(text, start, Calendar.MONTH,
1435                                          formatData.months)) > 0)
1436                    return newStart;
1437                else // count == 4 failed, now try count == 3
1438
if ((index = matchString(text, start, Calendar.MONTH,
1439                                       formatData.shortMonths)) > 0) {
1440                        return index;
1441                    } else {
1442                        origPos.errorIndex = pos.index;
1443                        return -1;
1444                    }
1445            }
1446        case 4: // 'k' - HOUR_OF_DAY: 1-based. eg, 23:59 + 1 hour =>> 24:59
1447
// [We computed 'value' above.]
1448
if (value == calendar.getMaximum(Calendar.HOUR_OF_DAY)+1) value = 0;
1449            calendar.set(Calendar.HOUR_OF_DAY, value);
1450            return pos.index;
1451        case 9: { // 'E' - DAY_OF_WEEK
1452
// Want to be able to parse both short and long forms.
1453
// Try count == 4 (DDDD) first:
1454
int newStart = 0;
1455            if ((newStart=matchString(text, start, Calendar.DAY_OF_WEEK,
1456                                      formatData.weekdays)) > 0)
1457                return newStart;
1458            else // DDDD failed, now try DDD
1459
if ((index = matchString(text, start, Calendar.DAY_OF_WEEK,
1460                                   formatData.shortWeekdays)) > 0) {
1461                    return index;
1462                } else {
1463                    origPos.errorIndex = pos.index;
1464                    return -1;
1465                }
1466        }
1467        case 14: // 'a' - AM_PM
1468
if ((index = matchString(text, start, Calendar.AM_PM, formatData.ampms)) > 0) {
1469                return index;
1470            } else {
1471                origPos.errorIndex = pos.index;
1472                return -1;
1473            }
1474
1475        case 15: // 'h' - HOUR:1-based. eg, 11PM + 1 hour =>> 12 AM
1476
// [We computed 'value' above.]
1477
if (value == calendar.getLeastMaximum(Calendar.HOUR)+1) value = 0;
1478            calendar.set(Calendar.HOUR, value);
1479            return pos.index;
1480        case 17: // 'z' - ZONE_OFFSET
1481
case 18: // 'Z' - ZONE_OFFSET
1482
// First try to parse generic forms such as GMT-07:00. Do this first
1483
// in case localized DateFormatZoneData contains the string "GMT"
1484
// for a zone; in that case, we don't want to match the first three
1485
// characters of GMT+/-hh:mm etc.
1486
{
1487                int sign = 0;
1488                int offset;
1489
1490                // For time zones that have no known names, look for strings
1491
// of the form:
1492
// GMT[+-]hours:minutes or
1493
// GMT.
1494
if ((text.length() - start) >= GMT.length() &&
1495                    text.regionMatches(true, start, GMT, 0, GMT.length())) {
1496                    int num;
1497                    calendar.set(Calendar.DST_OFFSET, 0);
1498                    pos.index = start + GMT.length();
1499
1500                    try { // try-catch for "GMT" only time zone string
1501
if( text.charAt(pos.index) == '+' ) {
1502                            sign = 1;
1503                        } else if( text.charAt(pos.index) == '-' ) {
1504                            sign = -1;
1505                        }
1506                    }
1507                    catch(StringIndexOutOfBoundsException JavaDoc e) {}
1508
1509                    if (sign == 0) { /* "GMT" without offset */
1510                        calendar.set(Calendar.ZONE_OFFSET, 0 );
1511                        return pos.index;
1512                    }
1513
1514                    // Look for hours.
1515
try {
1516                        char c = text.charAt(++pos.index);
1517                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1518                            origPos.errorIndex = pos.index;
1519                            return -1; // Wasn't actually a number.
1520
} else {
1521                            num = c - '0';
1522                        }
1523                        if (text.charAt(++pos.index) != ':') {
1524                            c = text.charAt(pos.index);
1525                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1526                                origPos.errorIndex = pos.index;
1527                                return -1; // Wasn't actually a number.
1528
} else {
1529                                num *= 10;
1530                                num += c - '0';
1531                                pos.index++;
1532                            }
1533                        }
1534                        if (num > 23) {
1535                            origPos.errorIndex = pos.index - 1;
1536                            return -1; // Wasn't actually a number.
1537
}
1538                        if (text.charAt(pos.index) != ':') {
1539                            origPos.errorIndex = pos.index;
1540                            return -1; // Wasn't actually a number.
1541
}
1542                    }
1543                    catch(StringIndexOutOfBoundsException JavaDoc e) {
1544                        origPos.errorIndex = pos.index;
1545                        return -1; // Wasn't actually a number.
1546
}
1547
1548                    // Look for minutes.
1549
offset = num * 60;
1550                    try {
1551                        char c = text.charAt(++pos.index);
1552                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1553                            origPos.errorIndex = pos.index;
1554                            return -1; // Wasn't actually a number.
1555
} else {
1556                            num = c - '0';
1557                            c = text.charAt(++pos.index);
1558                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1559                                origPos.errorIndex = pos.index;
1560                                return -1; // Wasn't actually a number.
1561
} else {
1562                                num *= 10;
1563                                num += c - '0';
1564                            }
1565                        }
1566
1567                        if (num > 59) {
1568                            origPos.errorIndex = pos.index;
1569                            return -1; // Wasn't actually a number.
1570
}
1571                    }
1572                    catch(StringIndexOutOfBoundsException JavaDoc e) {
1573                        origPos.errorIndex = pos.index;
1574                        return -1; // Wasn't actually a number.
1575
}
1576                    offset += num;
1577
1578                    // Fall through for final processing below of 'offset' and 'sign'.
1579
}
1580                else {
1581                    // At this point, check for named time zones by looking through
1582
// the locale data from the DateFormatZoneData strings.
1583
// Want to be able to parse both short and long forms.
1584
int i = subParseZoneString(text, pos.index);
1585                    if (i != 0) {
1586                        return i;
1587                    }
1588
1589                    // As a last resort, look for numeric timezones of the form
1590
// [+-]hhmm as specified by RFC 822. This code is actually
1591
// a little more permissive than RFC 822. It will try to do
1592
// its best with numbers that aren't strictly 4 digits long.
1593
try {
1594                        if( text.charAt(pos.index) == '+' ) {
1595                            sign = 1;
1596                        } else if( text.charAt(pos.index) == '-' ) {
1597                            sign = -1;
1598                        }
1599                        if (sign == 0) {
1600                            origPos.errorIndex = pos.index;
1601                            return -1;
1602                        }
1603
1604                        // Look for hh.
1605
int hours = 0;
1606                        char c = text.charAt(++pos.index);
1607                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1608                            origPos.errorIndex = pos.index;
1609                            return -1; // Wasn't actually a number.
1610
} else {
1611                            hours = c - '0';
1612                            c = text.charAt(++pos.index);
1613                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1614                                origPos.errorIndex = pos.index;
1615                                return -1; // Wasn't actually a number.
1616
} else {
1617                                hours *= 10;
1618                                hours += c - '0';
1619                            }
1620                        }
1621                        if (hours > 23) {
1622                            origPos.errorIndex = pos.index;
1623                            return -1; // Wasn't actually a number.
1624
}
1625
1626                        // Look for mm.
1627
int minutes = 0;
1628                        c = text.charAt(++pos.index);
1629                        if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1630                            origPos.errorIndex = pos.index;
1631                            return -1; // Wasn't actually a number.
1632
} else {
1633                            minutes = c - '0';
1634                            c = text.charAt(++pos.index);
1635                            if (c < '0' || c > '9') { /* must be from '0' to '9'. */
1636                                origPos.errorIndex = pos.index;
1637                                return -1; // Wasn't actually a number.
1638
} else {
1639                                minutes *= 10;
1640                                minutes += c - '0';
1641                            }
1642                        }
1643
1644                        if (minutes > 59) {
1645                            origPos.errorIndex = pos.index;
1646                            return -1; // Wasn't actually a number.
1647
}
1648
1649                        offset = hours * 60 + minutes;
1650                    } catch(StringIndexOutOfBoundsException JavaDoc e) {
1651                        origPos.errorIndex = pos.index;
1652                        return -1; // Wasn't actually a number.
1653
}
1654                }
1655
1656                // Do the final processing for both of the above cases. We only
1657
// arrive here if the form GMT+/-... or an RFC 822 form was seen.
1658
if (sign != 0)
1659                {
1660                    offset *= millisPerMinute * sign;
1661                    calendar.set(Calendar.ZONE_OFFSET, offset);
1662                    calendar.set(Calendar.DST_OFFSET, 0);
1663                    return ++pos.index;
1664                }
1665            }
1666
1667            // All efforts to parse a zone failed.
1668
origPos.errorIndex = pos.index;
1669            return -1;
1670
1671        default:
1672            // case 3: // 'd' - DATE
1673
// case 5: // 'H' - HOUR_OF_DAY:0-based. eg, 23:59 + 1 hour =>> 00:59
1674
// case 6: // 'm' - MINUTE
1675
// case 7: // 's' - SECOND
1676
// case 8: // 'S' - MILLISECOND
1677
// case 10: // 'D' - DAY_OF_YEAR
1678
// case 11: // 'F' - DAY_OF_WEEK_IN_MONTH
1679
// case 12: // 'w' - WEEK_OF_YEAR
1680
// case 13: // 'W' - WEEK_OF_MONTH
1681
// case 16: // 'K' - HOUR: 0-based. eg, 11PM + 1 hour =>> 0 AM
1682

1683            // Handle "generic" fields
1684
if (obeyCount)
1685            {
1686                if ((start+count) > text.length()) {
1687                    origPos.errorIndex = pos.index;
1688                    return -1;
1689                }
1690                number = numberFormat.parse(text.substring(0, start+count), pos);
1691            }
1692            else number = numberFormat.parse(text, pos);
1693            if (number != null) {
1694                calendar.set(field, number.intValue());
1695                return pos.index;
1696            }
1697            origPos.errorIndex = pos.index;
1698            return -1;
1699        }
1700    }
1701
1702
1703    /**
1704     * Translates a pattern, mapping each character in the from string to the
1705     * corresponding character in the to string.
1706     *
1707     * @exception IllegalArgumentException if the given pattern is invalid
1708     */

1709    private String JavaDoc translatePattern(String JavaDoc pattern, String JavaDoc from, String JavaDoc to) {
1710        StringBuilder JavaDoc result = new StringBuilder JavaDoc();
1711        boolean inQuote = false;
1712        for (int i = 0; i < pattern.length(); ++i) {
1713            char c = pattern.charAt(i);
1714            if (inQuote) {
1715                if (c == '\'')
1716                    inQuote = false;
1717            }
1718            else {
1719                if (c == '\'')
1720                    inQuote = true;
1721                else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
1722                    int ci = from.indexOf(c);
1723                    if (ci == -1)
1724                        throw new IllegalArgumentException JavaDoc("Illegal pattern " +
1725                                                           " character '" +
1726                                                           c + "'");
1727                    c = to.charAt(ci);
1728                }
1729            }
1730            result.append(c);
1731        }
1732        if (inQuote)
1733            throw new IllegalArgumentException JavaDoc("Unfinished quote in pattern");
1734        return result.toString();
1735    }
1736
1737    /**
1738     * Returns a pattern string describing this date format.
1739     *
1740     * @return a pattern string describing this date format.
1741     */

1742    public String JavaDoc toPattern() {
1743        return pattern;
1744    }
1745
1746    /**
1747     * Returns a localized pattern string describing this date format.
1748     *
1749     * @return a localized pattern string describing this date format.
1750     */

1751    public String JavaDoc toLocalizedPattern() {
1752        return translatePattern(pattern,
1753                                formatData.patternChars,
1754                                formatData.localPatternChars);
1755    }
1756
1757    /**
1758     * Applies the given pattern string to this date format.
1759     *
1760     * @param pattern the new date and time pattern for this date format
1761     * @exception NullPointerException if the given pattern is null
1762     * @exception IllegalArgumentException if the given pattern is invalid
1763     */

1764    public void applyPattern (String JavaDoc pattern)
1765    {
1766    compiledPattern = compile(pattern);
1767        this.pattern = pattern;
1768    }
1769
1770    /**
1771     * Applies the given localized pattern string to this date format.
1772     *
1773     * @param pattern a String to be mapped to the new date and time format
1774     * pattern for this format
1775     * @exception NullPointerException if the given pattern is null
1776     * @exception IllegalArgumentException if the given pattern is invalid
1777     */

1778    public void applyLocalizedPattern(String JavaDoc pattern) {
1779         String JavaDoc p = translatePattern(pattern,
1780                     formatData.localPatternChars,
1781                     formatData.patternChars);
1782     compiledPattern = compile(p);
1783     this.pattern = p;
1784    }
1785
1786    /**
1787     * Gets a copy of the date and time format symbols of this date format.
1788     *
1789     * @return the date and time format symbols of this date format
1790     * @see #setDateFormatSymbols
1791     */

1792    public DateFormatSymbols JavaDoc getDateFormatSymbols()
1793    {
1794        return (DateFormatSymbols JavaDoc)formatData.clone();
1795    }
1796
1797    /**
1798     * Sets the date and time format symbols of this date format.
1799     *
1800     * @param newFormatSymbols the new date and time format symbols
1801     * @exception NullPointerException if the given newFormatSymbols is null
1802     * @see #getDateFormatSymbols
1803     */

1804    public void setDateFormatSymbols(DateFormatSymbols JavaDoc newFormatSymbols)
1805    {
1806        this.formatData = (DateFormatSymbols JavaDoc)newFormatSymbols.clone();
1807    }
1808
1809    /**
1810     * Creates a copy of this <code>SimpleDateFormat</code>. This also
1811     * clones the format's date format symbols.
1812     *
1813     * @return a clone of this <code>SimpleDateFormat</code>
1814     */

1815    public Object JavaDoc clone() {
1816        SimpleDateFormat JavaDoc other = (SimpleDateFormat JavaDoc) super.clone();
1817        other.formatData = (DateFormatSymbols JavaDoc) formatData.clone();
1818        return other;
1819    }
1820
1821    /**
1822     * Returns the hash code value for this <code>SimpleDateFormat</code> object.
1823     *
1824     * @return the hash code value for this <code>SimpleDateFormat</code> object.
1825     */

1826    public int hashCode()
1827    {
1828        return pattern.hashCode();
1829        // just enough fields for a reasonable distribution
1830
}
1831
1832    /**
1833     * Compares the given object with this <code>SimpleDateFormat</code> for
1834     * equality.
1835     *
1836     * @return true if the given object is equal to this
1837     * <code>SimpleDateFormat</code>
1838     */

1839    public boolean equals(Object JavaDoc obj)
1840    {
1841        if (!super.equals(obj)) return false; // super does class check
1842
SimpleDateFormat JavaDoc that = (SimpleDateFormat JavaDoc) obj;
1843        return (pattern.equals(that.pattern)
1844                && formatData.equals(that.formatData));
1845    }
1846
1847    /**
1848     * After reading an object from the input stream, the format
1849     * pattern in the object is verified.
1850     * <p>
1851     * @exception InvalidObjectException if the pattern is invalid
1852     */

1853    private void readObject(ObjectInputStream JavaDoc stream)
1854                 throws IOException JavaDoc, ClassNotFoundException JavaDoc {
1855    stream.defaultReadObject();
1856
1857    try {
1858        compiledPattern = compile(pattern);
1859    } catch (Exception JavaDoc e) {
1860        throw new InvalidObjectException JavaDoc("invalid pattern");
1861    }
1862
1863    if (serialVersionOnStream < 1) {
1864        // didn't have defaultCenturyStart field
1865
initializeDefaultCentury();
1866    }
1867    else {
1868        // fill in dependent transient field
1869
parseAmbiguousDatesAsAfter(defaultCenturyStart);
1870    }
1871    serialVersionOnStream = currentSerialVersion;
1872
1873    // If the deserialized object has a SimpleTimeZone, try
1874
// to replace it with a ZoneInfo equivalent in order to
1875
// be compatible with the SimpleTimeZone-based
1876
// implementation as much as possible.
1877
TimeZone JavaDoc tz = getTimeZone();
1878    if (tz instanceof SimpleTimeZone JavaDoc) {
1879        String JavaDoc id = tz.getID();
1880        TimeZone JavaDoc zi = TimeZone.getTimeZone(id);
1881        if (zi != null && zi.hasSameRules(tz) && zi.getID().equals(id)) {
1882        setTimeZone(zi);
1883        }
1884    }
1885    }
1886}
1887
Popular Tags