OSDN Git Service

e7bbe0cf734bd9d14565805a94f195f79c97a3c8
[pf3gnuchains/gcc-fork.git] / libjava / java / text / DateFormat.java
1 /* Copyright (C) 1998, 1999  Red Hat, Inc.
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.text;
10
11 import java.util.*;
12
13 /**
14  * @author Per Bothner <bothner@cygnus.com>
15  * @date October 25, 1998.
16  */
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.
20  */
21
22 public abstract class DateFormat extends Format implements Cloneable
23 {
24   protected Calendar calendar;
25   protected NumberFormat numberFormat;
26
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;
33
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;
52
53   protected DateFormat ()
54   {
55   }
56
57   public boolean equals (Object obj)
58   {
59     if (! (obj instanceof DateFormat))
60       return false;
61     DateFormat d = (DateFormat) obj;
62     return calendar.equals(d.calendar) && numberFormat.equals(d.numberFormat);
63   }
64
65   public final StringBuffer format (Object obj,
66                                     StringBuffer buf, FieldPosition pos)
67   {
68     if (obj instanceof Number)
69       return format (new Date(((Number) obj).longValue()), buf, pos);
70     return format ((Date) obj, buf, pos);
71   }
72
73   public final String format (Date date)
74   {
75     StringBuffer sb = new StringBuffer ();
76     format (date, sb, new FieldPosition (MONTH_FIELD));
77     return sb.toString();
78   }
79
80   public abstract StringBuffer format (Date date,
81                                        StringBuffer buf, FieldPosition pos);
82
83   public static Locale[] getAvailableLocales ()
84   {
85     return null;                // FIXME
86   }
87
88   public Calendar getCalendar ()
89   {
90     return calendar;
91   }
92
93   private static final DateFormat computeInstance (int style, Locale loc,
94                                                    boolean use_date,
95                                                    boolean use_time)
96   {
97     return computeInstance (style, style, loc, use_date, use_time);
98   }
99
100   private static final DateFormat computeInstance (int dateStyle, 
101                                                    int timeStyle,
102                                                    Locale loc,
103                                                    boolean use_date,
104                                                    boolean use_time)
105   {
106     ResourceBundle res;
107     try
108       {
109         res = ResourceBundle.getBundle("gnu.gcj.text.LocaleData", loc);
110       }
111     catch (MissingResourceException x)
112       {
113         res = null;
114       }
115
116     String pattern = null;
117     if (use_date)
118       {
119         String name, def;
120         switch (dateStyle)
121           {
122           case FULL:
123             name = "fullDateFormat";
124             def = "EEEE MMMM d, yyyy G";
125             break;
126           case LONG:
127             name = "longDateFormat";
128             def = "MMMM d, yyyy";
129             break;
130           case MEDIUM:
131             name = "mediumDateFormat";
132             def = "d-MMM-yy";
133             break;
134           case SHORT:
135             name = "shortDateFormat";
136             def = "M/d/yy";
137             break;
138           default:
139             throw new IllegalArgumentException ();
140           }
141         try
142           {
143             pattern = res == null ? def : res.getString(name);
144           }
145         catch (MissingResourceException x)
146           {
147             pattern = def;
148           }
149       }
150
151     if (use_time)
152       {
153         if (pattern == null)
154           pattern = "";
155         else
156           pattern += " ";
157
158         String name, def;
159         switch (timeStyle)
160           {
161           case FULL:
162             name = "fullTimeFormat";
163             def = "h:mm:ss;S 'o''clock' a z";
164             break;
165           case LONG:
166             name = "longTimeFormat";
167             def = "h:mm:ss a z";
168             break;
169           case MEDIUM:
170             name = "mediumTimeFormat";
171             def = "h:mm:ss a";
172             break;
173           case SHORT:
174             name = "shortTimeFormat";
175             def = "h:mm a";
176             break;
177           default:
178             throw new IllegalArgumentException ();
179           }
180
181         String s;
182         try
183           {
184             s = res == null ? def : res.getString(name);
185           }
186         catch (MissingResourceException x)
187           {
188             s = def;
189           }
190         pattern += s;
191       }
192
193     return new SimpleDateFormat (pattern, loc);
194   }
195
196   public static final DateFormat getDateInstance ()
197   {
198     return getDateInstance (DEFAULT, Locale.getDefault());
199   }
200
201   public static final DateFormat getDateInstance (int style)
202   {
203     return getDateInstance (style, Locale.getDefault());
204   }
205
206   public static final DateFormat getDateInstance (int style, Locale loc)
207   {
208     return computeInstance (style, loc, true, false);
209   }
210
211   public static final DateFormat getDateTimeInstance ()
212   {
213     return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
214   }
215
216   public static final DateFormat getDateTimeInstance (int style)
217   {
218     return getDateTimeInstance (style, style, Locale.getDefault());
219   }
220
221   public static final DateFormat getDateTimeInstance (int dateStyle, 
222                                                       int timeStyle)
223   {
224     return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
225   }
226
227   public static final DateFormat getDateTimeInstance (int dateStyle, 
228                                                       int timeStyle, 
229                                                       Locale loc)
230   {
231     return computeInstance (dateStyle, timeStyle, loc, true, true);
232   }
233
234   public static final DateFormat getInstance ()
235   {
236     // JCL book says SHORT.
237     return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
238   }
239
240   public NumberFormat getNumberFormat ()
241   {
242     return numberFormat;
243   }
244
245   public static final DateFormat getTimeInstance ()
246   {
247     return getTimeInstance (DEFAULT, Locale.getDefault());
248   }
249
250   public static final DateFormat getTimeInstance (int style)
251   {
252     return getTimeInstance (style, Locale.getDefault());
253   }
254
255   public static final DateFormat getTimeInstance (int style, Locale loc)
256   {
257     return computeInstance (style, loc, false, true);
258   }
259
260   public TimeZone getTimeZone ()
261   {
262     return calendar.getTimeZone();
263   }
264
265   public int hashCode ()
266   {
267     int hash = calendar.hashCode();
268     if (numberFormat != null)
269       hash ^= numberFormat.hashCode();
270     return hash;
271   }
272
273   public boolean isLenient ()
274   {
275     return calendar.isLenient();
276   }
277
278   public Date parse (String source) throws ParseException
279   {
280     ParsePosition pos = new ParsePosition(0);
281     Date result = parse (source, pos);
282     if (result == null)
283       {
284         int index = pos.getErrorIndex();
285         if (index < 0)
286           index = pos.getIndex();
287         throw new ParseException("invalid Date syntax", index);
288       }
289     return result;
290   }
291
292   public abstract Date parse (String source, ParsePosition pos);
293
294   public Object parseObject (String source, ParsePosition pos)
295   {
296     return parse(source, pos);
297   }
298
299   public void setCalendar (Calendar calendar)
300   {
301     this.calendar = calendar;
302   }
303
304   public void setLenient (boolean lenient)
305   {
306     calendar.setLenient(lenient);
307   }
308
309   public void setNumberFormat (NumberFormat numberFormat)
310   {
311     this.numberFormat = numberFormat;
312   }
313
314   public void setTimeZone (TimeZone timeZone)
315   {
316     calendar.setTimeZone(timeZone);
317   }
318 }