1 /* Copyright (C) 1998, 1999 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 final StringBuffer format (Object obj,
66 StringBuffer buf, FieldPosition pos)
68 if (obj instanceof Number)
69 return format (new Date(((Number) obj).longValue()), buf, pos);
70 return format ((Date) obj, buf, pos);
73 public final String format (Date date)
75 StringBuffer sb = new StringBuffer ();
76 format (date, sb, new FieldPosition (MONTH_FIELD));
80 public abstract StringBuffer format (Date date,
81 StringBuffer buf, FieldPosition pos);
83 public static Locale[] getAvailableLocales ()
88 public Calendar getCalendar ()
93 private static final DateFormat computeInstance (int style, Locale loc,
97 return computeInstance (style, style, loc, use_date, use_time);
100 private static final DateFormat computeInstance (int dateStyle,
109 res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
111 catch (MissingResourceException x)
116 String pattern = null;
123 name = "fullDateFormat";
124 def = "EEEE MMMM d, yyyy G";
127 name = "longDateFormat";
128 def = "MMMM d, yyyy";
131 name = "mediumDateFormat";
135 name = "shortDateFormat";
139 throw new IllegalArgumentException ();
143 pattern = res == null ? def : res.getString(name);
145 catch (MissingResourceException x)
162 name = "fullTimeFormat";
163 def = "h:mm:ss;S 'o''clock' a z";
166 name = "longTimeFormat";
170 name = "mediumTimeFormat";
174 name = "shortTimeFormat";
178 throw new IllegalArgumentException ();
184 s = res == null ? def : res.getString(name);
186 catch (MissingResourceException x)
193 return new SimpleDateFormat (pattern, loc);
196 public static final DateFormat getDateInstance ()
198 return getDateInstance (DEFAULT, Locale.getDefault());
201 public static final DateFormat getDateInstance (int style)
203 return getDateInstance (style, Locale.getDefault());
206 public static final DateFormat getDateInstance (int style, Locale loc)
208 return computeInstance (style, loc, true, false);
211 public static final DateFormat getDateTimeInstance ()
213 return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
216 public static final DateFormat getDateTimeInstance (int style)
218 return getDateTimeInstance (style, style, Locale.getDefault());
221 public static final DateFormat getDateTimeInstance (int dateStyle,
224 return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
227 public static final DateFormat getDateTimeInstance (int dateStyle,
231 return computeInstance (dateStyle, timeStyle, loc, true, true);
234 public static final DateFormat getInstance ()
236 // JCL book says SHORT.
237 return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
240 public NumberFormat getNumberFormat ()
245 public static final DateFormat getTimeInstance ()
247 return getTimeInstance (DEFAULT, Locale.getDefault());
250 public static final DateFormat getTimeInstance (int style)
252 return getTimeInstance (style, Locale.getDefault());
255 public static final DateFormat getTimeInstance (int style, Locale loc)
257 return computeInstance (style, loc, false, true);
260 public TimeZone getTimeZone ()
262 return calendar.getTimeZone();
265 public int hashCode ()
267 int hash = calendar.hashCode();
268 if (numberFormat != null)
269 hash ^= numberFormat.hashCode();
273 public boolean isLenient ()
275 return calendar.isLenient();
278 public Date parse (String source) throws ParseException
280 ParsePosition pos = new ParsePosition(0);
281 Date result = parse (source, pos);
284 int index = pos.getErrorIndex();
286 index = pos.getIndex();
287 throw new ParseException("invalid Date syntax", index);
292 public abstract Date parse (String source, ParsePosition pos);
294 public Object parseObject (String source, ParsePosition pos)
296 return parse(source, pos);
299 public void setCalendar (Calendar calendar)
301 this.calendar = calendar;
304 public void setLenient (boolean lenient)
306 calendar.setLenient(lenient);
309 public void setNumberFormat (NumberFormat numberFormat)
311 this.numberFormat = numberFormat;
314 public void setTimeZone (TimeZone timeZone)
316 calendar.setTimeZone(timeZone);