1 /* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Per Bothner <bothner@cygnus.com>
15 * @date October 25, 1998.
17 /* Written using "Java Class Libraries", 2nd edition, plus online
18 * API docs for JDK 1.2 beta from http://www.javasoft.com.
19 * Status: Mostly complete; search for FIXME to see omissions.
22 public abstract class DateFormat
extends Format
implements Cloneable
24 protected Calendar calendar
;
25 protected NumberFormat numberFormat
;
27 // (Values determined using a test program.)
28 public final static int FULL
= 0;
29 public final static int LONG
= 1;
30 public final static int MEDIUM
= 2;
31 public final static int SHORT
= 3;
32 public final static int DEFAULT
= MEDIUM
;
34 public final static int ERA_FIELD
= 0;
35 public final static int YEAR_FIELD
= 1;
36 public final static int MONTH_FIELD
= 2;
37 public final static int DATE_FIELD
= 3;
38 public final static int HOUR_OF_DAY1_FIELD
= 4;
39 public final static int HOUR_OF_DAY0_FIELD
= 5;
40 public final static int MINUTE_FIELD
= 6;
41 public final static int SECOND_FIELD
= 7;
42 public final static int MILLISECOND_FIELD
= 8;
43 public final static int DAY_OF_WEEK_FIELD
= 9;
44 public final static int DAY_OF_YEAR_FIELD
= 10;
45 public final static int DAY_OF_WEEK_IN_MONTH_FIELD
= 11;
46 public final static int WEEK_OF_YEAR_FIELD
= 12;
47 public final static int WEEK_OF_MONTH_FIELD
= 13;
48 public final static int AM_PM_FIELD
= 14;
49 public final static int HOUR1_FIELD
= 15;
50 public final static int HOUR0_FIELD
= 16;
51 public final static int TIMEZONE_FIELD
= 17;
53 protected DateFormat ()
57 public boolean equals (Object obj
)
59 if (! (obj
instanceof DateFormat
))
61 DateFormat d
= (DateFormat
) obj
;
62 return calendar
.equals(d
.calendar
) && numberFormat
.equals(d
.numberFormat
);
65 public Object
clone ()
67 // We know the superclass just call's Object's generic cloner.
68 return super.clone ();
71 public final StringBuffer
format (Object obj
,
72 StringBuffer buf
, FieldPosition pos
)
74 if (obj
instanceof Number
)
75 return format (new Date(((Number
) obj
).longValue()), buf
, pos
);
76 return format ((Date
) obj
, buf
, pos
);
79 public final String
format (Date date
)
81 StringBuffer sb
= new StringBuffer ();
82 format (date
, sb
, new FieldPosition (MONTH_FIELD
));
86 public abstract StringBuffer
format (Date date
,
87 StringBuffer buf
, FieldPosition pos
);
89 public static Locale
[] getAvailableLocales ()
94 public Calendar
getCalendar ()
99 private static final DateFormat
computeInstance (int style
, Locale loc
,
103 return computeInstance (style
, style
, loc
, use_date
, use_time
);
106 private static final DateFormat
computeInstance (int dateStyle
,
115 res
= ResourceBundle
.getBundle("gnu.gcj.text.LocaleData", loc
);
117 catch (MissingResourceException x
)
122 String pattern
= null;
129 name
= "fullDateFormat";
130 def
= "EEEE MMMM d, yyyy G";
133 name
= "longDateFormat";
134 def
= "MMMM d, yyyy";
137 name
= "mediumDateFormat";
141 name
= "shortDateFormat";
145 throw new IllegalArgumentException ();
149 pattern
= res
== null ? def
: res
.getString(name
);
151 catch (MissingResourceException x
)
168 name
= "fullTimeFormat";
169 def
= "h:mm:ss;S 'o''clock' a z";
172 name
= "longTimeFormat";
176 name
= "mediumTimeFormat";
180 name
= "shortTimeFormat";
184 throw new IllegalArgumentException ();
190 s
= res
== null ? def
: res
.getString(name
);
192 catch (MissingResourceException x
)
199 return new SimpleDateFormat (pattern
, loc
);
202 public static final DateFormat
getDateInstance ()
204 return getDateInstance (DEFAULT
, Locale
.getDefault());
207 public static final DateFormat
getDateInstance (int style
)
209 return getDateInstance (style
, Locale
.getDefault());
212 public static final DateFormat
getDateInstance (int style
, Locale loc
)
214 return computeInstance (style
, loc
, true, false);
217 public static final DateFormat
getDateTimeInstance ()
219 return getDateTimeInstance (DEFAULT
, DEFAULT
, Locale
.getDefault());
222 public static final DateFormat
getDateTimeInstance (int style
)
224 return getDateTimeInstance (style
, style
, Locale
.getDefault());
227 public static final DateFormat
getDateTimeInstance (int dateStyle
,
230 return getDateTimeInstance (dateStyle
, timeStyle
, Locale
.getDefault());
233 public static final DateFormat
getDateTimeInstance (int dateStyle
,
237 return computeInstance (dateStyle
, timeStyle
, loc
, true, true);
240 public static final DateFormat
getInstance ()
242 // JCL book says SHORT.
243 return getDateTimeInstance (SHORT
, SHORT
, Locale
.getDefault());
246 public NumberFormat
getNumberFormat ()
251 public static final DateFormat
getTimeInstance ()
253 return getTimeInstance (DEFAULT
, Locale
.getDefault());
256 public static final DateFormat
getTimeInstance (int style
)
258 return getTimeInstance (style
, Locale
.getDefault());
261 public static final DateFormat
getTimeInstance (int style
, Locale loc
)
263 return computeInstance (style
, loc
, false, true);
266 public TimeZone
getTimeZone ()
268 return calendar
.getTimeZone();
271 public int hashCode ()
273 int hash
= calendar
.hashCode();
274 if (numberFormat
!= null)
275 hash ^
= numberFormat
.hashCode();
279 public boolean isLenient ()
281 return calendar
.isLenient();
284 public Date
parse (String source
) throws ParseException
286 ParsePosition pos
= new ParsePosition(0);
287 Date result
= parse (source
, pos
);
290 int index
= pos
.getErrorIndex();
292 index
= pos
.getIndex();
293 throw new ParseException("invalid Date syntax", index
);
298 public abstract Date
parse (String source
, ParsePosition pos
);
300 public Object
parseObject (String source
, ParsePosition pos
)
302 return parse(source
, pos
);
305 public void setCalendar (Calendar calendar
)
307 this.calendar
= calendar
;
310 public void setLenient (boolean lenient
)
312 calendar
.setLenient(lenient
);
315 public void setNumberFormat (NumberFormat numberFormat
)
317 this.numberFormat
= numberFormat
;
320 public void setTimeZone (TimeZone timeZone
)
322 calendar
.setTimeZone(timeZone
);