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.get