Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / java / text / DateFormat.java
blob73aa62d9805301678df41627108ab512843d2413
1 /* DateFormat.java -- Class for formatting/parsing date/times
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.text;
42 import java.io.InvalidObjectException;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.Locale;
46 import java.util.MissingResourceException;
47 import java.util.ResourceBundle;
48 import java.util.TimeZone;
50 /**
51 * @author Per Bothner (bothner@cygnus.com)
52 * @date October 25, 1998.
54 /* Written using "Java Class Libraries", 2nd edition, plus online
55 * API docs for JDK 1.2 beta from http://www.javasoft.com.
56 * Status: Mostly complete; search for FIXME to see omissions.
59 public abstract class DateFormat extends Format implements Cloneable
61 private static final long serialVersionUID = 7218322306649953788L;
63 // Names fixed by serialization spec.
64 protected Calendar calendar;
65 protected NumberFormat numberFormat;
67 // (Values determined using a test program.)
68 public static final int FULL = 0;
69 public static final int LONG = 1;
70 public static final int MEDIUM = 2;
71 public static final int SHORT = 3;
72 public static final int DEFAULT = MEDIUM;
74 /* These constants need to have these exact values. They
75 * correspond to index positions within the localPatternChars
76 * string for a given locale. Each locale may specify its
77 * own character for a particular field, but the position
78 * of these characters must correspond to an appropriate field
79 * number (as listed below), in order for their meaning to
80 * be determined. For example, the US locale uses
81 * the string "GyMdkHmsSEDFwWahKzYeugAZ", where 'G' is the character
82 * for era, 'y' for year, and so on down to 'Z' for time zone.
84 /**
85 * Represents the position of the era
86 * pattern character in the array of
87 * localized pattern characters.
88 * For example, 'AD' is an era used
89 * in the Gregorian calendar system.
90 * In the U.S. locale, this is 'G'.
91 */
92 public static final int ERA_FIELD = 0;
93 /**
94 * Represents the position of the year
95 * pattern character in the array of
96 * localized pattern characters.
97 * In the U.S. locale, this is 'y'.
99 public static final int YEAR_FIELD = 1;
101 * Represents the position of the month
102 * pattern character in the array of
103 * localized pattern characters.
104 * In the U.S. locale, this is 'M'.
106 public static final int MONTH_FIELD = 2;
108 * Represents the position of the date
109 * or day of the month pattern character
110 * in the array of localized pattern
111 * characters. In the U.S. locale,
112 * this is 'd'.
114 public static final int DATE_FIELD = 3;
116 * Represents the position of the 24
117 * hour pattern character in the array of
118 * localized pattern characters.
119 * In the U.S. locale, this is 'k'.
120 * This field numbers hours from 1 to 24.
122 public static final int HOUR_OF_DAY1_FIELD = 4;
124 * Represents the position of the 24
125 * hour pattern character in the array of
126 * localized pattern characters.
127 * In the U.S. locale, this is 'H'.
128 * This field numbers hours from 0 to 23.
130 public static final int HOUR_OF_DAY0_FIELD = 5;
132 * Represents the position of the minute
133 * pattern character in the array of
134 * localized pattern characters.
135 * In the U.S. locale, this is 'm'.
137 public static final int MINUTE_FIELD = 6;
139 * Represents the position of the second
140 * pattern character in the array of
141 * localized pattern characters.
142 * In the U.S. locale, this is 's'.
144 public static final int SECOND_FIELD = 7;
146 * Represents the position of the millisecond
147 * pattern character in the array of
148 * localized pattern characters.
149 * In the U.S. locale, this is 'S'.
151 public static final int MILLISECOND_FIELD = 8;
153 * Represents the position of the day of the
154 * week pattern character in the array of
155 * localized pattern characters.
156 * In the U.S. locale, this is 'E'.
158 public static final int DAY_OF_WEEK_FIELD = 9;
160 * Represents the position of the day of the
161 * year pattern character in the array of
162 * localized pattern characters.
163 * In the U.S. locale, this is 'D'.
165 public static final int DAY_OF_YEAR_FIELD = 10;
167 * Represents the position of the day of the
168 * week in the month pattern character in the
169 * array of localized pattern characters.
170 * In the U.S. locale, this is 'F'.
172 public static final int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
174 * Represents the position of the week of the
175 * year pattern character in the array of
176 * localized pattern characters.
177 * In the U.S. locale, this is 'w'.
179 public static final int WEEK_OF_YEAR_FIELD = 12;
181 * Represents the position of the week of the
182 * month pattern character in the array of
183 * localized pattern characters.
184 * In the U.S. locale, this is 'W'.
186 public static final int WEEK_OF_MONTH_FIELD = 13;
188 * Represents the position of the am/pm
189 * pattern character in the array of
190 * localized pattern characters.
191 * In the U.S. locale, this is 'a'.
193 public static final int AM_PM_FIELD = 14;
195 * Represents the position of the 12
196 * hour pattern character in the array of
197 * localized pattern characters.
198 * In the U.S. locale, this is 'h'.
199 * This field numbers hours from 1 to 12.
201 public static final int HOUR1_FIELD = 15;
203 * Represents the position of the 12
204 * hour pattern character in the array of
205 * localized pattern characters.
206 * In the U.S. locale, this is 'K'.
207 * This field numbers hours from 0 to 11.
209 public static final int HOUR0_FIELD = 16;
211 * Represents the position of the generic
212 * timezone pattern character in the array of
213 * localized pattern characters.
214 * In the U.S. locale, this is 'z'.
216 public static final int TIMEZONE_FIELD = 17;
218 * Represents the position of the ISO year
219 * pattern character in the array of
220 * localized pattern characters.
221 * In the U.S. locale, this is 'Y'.
222 * This is a GNU extension in accordance with
223 * the CLDR data used. This value may
224 * differ from the normal year value.
226 public static final int ISO_YEAR_FIELD = 18;
228 * Represents the position of the localized
229 * day of the week pattern character in the
230 * array of localized pattern characters.
231 * In the U.S. locale, this is 'e'.
232 * This is a GNU extension in accordance with
233 * the CLDR data used. This value only
234 * differs from the day of the week with
235 * numeric formatting, in which case the
236 * locale's first day of the week is used.
238 public static final int LOCALIZED_DAY_OF_WEEK_FIELD = 19;
240 * Represents the position of the extended year
241 * pattern character in the array of
242 * localized pattern characters.
243 * In the U.S. locale, this is 'u'.
244 * This is a GNU extension in accordance with
245 * the CLDR data used. This value modifies
246 * the year value, so as to incorporate the era.
247 * For example, in the Gregorian calendar system,
248 * the extended year is negative instead of being
249 * marked as BC.
251 public static final int EXTENDED_YEAR_FIELD = 20;
253 * Represents the position of the modified Julian
254 * day pattern character in the array of
255 * localized pattern characters.
256 * In the U.S. locale, this is 'g'.
257 * This is a GNU extension in accordance with
258 * the CLDR data used. This value differs
259 * from the standard Julian day in that days
260 * are marked from midnight onwards rather than
261 * noon, and the local time zone affects the value.
262 * In simple terms, it can be thought of as all
263 * the date fields represented as a single number.
265 public static final int MODIFIED_JULIAN_DAY_FIELD = 21;
267 * Represents the position of the millisecond
268 * in the day pattern character in the array of
269 * localized pattern characters.
270 * In the U.S. locale, this is 'A'.
271 * This is a GNU extension in accordance with
272 * the CLDR data used. This value represents
273 * all the time fields (excluding the time zone)
274 * numerically, giving the number of milliseconds
275 * into the day (e.g. 10 in the morning would
276 * be 10 * 60 * 60 * 1000). Any daylight savings
277 * offset also affects this value.
279 public static final int MILLISECOND_IN_DAY_FIELD = 22;
281 * Represents the position of the RFC822
282 * timezone pattern character in the array of
283 * localized pattern characters.
284 * In the U.S. locale, this is 'Z'.
285 * This is a GNU extension in accordance with
286 * the CLDR data used. The value is the offset
287 * of the current time from GMT e.g. -0500 would
288 * be five hours prior to GMT.
290 public static final int RFC822_TIMEZONE_FIELD = 23;
292 public static class Field extends Format.Field
294 static final long serialVersionUID = 7441350119349544720L;
296 private int calendarField;
298 public static final DateFormat.Field ERA
299 = new Field("era", Calendar.ERA);
300 public static final DateFormat.Field YEAR
301 = new Field("year", Calendar.YEAR);
302 public static final DateFormat.Field MONTH
303 = new Field("month", Calendar.MONTH);
304 public static final DateFormat.Field DAY_OF_MONTH
305 = new Field("day of month", Calendar.DAY_OF_MONTH);
306 public static final DateFormat.Field HOUR_OF_DAY1
307 = new Field("hour of day 1", Calendar.HOUR_OF_DAY);
308 public static final DateFormat.Field HOUR_OF_DAY0
309 = new Field("hour of day 0", Calendar.HOUR_OF_DAY);
310 public static final DateFormat.Field MINUTE
311 = new Field("minute", Calendar.MINUTE);
312 public static final DateFormat.Field SECOND
313 = new Field("second", Calendar.SECOND);
314 public static final DateFormat.Field MILLISECOND
315 = new Field("millisecond", Calendar.MILLISECOND);
316 public static final DateFormat.Field DAY_OF_WEEK
317 = new Field("day of week", Calendar.DAY_OF_WEEK);
318 public static final DateFormat.Field DAY_OF_YEAR
319 = new Field("day of year", Calendar.DAY_OF_YEAR);
320 public static final DateFormat.Field DAY_OF_WEEK_IN_MONTH
321 = new Field("day of week in month", Calendar.DAY_OF_WEEK_IN_MONTH);
322 public static final DateFormat.Field WEEK_OF_YEAR
323 = new Field("week of year", Calendar.WEEK_OF_YEAR);
324 public static final DateFormat.Field WEEK_OF_MONTH
325 = new Field("week of month", Calendar.WEEK_OF_MONTH);
326 public static final DateFormat.Field AM_PM
327 = new Field("am/pm", Calendar.AM_PM);
328 public static final DateFormat.Field HOUR1
329 = new Field("hour1", Calendar.HOUR);
330 public static final DateFormat.Field HOUR0
331 = new Field("hour0", Calendar.HOUR);
332 public static final DateFormat.Field TIME_ZONE
333 = new Field("timezone", Calendar.ZONE_OFFSET);
334 public static final DateFormat.Field ISO_YEAR
335 = new Field("iso year", Calendar.YEAR);
336 public static final DateFormat.Field LOCALIZED_DAY_OF_WEEK
337 = new Field("localized day of week", Calendar.DAY_OF_WEEK);
338 public static final DateFormat.Field EXTENDED_YEAR
339 = new Field("extended year", Calendar.YEAR);
340 public static final DateFormat.Field MODIFIED_JULIAN_DAY
341 = new Field("julian day", -1);
342 public static final DateFormat.Field MILLISECOND_IN_DAY
343 = new Field("millisecond in day", -1);
344 public static final DateFormat.Field RFC822_TIME_ZONE
345 = new Field("rfc822 timezone", Calendar.ZONE_OFFSET);
347 static final DateFormat.Field[] allFields =
349 ERA, YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY1,
350 HOUR_OF_DAY0, MINUTE, SECOND, MILLISECOND,
351 DAY_OF_WEEK, DAY_OF_YEAR, DAY_OF_WEEK_IN_MONTH,
352 WEEK_OF_YEAR, WEEK_OF_MONTH, AM_PM, HOUR1, HOUR0,
353 TIME_ZONE, ISO_YEAR, LOCALIZED_DAY_OF_WEEK,
354 EXTENDED_YEAR, MODIFIED_JULIAN_DAY, MILLISECOND_IN_DAY,
355 RFC822_TIME_ZONE
358 // For deserialization
359 private Field()
361 super("");
364 protected Field(String name, int calendarField)
366 super(name);
367 this.calendarField = calendarField;
370 public int getCalendarField()
372 return calendarField;
375 public static Field ofCalendarField(int calendarField)
377 if (calendarField >= allFields.length || calendarField < 0)
378 throw new IllegalArgumentException("no such calendar field ("
379 + calendarField + ")");
381 return allFields[calendarField];
384 protected Object readResolve() throws InvalidObjectException
386 String s = getName();
388 for (int i=0;i<allFields.length;i++)
389 if (s.equals(allFields[i].getName()))
390 return allFields[i];
392 throw new InvalidObjectException("no such DateFormat field called " + s);
397 * This method initializes a new instance of <code>DateFormat</code>.
399 protected DateFormat ()
404 * This method tests this object for equality against the specified object.
405 * The two objects will be considered equal if an only if the specified
406 * object:
407 * <P>
408 * <ul>
409 * <li>Is not <code>null</code>.</li>
410 * <li>Is an instance of <code>DateFormat</code>.</li>
411 * <li>Has equal numberFormat field as this object.</li>
412 * <li>Has equal (Calendar) TimeZone rules as this object.</li>
413 * <li>Has equal (Calendar) isLenient results.</li>
414 * <li>Has equal Calendar first day of week and minimal days in week
415 * values.</li>
416 * </ul>
417 * Note that not all properties of the Calendar are relevant for a
418 * DateFormat. For formatting only the fact whether or not the
419 * TimeZone has the same rules and whether the calendar is lenient
420 * and has the same week rules is compared for this implementation
421 * of equals. Other properties of the Calendar (such as the time)
422 * are not taken into account.
424 * @param obj The object to test for equality against.
426 * @return <code>true</code> if the specified object is equal to this object,
427 * <code>false</code> otherwise.
429 public boolean equals (Object obj)
431 if (!(obj instanceof DateFormat))
432 return false;
434 DateFormat d = (DateFormat) obj;
435 TimeZone tz = getTimeZone();
436 TimeZone tzd = d.getTimeZone();
437 if (tz.hasSameRules(tzd))
438 if (isLenient() == d.isLenient())
440 Calendar c = getCalendar();
441 Calendar cd = d.getCalendar();
442 if ((c == null && cd == null)
444 (c.getFirstDayOfWeek() == cd.getFirstDayOfWeek()
446 c.getMinimalDaysInFirstWeek()
447 == cd.getMinimalDaysInFirstWeek()))
448 return ((numberFormat == null && d.numberFormat == null)
449 || numberFormat.equals(d.numberFormat));
452 return false;
456 * This method returns a copy of this object.
458 * @return A copy of this object.
460 public Object clone ()
462 // We know the superclass just call's Object's generic cloner.
463 return super.clone ();
467 * This method formats the specified <code>Object</code> into a date string
468 * and appends it to the specified <code>StringBuffer</code>.
469 * The specified object must be an instance of <code>Number</code> or
470 * <code>Date</code> or an <code>IllegalArgumentException</code> will be
471 * thrown.
473 * @param obj The <code>Object</code> to format.
474 * @param buf The <code>StringBuffer</code> to append the resultant
475 * <code>String</code> to.
476 * @param pos Is updated to the start and end index of the
477 * specified field.
479 * @return The <code>StringBuffer</code> supplied on input, with the
480 * formatted date/time appended.
482 public final StringBuffer format (Object obj,
483 StringBuffer buf, FieldPosition pos)
485 if (obj instanceof Number)
486 obj = new Date(((Number) obj).longValue());
487 else if (! (obj instanceof Date))
488 throw new IllegalArgumentException
489 ("Cannot format given Object as a Date");
491 return format ((Date) obj, buf, pos);
494 /**
495 * Formats the date argument according to the pattern specified.
497 * @param date The formatted date.
499 public final String format (Date date)
501 StringBuffer sb = new StringBuffer ();
502 format (date, sb, new FieldPosition (MONTH_FIELD));
503 return sb.toString();
507 * This method formats a <code>Date</code> into a string and appends it
508 * to the specified <code>StringBuffer</code>.
510 * @param date The <code>Date</code> value to format.
511 * @param buf The <code>StringBuffer</code> to append the resultant
512 * <code>String</code> to.
513 * @param pos Is updated to the start and end index of the
514 * specified field.
516 * @return The <code>StringBuffer</code> supplied on input, with the
517 * formatted date/time appended.
519 public abstract StringBuffer format (Date date,
520 StringBuffer buf, FieldPosition pos);
523 * This method returns a list of available locales supported by this
524 * class.
526 public static Locale[] getAvailableLocales()
528 return Locale.getAvailableLocales();
532 * This method returns the <code>Calendar</code> object being used by
533 * this object to parse/format datetimes.
535 * @return The <code>Calendar</code> being used by this object.
537 * @see java.util.Calendar
539 public Calendar getCalendar ()
541 return calendar;
544 private static DateFormat computeInstance (int style, Locale loc,
545 boolean use_date, boolean use_time)
547 return computeInstance (style, style, loc, use_date, use_time);
550 private static DateFormat computeInstance (int dateStyle, int timeStyle,
551 Locale loc, boolean use_date,
552 boolean use_time)
554 ResourceBundle res;
557 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
558 loc, ClassLoader.getSystemClassLoader());
560 catch (MissingResourceException x)
562 res = null;
565 String pattern = null;
566 if (use_date)
568 String name, def;
569 switch (dateStyle)
571 case FULL:
572 name = "fullDateFormat";
573 def = "EEEE MMMM d, yyyy G";
574 break;
575 case LONG:
576 name = "longDateFormat";
577 def = "MMMM d, yyyy";
578 break;
579 case MEDIUM:
580 name = "mediumDateFormat";
581 def = "d-MMM-yy";
582 break;
583 case SHORT:
584 name = "shortDateFormat";
585 def = "M/d/yy";
586 break;
587 default:
588 throw new IllegalArgumentException ();
592 pattern = res == null ? def : res.getString(name);
594 catch (MissingResourceException x)
596 pattern = def;
600 if (use_time)
602 if (pattern == null)
603 pattern = "";
604 else
605 pattern += " ";
607 String name, def;
608 switch (timeStyle)
610 case FULL:
611 name = "fullTimeFormat";
612 def = "h:mm:ss;S 'o''clock' a z";
613 break;
614 case LONG:
615 name = "longTimeFormat";
616 def = "h:mm:ss a z";
617 break;
618 case MEDIUM:
619 name = "mediumTimeFormat";
620 def = "h:mm:ss a";
621 break;
622 case SHORT:
623 name = "shortTimeFormat";
624 def = "h:mm a";
625 break;
626 default:
627 throw new IllegalArgumentException ();
630 String s;
633 s = res == null ? def : res.getString(name);
635 catch (MissingResourceException x)
637 s = def;
639 pattern += s;
642 return new SimpleDateFormat (pattern, loc);
646 * This method returns an instance of <code>DateFormat</code> that will
647 * format using the default formatting style for dates.
649 * @return A new <code>DateFormat</code> instance.
651 public static final DateFormat getDateInstance ()
653 return getDateInstance (DEFAULT, Locale.getDefault());
657 * This method returns an instance of <code>DateFormat</code> that will
658 * format using the specified formatting style for dates.
660 * @param style The type of formatting to perform.
662 * @return A new <code>DateFormat</code> instance.
664 public static final DateFormat getDateInstance (int style)
666 return getDateInstance (style, Locale.getDefault());
670 * This method returns an instance of <code>DateFormat</code> that will
671 * format using the specified formatting style for dates. The specified
672 * localed will be used in place of the default.
674 * @param style The type of formatting to perform.
675 * @param loc The desired locale.
677 * @return A new <code>DateFormat</code> instance.
679 public static final DateFormat getDateInstance (int style, Locale loc)
681 return computeInstance (style, loc, true, false);
685 * This method returns a new instance of <code>DateFormat</code> that
686 * formats both dates and times using the <code>SHORT</code> style.
688 * @return A new <code>DateFormat</code>instance.
690 public static final DateFormat getDateTimeInstance ()
692 return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
696 * This method returns a new instance of <code>DateFormat</code> that
697 * formats both dates and times using the <code>DEFAULT</code> style.
699 * @return A new <code>DateFormat</code>instance.
701 public static final DateFormat getDateTimeInstance (int dateStyle,
702 int timeStyle)
704 return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
708 * This method returns a new instance of <code>DateFormat</code> that
709 * formats both dates and times using the specified styles.
711 * @param dateStyle The desired style for date formatting.
712 * @param timeStyle The desired style for time formatting
714 * @return A new <code>DateFormat</code>instance.
716 public static final DateFormat getDateTimeInstance (int dateStyle,
717 int timeStyle,
718 Locale loc)
720 return computeInstance (dateStyle, timeStyle, loc, true, true);
724 * This method returns a new instance of <code>DateFormat</code> that
725 * formats both dates and times using the <code>SHORT</code> style.
727 * @return A new <code>DateFormat</code>instance.
729 public static final DateFormat getInstance ()
731 // JCL book says SHORT.
732 return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
736 * This method returns the <code>NumberFormat</code> object being used
737 * by this object to parse/format time values.
739 * @return The <code>NumberFormat</code> in use by this object.
741 public NumberFormat getNumberFormat ()
743 return numberFormat;
747 * This method returns an instance of <code>DateFormat</code> that will
748 * format using the default formatting style for times.
750 * @return A new <code>DateFormat</code> instance.
752 public static final DateFormat getTimeInstance ()
754 return getTimeInstance (DEFAULT, Locale.getDefault());
758 * This method returns an instance of <code>DateFormat</code> that will
759 * format using the specified formatting style for times.
761 * @param style The type of formatting to perform.
763 * @return A new <code>DateFormat</code> instance.
765 public static final DateFormat getTimeInstance (int style)
767 return getTimeInstance (style, Locale.getDefault());
771 * This method returns an instance of <code>DateFormat</code> that will
772 * format using the specified formatting style for times. The specified
773 * localed will be used in place of the default.
775 * @param style The type of formatting to perform.
776 * @param loc The desired locale.
778 * @return A new <code>DateFormat</code> instance.
780 public static final DateFormat getTimeInstance (int style, Locale loc)
782 return computeInstance (style, loc, false, true);
786 * This method returns the <code>TimeZone</code> object being used by
787 * this instance.
789 * @return The time zone in use.
791 public TimeZone getTimeZone ()
793 return calendar.getTimeZone();
797 * This method returns a hash value for this object.
799 * @return A hash value for this object.
801 public int hashCode ()
803 if (numberFormat != null)
804 return numberFormat.hashCode();
805 else
806 return 0;
810 * This method indicates whether or not the parsing of date and time
811 * values should be done in a lenient value.
813 * @return <code>true</code> if date/time parsing is lenient,
814 * <code>false</code> otherwise.
816 public boolean isLenient ()
818 return calendar.isLenient();
822 * This method parses the specified date/time string.
824 * @param source The string to parse.
825 * @return The resultant date.
827 * @exception ParseException If the specified string cannot be parsed.
829 public Date parse (String source) throws ParseException
831 ParsePosition pos = new ParsePosition(0);
832 Date result = parse (source, pos);
833 if (result == null)
835 int index = pos.getErrorIndex();
836 if (index < 0)
837 index = pos.getIndex();
838 throw new ParseException("invalid Date syntax in \""
839 + source + '\"', index);
841 return result;
844 /**
845 * This method parses the specified <code>String</code> into a
846 * <code>Date</code>. The <code>pos</code> argument contains the
847 * starting parse position on method entry and the ending parse
848 * position on method exit.
850 * @param source The string to parse.
851 * @param pos The starting parse position in entry, the ending parse
852 * position on exit.
854 * @return The parsed date, or <code>null</code> if the string cannot
855 * be parsed.
857 public abstract Date parse (String source, ParsePosition pos);
860 * This method is identical to <code>parse(String, ParsePosition)</code>,
861 * but returns its result as an <code>Object</code> instead of a
862 * <code>Date</code>.
864 * @param source The string to parse.
865 * @param pos The starting parse position in entry, the ending parse
866 * position on exit.
868 * @return The parsed date, or <code>null</code> if the string cannot
869 * be parsed.
871 public Object parseObject (String source, ParsePosition pos)
873 return parse(source, pos);
877 * This method specified the <code>Calendar</code> that should be used
878 * by this object to parse/format datetimes.
880 * @param calendar The new <code>Calendar</code> for this object.
882 * @see java.util.Calendar
884 public void setCalendar (Calendar calendar)
886 this.calendar = calendar;
890 * This method specifies whether or not this object should be lenient in
891 * the syntax it accepts while parsing date/time values.
893 * @param lenient <code>true</code> if parsing should be lenient,
894 * <code>false</code> otherwise.
896 public void setLenient (boolean lenient)
898 calendar.setLenient(lenient);
902 * This method specifies the <code>NumberFormat</code> object that should
903 * be used by this object to parse/format times.
905 * @param numberFormat The <code>NumberFormat</code> in use by this object.
907 public void setNumberFormat (NumberFormat numberFormat)
909 this.numberFormat = numberFormat;
913 * This method sets the time zone that should be used by this object.
915 * @param timeZone The new time zone.
917 public void setTimeZone (TimeZone timeZone)
919 calendar.setTimeZone(timeZone);