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