source: trunk/gcc/libjava/java/text/DateFormat.java

Last change on this file was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 16.6 KB
Line 
1/* DateFormat.java -- Class for formatting/parsing date/times
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.text;
40
41import java.util.*;
42
43/**
44 * @author Per Bothner <bothner@cygnus.com>
45 * @date October 25, 1998.
46 */
47/* Written using "Java Class Libraries", 2nd edition, plus online
48 * API docs for JDK 1.2 beta from http://www.javasoft.com.
49 * Status: Mostly complete; search for FIXME to see omissions.
50 */
51
52public abstract class DateFormat extends Format implements Cloneable
53{
54 protected Calendar calendar;
55 protected NumberFormat numberFormat;
56
57 // (Values determined using a test program.)
58 public static final int FULL = 0;
59 public static final int LONG = 1;
60 public static final int MEDIUM = 2;
61 public static final int SHORT = 3;
62 public static final int DEFAULT = MEDIUM;
63
64 /* These constants need to have these exact values. They
65 * correspond to index positions within the localPatternChars
66 * string for a given locale. For example, the US locale uses
67 * the string "GyMdkHmsSEDFwWahKz", where 'G' is the character
68 * for era, 'y' for year, and so on down to 'z' for time zone.
69 */
70 public static final int ERA_FIELD = 0;
71 public static final int YEAR_FIELD = 1;
72 public static final int MONTH_FIELD = 2;
73 public static final int DATE_FIELD = 3;
74 public static final int HOUR_OF_DAY1_FIELD = 4;
75 public static final int HOUR_OF_DAY0_FIELD = 5;
76 public static final int MINUTE_FIELD = 6;
77 public static final int SECOND_FIELD = 7;
78 public static final int MILLISECOND_FIELD = 8;
79 public static final int DAY_OF_WEEK_FIELD = 9;
80 public static final int DAY_OF_YEAR_FIELD = 10;
81 public static final int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
82 public static final int WEEK_OF_YEAR_FIELD = 12;
83 public static final int WEEK_OF_MONTH_FIELD = 13;
84 public static final int AM_PM_FIELD = 14;
85 public static final int HOUR1_FIELD = 15;
86 public static final int HOUR0_FIELD = 16;
87 public static final int TIMEZONE_FIELD = 17;
88
89 /**
90 * This method initializes a new instance of <code>DateFormat</code>.
91 */
92 protected DateFormat ()
93 {
94 }
95
96 /**
97 * This method tests this object for equality against the specified object.
98 * The two objects will be considered equal if an only if the specified
99 * object:
100 * <P>
101 * <ul>
102 * <li>Is not <code>null</code>.
103 * <li>Is an instance of <code>DateFormat</code>.
104 * <li>Has the same calendar and numberFormat field values as this object.
105 * </ul>
106 *
107 * @param obj The object to test for equality against.
108 *
109 * @return <code>true</code> if the specified object is equal to this object,
110 * <code>false</code> otherwise.
111 */
112 public boolean equals (Object obj)
113 {
114 if (! (obj instanceof DateFormat))
115 return false;
116 DateFormat d = (DateFormat) obj;
117 return calendar.equals(d.calendar) && numberFormat.equals(d.numberFormat);
118 }
119
120 /**
121 * This method returns a copy of this object.
122 *
123 * @return A copy of this object.
124 */
125 public Object clone ()
126 {
127 // We know the superclass just call's Object's generic cloner.
128 return super.clone ();
129 }
130
131 /**
132 * This method formats the specified <code>Object</code> into a date string
133 * and appends it to the specified <code>StringBuffer</code>.
134 * The specified object must be an instance of <code>Number</code> or
135 * <code>Date</code> or an <code>IllegalArgumentException</code> will be
136 * thrown.
137 *
138 * @param obj The <code>Object</code> to format.
139 * @param toAppendTo The <code>StringBuffer</code> to append the resultant
140 * <code>String</code> to.
141 * @param fieldPosition Is updated to the start and end index of the
142 * specified field.
143 *
144 * @return The <code>StringBuffer</code> supplied on input, with the
145 * formatted date/time appended.
146 */
147 public final StringBuffer format (Object obj,
148 StringBuffer buf, FieldPosition pos)
149 {
150 if (obj instanceof Number)
151 obj = new Date(((Number) obj).longValue());
152 return format ((Date) obj, buf, pos);
153 }
154
155 /**
156 * Formats the date argument according to the pattern specified.
157 *
158 * @param date The formatted date.
159 */
160 public final String format (Date date)
161 {
162 StringBuffer sb = new StringBuffer ();
163 format (date, sb, new FieldPosition (MONTH_FIELD));
164 return sb.toString();
165 }
166
167 /**
168 * This method formats a <code>Date</code> into a string and appends it
169 * to the specified <code>StringBuffer</code>.
170 *
171 * @param date The <code>Date</code> value to format.
172 * @param toAppendTo The <code>StringBuffer</code> to append the resultant
173 * <code>String</code> to.
174 * @param fieldPosition Is updated to the start and end index of the
175 * specified field.
176 *
177 * @return The <code>StringBuffer</code> supplied on input, with the
178 * formatted date/time appended.
179 */
180 public abstract StringBuffer format (Date date,
181 StringBuffer buf, FieldPosition pos);
182
183 /**
184 * This method returns a list of available locales supported by this
185 * class.
186 */
187 public static Locale[] getAvailableLocales ()
188 {
189 // FIXME
190 Locale[] l = new Locale[1];
191 l[0] = Locale.US;
192 return l;
193 }
194
195 /**
196 * This method returns the <code>Calendar</code> object being used by
197 * this object to parse/format datetimes.
198 *
199 * @return The <code>Calendar</code> being used by this object.
200 *
201 * @see java.util.Calendar
202 */
203 public Calendar getCalendar ()
204 {
205 return calendar;
206 }
207
208 private static final DateFormat computeInstance (int style, Locale loc,
209 boolean use_date,
210 boolean use_time)
211 {
212 return computeInstance (style, style, loc, use_date, use_time);
213 }
214
215 private static final DateFormat computeInstance (int dateStyle,
216 int timeStyle,
217 Locale loc,
218 boolean use_date,
219 boolean use_time)
220 {
221 ResourceBundle res;
222 try
223 {
224 res = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
225 loc);
226 }
227 catch (MissingResourceException x)
228 {
229 res = null;
230 }
231
232 String pattern = null;
233 if (use_date)
234 {
235 String name, def;
236 switch (dateStyle)
237 {
238 case FULL:
239 name = "fullDateFormat";
240 def = "EEEE MMMM d, yyyy G";
241 break;
242 case LONG:
243 name = "longDateFormat";
244 def = "MMMM d, yyyy";
245 break;
246 case MEDIUM:
247 name = "mediumDateFormat";
248 def = "d-MMM-yy";
249 break;
250 case SHORT:
251 name = "shortDateFormat";
252 def = "M/d/yy";
253 break;
254 default:
255 throw new IllegalArgumentException ();
256 }
257 try
258 {
259 pattern = res == null ? def : res.getString(name);
260 }
261 catch (MissingResourceException x)
262 {
263 pattern = def;
264 }
265 }
266
267 if (use_time)
268 {
269 if (pattern == null)
270 pattern = "";
271 else
272 pattern += " ";
273
274 String name, def;
275 switch (timeStyle)
276 {
277 case FULL:
278 name = "fullTimeFormat";
279 def = "h:mm:ss;S 'o''clock' a z";
280 break;
281 case LONG:
282 name = "longTimeFormat";
283 def = "h:mm:ss a z";
284 break;
285 case MEDIUM:
286 name = "mediumTimeFormat";
287 def = "h:mm:ss a";
288 break;
289 case SHORT:
290 name = "shortTimeFormat";
291 def = "h:mm a";
292 break;
293 default:
294 throw new IllegalArgumentException ();
295 }
296
297 String s;
298 try
299 {
300 s = res == null ? def : res.getString(name);
301 }
302 catch (MissingResourceException x)
303 {
304 s = def;
305 }
306 pattern += s;
307 }
308
309 return new SimpleDateFormat (pattern, loc);
310 }
311
312 /**
313 * This method returns an instance of <code>DateFormat</code> that will
314 * format using the default formatting style for dates.
315 *
316 * @return A new <code>DateFormat</code> instance.
317 */
318 public static final DateFormat getDateInstance ()
319 {
320 return getDateInstance (DEFAULT, Locale.getDefault());
321 }
322
323 /**
324 * This method returns an instance of <code>DateFormat</code> that will
325 * format using the specified formatting style for dates.
326 *
327 * @param style The type of formatting to perform.
328 *
329 * @return A new <code>DateFormat</code> instance.
330 */
331 public static final DateFormat getDateInstance (int style)
332 {
333 return getDateInstance (style, Locale.getDefault());
334 }
335
336 /**
337 * This method returns an instance of <code>DateFormat</code> that will
338 * format using the specified formatting style for dates. The specified
339 * localed will be used in place of the default.
340 *
341 * @param style The type of formatting to perform.
342 * @param aLocale The desired locale.
343 *
344 * @return A new <code>DateFormat</code> instance.
345 */
346 public static final DateFormat getDateInstance (int style, Locale loc)
347 {
348 return computeInstance (style, loc, true, false);
349 }
350
351 /**
352 * This method returns a new instance of <code>DateFormat</code> that
353 * formats both dates and times using the <code>SHORT</code> style.
354 *
355 * @return A new <code>DateFormat</code>instance.
356 */
357 public static final DateFormat getDateTimeInstance ()
358 {
359 return getDateTimeInstance (DEFAULT, DEFAULT, Locale.getDefault());
360 }
361
362 /**
363 * This method returns a new instance of <code>DateFormat</code> that
364 * formats both dates and times using the <code>DEFAULT</code> style.
365 *
366 * @return A new <code>DateFormat</code>instance.
367 */
368 public static final DateFormat getDateTimeInstance (int dateStyle,
369 int timeStyle)
370 {
371 return getDateTimeInstance (dateStyle, timeStyle, Locale.getDefault());
372 }
373
374 /**
375 * This method returns a new instance of <code>DateFormat</code> that
376 * formats both dates and times using the specified styles.
377 *
378 * @param dateStyle The desired style for date formatting.
379 * @param timeStyle The desired style for time formatting
380 *
381 * @return A new <code>DateFormat</code>instance.
382 */
383 public static final DateFormat getDateTimeInstance (int dateStyle,
384 int timeStyle,
385 Locale loc)
386 {
387 return computeInstance (dateStyle, timeStyle, loc, true, true);
388 }
389
390 /**
391 * This method returns a new instance of <code>DateFormat</code> that
392 * formats both dates and times using the <code>SHORT</code> style.
393 *
394 * @return A new <code>DateFormat</code>instance.
395 */
396 public static final DateFormat getInstance ()
397 {
398 // JCL book says SHORT.
399 return getDateTimeInstance (SHORT, SHORT, Locale.getDefault());
400 }
401
402 /**
403 * This method returns the <code>NumberFormat</code> object being used
404 * by this object to parse/format time values.
405 *
406 * @return The <code>NumberFormat</code> in use by this object.
407 */
408 public NumberFormat getNumberFormat ()
409 {
410 return numberFormat;
411 }
412
413 /**
414 * This method returns an instance of <code>DateFormat</code> that will
415 * format using the default formatting style for times.
416 *
417 * @return A new <code>DateFormat</code> instance.
418 */
419 public static final DateFormat getTimeInstance ()
420 {
421 return getTimeInstance (DEFAULT, Locale.getDefault());
422 }
423
424 /**
425 * This method returns an instance of <code>DateFormat</code> that will
426 * format using the specified formatting style for times.
427 *
428 * @param style The type of formatting to perform.
429 *
430 * @return A new <code>DateFormat</code> instance.
431 */
432 public static final DateFormat getTimeInstance (int style)
433 {
434 return getTimeInstance (style, Locale.getDefault());
435 }
436
437 /**
438 * This method returns an instance of <code>DateFormat</code> that will
439 * format using the specified formatting style for times. The specified
440 * localed will be used in place of the default.
441 *
442 * @param style The type of formatting to perform.
443 * @param aLocale The desired locale.
444 *
445 * @return A new <code>DateFormat</code> instance.
446 */
447 public static final DateFormat getTimeInstance (int style, Locale loc)
448 {
449 return computeInstance (style, loc, false, true);
450 }
451
452 /**
453 * This method returns the <code>TimeZone</code> object being used by
454 * this instance.
455 *
456 * @return The time zone in use.
457 */
458 public TimeZone getTimeZone ()
459 {
460 return calendar.getTimeZone();
461 }
462
463 /**
464 * This method returns a hash value for this object.
465 *
466 * @return A hash value for this object.
467 */
468 public int hashCode ()
469 {
470 int hash = calendar.hashCode();
471 if (numberFormat != null)
472 hash ^= numberFormat.hashCode();
473 return hash;
474 }
475
476 /**
477 * This method indicates whether or not the parsing of date and time
478 * values should be done in a lenient value.
479 *
480 * @return <code>true</code> if date/time parsing is lenient,
481 * <code>false</code> otherwise.
482 */
483 public boolean isLenient ()
484 {
485 return calendar.isLenient();
486 }
487
488 /**
489 * This method parses the specified date/time string.
490 *
491 * @return The resultant date.
492 *
493 * @exception ParseException If the specified string cannot be parsed.
494 */
495 public Date parse (String source) throws ParseException
496 {
497 ParsePosition pos = new ParsePosition(0);
498 Date result = parse (source, pos);
499 if (result == null)
500 {
501 int index = pos.getErrorIndex();
502 if (index < 0)
503 index = pos.getIndex();
504 throw new ParseException("invalid Date syntax", index);
505 }
506 return result;
507 }
508
509 /**
510 * This method parses the specified <code>String</code> into a
511 * <code>Date</code>. The <code>pos</code> argument contains the
512 * starting parse position on method entry and the ending parse
513 * position on method exit.
514 *
515 * @param text The string to parse.
516 * @param pos The starting parse position in entry, the ending parse
517 * position on exit.
518 *
519 * @return The parsed date, or <code>null</code> if the string cannot
520 * be parsed.
521 */
522 public abstract Date parse (String source, ParsePosition pos);
523
524 /**
525 * This method is identical to <code>parse(String, ParsePosition)</code>,
526 * but returns its result as an <code>Object</code> instead of a
527 * <code>Date</code>.
528 *
529 * @param source The string to parse.
530 * @param pos The starting parse position in entry, the ending parse
531 * position on exit.
532 *
533 * @return The parsed date, or <code>null</code> if the string cannot
534 * be parsed.
535 */
536 public Object parseObject (String source, ParsePosition pos)
537 {
538 return parse(source, pos);
539 }
540
541 /**
542 * This method specified the <code>Calendar</code> that should be used
543 * by this object to parse/format datetimes.
544 *
545 * @param The new <code>Calendar</code> for this object.
546 *
547 * @see java.util.Calendar
548 */
549 public void setCalendar (Calendar calendar)
550 {
551 this.calendar = calendar;
552 }
553
554 /**
555 * This method specifies whether or not this object should be lenient in
556 * the syntax it accepts while parsing date/time values.
557 *
558 * @param lenient <code>true</code> if parsing should be lenient,
559 * <code>false</code> otherwise.
560 */
561 public void setLenient (boolean lenient)
562 {
563 calendar.setLenient(lenient);
564 }
565
566 /**
567 * This method specifies the <code>NumberFormat</code> object that should
568 * be used by this object to parse/format times.
569 *
570 * @param The <code>NumberFormat</code> in use by this object.
571 */
572 public void setNumberFormat (NumberFormat numberFormat)
573 {
574 this.numberFormat = numberFormat;
575 }
576
577 /**
578 * This method sets the time zone that should be used by this object.
579 *
580 * @param The new time zone.
581 */
582 public void setTimeZone (TimeZone timeZone)
583 {
584 calendar.setTimeZone(timeZone);
585 }
586}
Note: See TracBrowser for help on using the repository browser.