]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/eclipse/plugins/com.ibm.icu.base/src/com/ibm/icu/text/DateFormat.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / eclipse / plugins / com.ibm.icu.base / src / com / ibm / icu / text / DateFormat.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2008, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.text;\r
9 \r
10 import java.text.FieldPosition;\r
11 import java.text.Format;\r
12 import java.text.ParseException;\r
13 import java.text.ParsePosition;\r
14 import java.util.Date;\r
15 import java.util.Locale;\r
16 \r
17 import com.ibm.icu.util.Calendar;\r
18 import com.ibm.icu.util.TimeZone;\r
19 import com.ibm.icu.util.ULocale;\r
20 \r
21 /**\r
22  * DateFormat is an abstract class for date/time formatting subclasses which\r
23  * formats and parses dates or time in a language-independent manner.\r
24  * The date/time formatting subclass, such as SimpleDateFormat, allows for\r
25  * formatting (i.e., date -> text), parsing (text -> date), and\r
26  * normalization.  The date is represented as a <code>Date</code> object or\r
27  * as the milliseconds since January 1, 1970, 00:00:00 GMT.\r
28  *\r
29  * <p>DateFormat provides many class methods for obtaining default date/time\r
30  * formatters based on the default or a given loacle and a number of formatting\r
31  * styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More\r
32  * detail and examples of using these styles are provided in the method\r
33  * descriptions.\r
34  *\r
35  * <p>DateFormat helps you to format and parse dates for any locale.\r
36  * Your code can be completely independent of the locale conventions for\r
37  * months, days of the week, or even the calendar format: lunar vs. solar.\r
38  *\r
39  * <p>To format a date for the current Locale, use one of the\r
40  * static factory methods:\r
41  * <pre>\r
42  *  myString = DateFormat.getDateInstance().format(myDate);\r
43  * </pre>\r
44  * <p>If you are formatting multiple numbers, it is\r
45  * more efficient to get the format and use it multiple times so that\r
46  * the system doesn't have to fetch the information about the local\r
47  * language and country conventions multiple times.\r
48  * <pre>\r
49  *  DateFormat df = DateFormat.getDateInstance();\r
50  *  for (int i = 0; i < a.length; ++i) {\r
51  *    output.println(df.format(myDate[i]) + "; ");\r
52  *  }\r
53  * </pre>\r
54  * <p>To format a number for a different Locale, specify it in the\r
55  * call to getDateInstance().\r
56  * <pre>\r
57  *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);\r
58  * </pre>\r
59  * <p>You can use a DateFormat to parse also.\r
60  * <pre>\r
61  *  myDate = df.parse(myString);\r
62  * </pre>\r
63  * <p>Use getDateInstance to get the normal date format for that country.\r
64  * There are other static factory methods available.\r
65  * Use getTimeInstance to get the time format for that country.\r
66  * Use getDateTimeInstance to get a date and time format. You can pass in \r
67  * different options to these factory methods to control the length of the\r
68  * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends\r
69  * on the locale, but generally:\r
70  * <ul><li>SHORT is completely numeric, such as 12.13.52 or 3:30pm\r
71  * <li>MEDIUM is longer, such as Jan 12, 1952\r
72  * <li>LONG is longer, such as January 12, 1952 or 3:30:32pm\r
73  * <li>FULL is pretty completely specified, such as\r
74  * Tuesday, April 12, 1952 AD or 3:30:42pm PST.\r
75  * </ul>\r
76  *\r
77  * <p>You can also set the time zone on the format if you wish.\r
78  * If you want even more control over the format or parsing,\r
79  * (or want to give your users more control),\r
80  * you can try casting the DateFormat you get from the factory methods\r
81  * to a SimpleDateFormat. This will work for the majority\r
82  * of countries; just remember to put it in a try block in case you\r
83  * encounter an unusual one.\r
84  *\r
85  * <p>You can also use forms of the parse and format methods with\r
86  * ParsePosition and FieldPosition to\r
87  * allow you to\r
88  * <ul><li>progressively parse through pieces of a string.\r
89  * <li>align any particular field, or find out where it is for selection\r
90  * on the screen.\r
91  * </ul>\r
92  *\r
93  * <h4>Synchronization</h4>\r
94  *\r
95  * Date formats are not synchronized. It is recommended to create separate \r
96  * format instances for each thread. If multiple threads access a format \r
97  * concurrently, it must be synchronized externally. \r
98  *\r
99  * @see          UFormat\r
100  * @see          NumberFormat\r
101  * @see          SimpleDateFormat\r
102  * @see          com.ibm.icu.util.Calendar\r
103  * @see          com.ibm.icu.util.GregorianCalendar\r
104  * @see          com.ibm.icu.util.TimeZone\r
105  * @author       Mark Davis, Chen-Lieh Huang, Alan Liu\r
106  * @stable ICU 2.0\r
107  */\r
108 public class DateFormat extends Format {\r
109     private static final long serialVersionUID = 1;\r
110 \r
111     /**\r
112      * @internal\r
113      */\r
114     public final java.text.DateFormat dateFormat;\r
115         \r
116     /**\r
117      * @internal\r
118      * @param delegate the DateFormat to which to delegate\r
119      */\r
120     public DateFormat(java.text.DateFormat delegate) {\r
121         this.dateFormat = delegate;\r
122     }\r
123     \r
124     /**\r
125      * For subclass use.  Subclasses will generally not\r
126      * work correctly unless they manipulate the delegate.\r
127      */\r
128     protected DateFormat() {\r
129         this.dateFormat = java.text.DateFormat.getInstance();\r
130     }\r
131 \r
132     /**\r
133      * FieldPosition selector for 'G' field alignment,\r
134      * corresponding to the {@link Calendar#ERA} field.\r
135      * @stable ICU 2.0\r
136      */\r
137     public final static int ERA_FIELD = 0;\r
138 \r
139     /**\r
140      * FieldPosition selector for 'y' field alignment,\r
141      * corresponding to the {@link Calendar#YEAR} field.\r
142      * @stable ICU 2.0\r
143      */\r
144     public final static int YEAR_FIELD = 1;\r
145 \r
146     /**\r
147      * FieldPosition selector for 'M' field alignment,\r
148      * corresponding to the {@link Calendar#MONTH} field.\r
149      * @stable ICU 2.0\r
150      */\r
151     public final static int MONTH_FIELD = 2;\r
152 \r
153     /**\r
154      * FieldPosition selector for 'd' field alignment,\r
155      * corresponding to the {@link Calendar#DATE} field.\r
156      * @stable ICU 2.0\r
157      */\r
158     public final static int DATE_FIELD = 3;\r
159 \r
160     /**\r
161      * FieldPosition selector for 'k' field alignment,\r
162      * corresponding to the {@link Calendar#HOUR_OF_DAY} field.\r
163      * HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.\r
164      * For example, 23:59 + 01:00 results in 24:59.\r
165      * @stable ICU 2.0\r
166      */\r
167     public final static int HOUR_OF_DAY1_FIELD = 4;\r
168 \r
169     /**\r
170      * FieldPosition selector for 'H' field alignment,\r
171      * corresponding to the {@link Calendar#HOUR_OF_DAY} field.\r
172      * HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.\r
173      * For example, 23:59 + 01:00 results in 00:59.\r
174      * @stable ICU 2.0\r
175      */\r
176     public final static int HOUR_OF_DAY0_FIELD = 5;\r
177 \r
178     /**\r
179      * FieldPosition selector for 'm' field alignment,\r
180      * corresponding to the {@link Calendar#MINUTE} field.\r
181      * @stable ICU 2.0\r
182      */\r
183     public final static int MINUTE_FIELD = 6;\r
184 \r
185     /**\r
186      * FieldPosition selector for 's' field alignment,\r
187      * corresponding to the {@link Calendar#SECOND} field.\r
188      * @stable ICU 2.0\r
189      */\r
190     public final static int SECOND_FIELD = 7;\r
191 \r
192     /**\r
193      * FieldPosition selector for 'S' field alignment,\r
194      * corresponding to the {@link Calendar#MILLISECOND} field.\r
195      * @stable ICU 3.0\r
196      */\r
197     public final static int FRACTIONAL_SECOND_FIELD = 8;\r
198 \r
199     /**\r
200      * Alias for FRACTIONAL_SECOND_FIELD.\r
201      * @stable ICU 3.4.3\r
202      */\r
203     public final static int MILLISECOND_FIELD = FRACTIONAL_SECOND_FIELD;\r
204 \r
205     /**\r
206      * FieldPosition selector for 'E' field alignment,\r
207      * corresponding to the {@link Calendar#DAY_OF_WEEK} field.\r
208      * @stable ICU 2.0\r
209      */\r
210     public final static int DAY_OF_WEEK_FIELD = 9;\r
211 \r
212     /**\r
213      * FieldPosition selector for 'D' field alignment,\r
214      * corresponding to the {@link Calendar#DAY_OF_YEAR} field.\r
215      * @stable ICU 2.0\r
216      */\r
217     public final static int DAY_OF_YEAR_FIELD = 10;\r
218 \r
219     /**\r
220      * FieldPosition selector for 'F' field alignment,\r
221      * corresponding to the {@link Calendar#DAY_OF_WEEK_IN_MONTH} field.\r
222      * @stable ICU 2.0\r
223      */\r
224     public final static int DAY_OF_WEEK_IN_MONTH_FIELD = 11;\r
225 \r
226     /**\r
227      * FieldPosition selector for 'w' field alignment,\r
228      * corresponding to the {@link Calendar#WEEK_OF_YEAR} field.\r
229      * @stable ICU 2.0\r
230      */\r
231     public final static int WEEK_OF_YEAR_FIELD = 12;\r
232 \r
233     /**\r
234      * FieldPosition selector for 'W' field alignment,\r
235      * corresponding to the {@link Calendar#WEEK_OF_MONTH} field.\r
236      * @stable ICU 2.0\r
237      */\r
238     public final static int WEEK_OF_MONTH_FIELD = 13;\r
239 \r
240     /**\r
241      * FieldPosition selector for 'a' field alignment,\r
242      * corresponding to the {@link Calendar#AM_PM} field.\r
243      * @stable ICU 2.0\r
244      */\r
245     public final static int AM_PM_FIELD = 14;\r
246 \r
247     /**\r
248      * FieldPosition selector for 'h' field alignment,\r
249      * corresponding to the {@link Calendar#HOUR} field.\r
250      * HOUR1_FIELD is used for the one-based 12-hour clock.\r
251      * For example, 11:30 PM + 1 hour results in 12:30 AM.\r
252      * @stable ICU 2.0\r
253      */\r
254     public final static int HOUR1_FIELD = 15;\r
255 \r
256     /**\r
257      * FieldPosition selector for 'K' field alignment,\r
258      * corresponding to the {@link Calendar#HOUR} field.\r
259      * HOUR0_FIELD is used for the zero-based 12-hour clock.\r
260      * For example, 11:30 PM + 1 hour results in 00:30 AM.\r
261      * @stable ICU 2.0\r
262      */\r
263     public final static int HOUR0_FIELD = 16;\r
264 \r
265     /**\r
266      * FieldPosition selector for 'z' field alignment,\r
267      * corresponding to the {@link Calendar#ZONE_OFFSET} and\r
268      * {@link Calendar#DST_OFFSET} fields.\r
269      * @stable ICU 2.0\r
270      */\r
271     public final static int TIMEZONE_FIELD = 17;\r
272 \r
273     /**\r
274      * Overrides Format.\r
275      * Formats a time object into a time string. Examples of time objects\r
276      * are a time value expressed in milliseconds and a Date object.\r
277      * @param obj must be a Number or a Date or a Calendar.\r
278      * @param toAppendTo the string buffer for the returning time string.\r
279      * @return the formatted time string.\r
280      * @param fieldPosition keeps track of the position of the field\r
281      * within the returned string.\r
282      * On input: an alignment field,\r
283      * if desired. On output: the offsets of the alignment field. For\r
284      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",\r
285      * if the given fieldPosition is DateFormat.YEAR_FIELD, the\r
286      * begin index and end index of fieldPosition will be set to\r
287      * 0 and 4, respectively.\r
288      * Notice that if the same time field appears\r
289      * more than once in a pattern, the fieldPosition will be set for the first\r
290      * occurence of that time field. For instance, formatting a Date to\r
291      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern\r
292      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,\r
293      * the begin index and end index of fieldPosition will be set to\r
294      * 5 and 8, respectively, for the first occurence of the timezone\r
295      * pattern character 'z'.\r
296      * @see java.text.Format\r
297      * @stable ICU 2.0\r
298      */\r
299     public final StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition fieldPosition) {\r
300         if (obj instanceof Calendar) {\r
301             return format((Calendar)obj, toAppendTo, fieldPosition);\r
302         } else if (obj instanceof Date) {\r
303             return format((Date)obj, toAppendTo, fieldPosition);\r
304         } else if (obj instanceof Number) {\r
305             return format(new Date(((Number)obj).longValue()), toAppendTo, fieldPosition );\r
306         } else {\r
307             throw new IllegalArgumentException("Cannot format given Object as a Date");\r
308         }\r
309     }\r
310 \r
311     /**\r
312      * Formats a date into a date/time string.\r
313      * @param cal a Calendar set to the date and time to be formatted\r
314      * into a date/time string.\r
315      * @param toAppendTo the string buffer for the returning date/time string.\r
316      * @param fieldPosition keeps track of the position of the field\r
317      * within the returned string.\r
318      * On input: an alignment field,\r
319      * if desired. On output: the offsets of the alignment field. For\r
320      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",\r
321      * if the given fieldPosition is DateFormat.YEAR_FIELD, the\r
322      * begin index and end index of fieldPosition will be set to\r
323      * 0 and 4, respectively.\r
324      * Notice that if the same time field appears\r
325      * more than once in a pattern, the fieldPosition will be set for the first\r
326      * occurence of that time field. For instance, formatting a Date to\r
327      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern\r
328      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,\r
329      * the begin index and end index of fieldPosition will be set to\r
330      * 5 and 8, respectively, for the first occurence of the timezone\r
331      * pattern character 'z'.\r
332      * @return the formatted date/time string.\r
333      * @stable ICU 2.0\r
334      */\r
335     public StringBuffer format(Calendar cal, StringBuffer toAppendTo, FieldPosition fieldPosition) {\r
336         return format(cal.getTime(), toAppendTo, fieldPosition);\r
337     }\r
338     \r
339 \r
340     /**\r
341      * Formats a Date into a date/time string.\r
342      * @param date a Date to be formatted into a date/time string.\r
343      * @param toAppendTo the string buffer for the returning date/time string.\r
344      * @param fieldPosition keeps track of the position of the field\r
345      * within the returned string.\r
346      * On input: an alignment field,\r
347      * if desired. On output: the offsets of the alignment field. For\r
348      * example, given a time text "1996.07.10 AD at 15:08:56 PDT",\r
349      * if the given fieldPosition is DateFormat.YEAR_FIELD, the\r
350      * begin index and end index of fieldPosition will be set to\r
351      * 0 and 4, respectively.\r
352      * Notice that if the same time field appears\r
353      * more than once in a pattern, the fieldPosition will be set for the first\r
354      * occurence of that time field. For instance, formatting a Date to\r
355      * the time string "1 PM PDT (Pacific Daylight Time)" using the pattern\r
356      * "h a z (zzzz)" and the alignment field DateFormat.TIMEZONE_FIELD,\r
357      * the begin index and end index of fieldPosition will be set to\r
358      * 5 and 8, respectively, for the first occurence of the timezone\r
359      * pattern character 'z'.\r
360      * @return the formatted date/time string.\r
361      * @stable ICU 2.0\r
362      */\r
363     public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {\r
364         return dateFormat.format(date, toAppendTo, fieldPosition);\r
365     }\r
366 \r
367     /**\r
368      * Formats a Date into a date/time string.\r
369      * @param date the time value to be formatted into a time string.\r
370      * @return the formatted time string.\r
371      * @stable ICU 2.0\r
372      */\r
373     public final String format(Date date) {\r
374         return dateFormat.format(date);\r
375     }\r
376 \r
377     /**\r
378      * Parse a date/time string.\r
379      *\r
380      * @param text  The date/time string to be parsed\r
381      *\r
382      * @return      A Date, or null if the input could not be parsed\r
383      *\r
384      * @exception  ParseException  If the given string cannot be parsed as a date.\r
385      *\r
386      * @see #parse(String, ParsePosition)\r
387      * @stable ICU 2.0\r
388      */\r
389     public Date parse(String text) throws ParseException {\r
390         return dateFormat.parse(text);\r
391     }\r
392 \r
393     /**\r
394      * Parse a date/time string according to the given parse position.\r
395      * For example, a time text "07/10/96 4:5 PM, PDT" will be parsed\r
396      * into a Calendar that is equivalent to Date(837039928046).  The\r
397      * caller should clear the calendar before calling this method,\r
398      * unless existing field information is to be kept.\r
399      *\r
400      * <p> By default, parsing is lenient: If the input is not in the form used\r
401      * by this object's format method but can still be parsed as a date, then\r
402      * the parse succeeds.  Clients may insist on strict adherence to the\r
403      * format by calling setLenient(false).\r
404      *\r
405      * @see #setLenient(boolean)\r
406      *\r
407      * @param text  The date/time string to be parsed\r
408      *\r
409      * @param cal   The calendar into which parsed data will be stored.\r
410      *              In general, this should be cleared before calling this\r
411      *              method.  If this parse fails, the calendar may still\r
412      *              have been modified.\r
413      *\r
414      * @param pos   On input, the position at which to start parsing; on\r
415      *              output, the position at which parsing terminated, or the\r
416      *              start position if the parse failed.\r
417      * @stable ICU 2.0\r
418      */\r
419     public void parse(String text, Calendar cal, ParsePosition pos) {\r
420         Date result = dateFormat.parse(text, pos);\r
421         cal.setTime(result);\r
422     }\r
423 \r
424     /**\r
425      * Parse a date/time string according to the given parse position.  For\r
426      * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date\r
427      * that is equivalent to Date(837039928046).\r
428      *\r
429      * <p> By default, parsing is lenient: If the input is not in the form used\r
430      * by this object's format method but can still be parsed as a date, then\r
431      * the parse succeeds.  Clients may insist on strict adherence to the\r
432      * format by calling setLenient(false).\r
433      *\r
434      * @see #setLenient(boolean)\r
435      *\r
436      * @param text  The date/time string to be parsed\r
437      *\r
438      * @param pos   On input, the position at which to start parsing; on\r
439      *              output, the position at which parsing terminated, or the\r
440      *              start position if the parse failed.\r
441      *\r
442      * @return      A Date, or null if the input could not be parsed\r
443      * @stable ICU 2.0\r
444      */\r
445     public Date parse(String text, ParsePosition pos) {\r
446         return dateFormat.parse(text, pos);\r
447     }\r
448 \r
449     /**\r
450      * Parse a date/time string into an Object.  This convenience method simply\r
451      * calls parse(String, ParsePosition).\r
452      *\r
453      * @see #parse(String, ParsePosition)\r
454      * @stable ICU 2.0\r
455      */\r
456     public Object parseObject(String source, ParsePosition pos) {\r
457         return parse(source, pos);\r
458     }\r
459 \r
460     /**\r
461      * Constant for full style pattern.\r
462      * @stable ICU 2.0\r
463      */\r
464     public static final int FULL = 0;\r
465 \r
466     /**\r
467      * Constant for long style pattern.\r
468      * @stable ICU 2.0\r
469      */\r
470     public static final int LONG = 1;\r
471 \r
472     /**\r
473      * Constant for medium style pattern.\r
474      * @stable ICU 2.0\r
475      */\r
476     public static final int MEDIUM = 2;\r
477 \r
478     /**\r
479      * Constant for short style pattern.\r
480      * @stable ICU 2.0\r
481      */\r
482     public static final int SHORT = 3;\r
483 \r
484     /**\r
485      * Constant for default style pattern.  Its value is MEDIUM.\r
486      * @stable ICU 2.0\r
487      */\r
488     public static final int DEFAULT = MEDIUM;\r
489 \r
490     /**\r
491      * Gets the time formatter with the default formatting style\r
492      * for the default locale.\r
493      * @return a time formatter.\r
494      * @stable ICU 2.0\r
495      */\r
496     public final static DateFormat getTimeInstance() {\r
497         return new DateFormat(java.text.DateFormat.getTimeInstance());\r
498     }\r
499 \r
500     /**\r
501      * Gets the time formatter with the given formatting style\r
502      * for the default locale.\r
503      * @param style the given formatting style. For example,\r
504      * SHORT for "h:mm a" in the US locale.\r
505      * @return a time formatter.\r
506      * @stable ICU 2.0\r
507      */\r
508     public final static DateFormat getTimeInstance(int style) {\r
509         return new DateFormat(java.text.DateFormat.getTimeInstance(style));\r
510     }\r
511 \r
512     /**\r
513      * Gets the time formatter with the given formatting style\r
514      * for the given locale.\r
515      * @param style the given formatting style. For example,\r
516      * SHORT for "h:mm a" in the US locale.\r
517      * @param aLocale the given locale.\r
518      * @return a time formatter.\r
519      * @stable ICU 2.0\r
520      */\r
521     public final static DateFormat getTimeInstance(int style, Locale aLocale) {\r
522         return new DateFormat(java.text.DateFormat.getTimeInstance(style, aLocale));\r
523     }\r
524 \r
525     /**\r
526      * Gets the time formatter with the given formatting style\r
527      * for the given locale.\r
528      * @param style the given formatting style. For example,\r
529      * SHORT for "h:mm a" in the US locale.\r
530      * @param aLocale the given locale.\r
531      * @return a time formatter.\r
532      * @stable ICU 3.2\r
533      */\r
534     public final static DateFormat getTimeInstance(int style, ULocale aLocale) {\r
535         return new DateFormat(java.text.DateFormat.getTimeInstance(style, aLocale.toLocale()));\r
536     }\r
537 \r
538     /**\r
539      * Gets the date formatter with the default formatting style\r
540      * for the default locale.\r
541      * @return a date formatter.\r
542      * @stable ICU 2.0\r
543      */\r
544     public final static DateFormat getDateInstance() {\r
545         return new DateFormat(java.text.DateFormat.getDateInstance());\r
546     }\r
547 \r
548     /**\r
549      * Gets the date formatter with the given formatting style\r
550      * for the default locale.\r
551      * @param style the given formatting style. For example,\r
552      * SHORT for "M/d/yy" in the US locale.\r
553      * @return a date formatter.\r
554      * @stable ICU 2.0\r
555      */\r
556     public final static DateFormat getDateInstance(int style) {\r
557         return new DateFormat(java.text.DateFormat.getDateInstance(style));\r
558     }\r
559 \r
560     /**\r
561      * Gets the date formatter with the given formatting style\r
562      * for the given locale.\r
563      * @param style the given formatting style. For example,\r
564      * SHORT for "M/d/yy" in the US locale.\r
565      * @param aLocale the given locale.\r
566      * @return a date formatter.\r
567      * @stable ICU 2.0\r
568      */\r
569     public final static DateFormat getDateInstance(int style, Locale aLocale) {\r
570         return new DateFormat(java.text.DateFormat.getDateInstance(style, aLocale));\r
571     }\r
572 \r
573     /**\r
574      * Gets the date formatter with the given formatting style\r
575      * for the given locale.\r
576      * @param style the given formatting style. For example,\r
577      * SHORT for "M/d/yy" in the US locale.\r
578      * @param aLocale the given locale.\r
579      * @return a date formatter.\r
580      * @stable ICU 3.4.3\r
581      */\r
582     public final static DateFormat getDateInstance(int style, ULocale aLocale) {\r
583         return new DateFormat(java.text.DateFormat.getDateInstance(style, aLocale.toLocale()));\r
584     }\r
585 \r
586     /**\r
587      * Gets the date/time formatter with the default formatting style\r
588      * for the default locale.\r
589      * @return a date/time formatter.\r
590      * @stable ICU 2.0\r
591      */\r
592     public final static DateFormat getDateTimeInstance() {\r
593         return new DateFormat(java.text.DateFormat.getDateTimeInstance());\r
594     }\r
595 \r
596     /**\r
597      * Gets the date/time formatter with the given date and time\r
598      * formatting styles for the default locale.\r
599      * @param dateStyle the given date formatting style. For example,\r
600      * SHORT for "M/d/yy" in the US locale.\r
601      * @param timeStyle the given time formatting style. For example,\r
602      * SHORT for "h:mm a" in the US locale.\r
603      * @return a date/time formatter.\r
604      * @stable ICU 2.0\r
605      */\r
606     public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {\r
607         return new DateFormat(java.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle));\r
608     }\r
609 \r
610     /**\r
611      * Gets the date/time formatter with the given formatting styles\r
612      * for the given locale.\r
613      * @param dateStyle the given date formatting style.\r
614      * @param timeStyle the given time formatting style.\r
615      * @param aLocale the given locale.\r
616      * @return a date/time formatter.\r
617      * @stable ICU 2.0\r
618      */\r
619     public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale) {\r
620         return new DateFormat(java.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale));\r
621     }\r
622 \r
623     /**\r
624      * Gets the date/time formatter with the given formatting styles\r
625      * for the given locale.\r
626      * @param dateStyle the given date formatting style.\r
627      * @param timeStyle the given time formatting style.\r
628      * @param aLocale the given locale.\r
629      * @return a date/time formatter.\r
630      * @stable ICU 3.4.3\r
631      */\r
632     public final static DateFormat getDateTimeInstance(int dateStyle, int timeStyle, ULocale aLocale) {\r
633         return new DateFormat(java.text.DateFormat.getDateTimeInstance(dateStyle, timeStyle, aLocale.toLocale()));\r
634     }\r
635 \r
636     /**\r
637      * Get a default date/time formatter that uses the SHORT style for both the\r
638      * date and the time.\r
639      * @stable ICU 2.0\r
640      */\r
641     public final static DateFormat getInstance() {\r
642         return new DateFormat(java.text.DateFormat.getInstance());\r
643     }\r
644 \r
645     /**\r
646      * Gets the set of locales for which DateFormats are installed.\r
647      * @return the set of locales for which DateFormats are installed.\r
648      * @stable ICU 2.0\r
649      */\r
650     public static Locale[] getAvailableLocales() {\r
651         return java.text.DateFormat.getAvailableLocales();\r
652     }\r
653 \r
654     /**\r
655      * Gets the set of locales for which DateFormats are installed.\r
656      * @return the set of locales for which DateFormats are installed.\r
657      * @stable ICU 3.4.3\r
658      */\r
659     public static ULocale[] getAvailableULocales() {\r
660         Locale[] locales = java.text.DateFormat.getAvailableLocales();\r
661         ULocale[] ulocales = new ULocale[locales.length];\r
662         for (int i = 0; i < locales.length; ++i) {\r
663             ulocales[i] = ULocale.forLocale(locales[i]);\r
664         }\r
665         return ulocales;\r
666     }\r
667 \r
668     /**\r
669      * Set the calendar to be used by this date format.  Initially, the default\r
670      * calendar for the specified or default locale is used.\r
671      * @param newCalendar the new Calendar to be used by the date format\r
672      * @stable ICU 2.0\r
673      */\r
674     public void setCalendar(Calendar newCalendar) {\r
675         dateFormat.setCalendar(newCalendar.calendar);\r
676     }\r
677 \r
678     /**\r
679      * Gets the calendar associated with this date/time formatter.\r
680      * @return the calendar associated with this date/time formatter.\r
681      * @stable ICU 2.0\r
682      */\r
683     public Calendar getCalendar() {\r
684         return new Calendar(dateFormat.getCalendar());\r
685     }\r
686 \r
687     /**\r
688      * Allows you to set the number formatter.\r
689      * @param newNumberFormat the given new NumberFormat.\r
690      * @stable ICU 2.0\r
691      */\r
692     public void setNumberFormat(NumberFormat newNumberFormat) {\r
693         dateFormat.setNumberFormat(newNumberFormat.numberFormat);\r
694     }\r
695 \r
696     /**\r
697      * Gets the number formatter which this date/time formatter uses to\r
698      * format and parse a time.\r
699      * @return the number formatter which this date/time formatter uses.\r
700      * @stable ICU 2.0\r
701      */\r
702     public NumberFormat getNumberFormat() {\r
703         return new NumberFormat(dateFormat.getNumberFormat());\r
704     }\r
705 \r
706     /**\r
707      * Sets the time zone for the calendar of this DateFormat object.\r
708      * @param zone the given new time zone.\r
709      * @stable ICU 2.0\r
710      */\r
711     public void setTimeZone(TimeZone zone) {\r
712         dateFormat.setTimeZone(zone.timeZone);\r
713     }\r
714 \r
715     /**\r
716      * Gets the time zone.\r
717      * @return the time zone associated with the calendar of DateFormat.\r
718      * @stable ICU 2.0\r
719      */\r
720     public TimeZone getTimeZone() {\r
721         return new TimeZone(dateFormat.getTimeZone());\r
722     }\r
723 \r
724     /**\r
725      * Specify whether or not date/time parsing is to be lenient.  With\r
726      * lenient parsing, the parser may use heuristics to interpret inputs that\r
727      * do not precisely match this object's format.  With strict parsing,\r
728      * inputs must match this object's format.\r
729      * @param lenient when true, parsing is lenient\r
730      * @see com.ibm.icu.util.Calendar#setLenient\r
731      * @stable ICU 2.0\r
732      */\r
733     public void setLenient(boolean lenient) {\r
734         dateFormat.setLenient(lenient);\r
735     }\r
736 \r
737     /**\r
738      * Tell whether date/time parsing is to be lenient.\r
739      * @stable ICU 2.0\r
740      */\r
741     public boolean isLenient() {\r
742         return dateFormat.isLenient();\r
743     }\r
744    \r
745     /**\r
746      * Create a {@link DateFormat} object that can be used to format dates in\r
747      * the calendar system specified by <code>cal</code>.\r
748      * <p>\r
749      * @param cal   The calendar system for which a date format is desired.\r
750      *\r
751      * @param dateStyle The type of date format desired.  This can be\r
752      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
753      *              etc.\r
754      *\r
755      * @param locale The locale for which the date format is desired.\r
756      * @stable ICU 2.0\r
757      */\r
758     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, Locale locale) {\r
759         DateFormat df = getDateInstance(dateStyle, locale);\r
760         df.setCalendar(cal);\r
761         return df;\r
762     }\r
763     \r
764     /**\r
765      * Create a {@link DateFormat} object that can be used to format dates in\r
766      * the calendar system specified by <code>cal</code>.\r
767      * <p>\r
768      * @param cal   The calendar system for which a date format is desired.\r
769      *\r
770      * @param dateStyle The type of date format desired.  This can be\r
771      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
772      *              etc.\r
773      *\r
774      * @param locale The locale for which the date format is desired.\r
775      * @stable ICU 3.2\r
776      */\r
777     static final public DateFormat getDateInstance(Calendar cal, int dateStyle, ULocale locale) {\r
778         DateFormat df = getDateInstance(dateStyle, locale);\r
779         df.setCalendar(cal);\r
780         return df;\r
781     }\r
782       \r
783     /**\r
784      * Create a {@link DateFormat} object that can be used to format times in\r
785      * the calendar system specified by <code>cal</code>.\r
786      * <p>\r
787      * <b>Note:</b> When this functionality is moved into the core JDK, this method\r
788      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.\r
789      * <p>\r
790      * @param cal   The calendar system for which a time format is desired.\r
791      *\r
792      * @param timeStyle The type of time format desired.  This can be\r
793      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
794      *              etc.\r
795      *\r
796      * @param locale The locale for which the time format is desired.\r
797      *\r
798      * @see DateFormat#getTimeInstance\r
799      * @stable ICU 2.0\r
800      */\r
801     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, Locale locale) {\r
802         DateFormat df = getTimeInstance(timeStyle, locale);\r
803         df.setCalendar(cal);\r
804         return df;\r
805     }\r
806     \r
807     /**\r
808      * Create a {@link DateFormat} object that can be used to format times in\r
809      * the calendar system specified by <code>cal</code>.\r
810      * <p>\r
811      * <b>Note:</b> When this functionality is moved into the core JDK, this method\r
812      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.\r
813      * <p>\r
814      * @param cal   The calendar system for which a time format is desired.\r
815      *\r
816      * @param timeStyle The type of time format desired.  This can be\r
817      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
818      *              etc.\r
819      *\r
820      * @param locale The locale for which the time format is desired.\r
821      *\r
822      * @see DateFormat#getTimeInstance\r
823      * @stable ICU 3.2\r
824      */\r
825     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle, ULocale locale) {\r
826         DateFormat df = getTimeInstance(timeStyle, locale);\r
827         df.setCalendar(cal);\r
828         return df;\r
829     }\r
830        \r
831     /**\r
832      * Create a {@link DateFormat} object that can be used to format dates and times in\r
833      * the calendar system specified by <code>cal</code>.\r
834      * <p>\r
835      * <b>Note:</b> When this functionality is moved into the core JDK, this method\r
836      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.\r
837      * <p>\r
838      * @param cal   The calendar system for which a date/time format is desired.\r
839      *\r
840      * @param dateStyle The type of date format desired.  This can be\r
841      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
842      *              etc.\r
843      *\r
844      * @param timeStyle The type of time format desired.  This can be\r
845      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
846      *              etc.\r
847      *\r
848      * @param locale The locale for which the date/time format is desired.\r
849      *\r
850      * @see DateFormat#getDateTimeInstance\r
851      * @stable ICU 2.0\r
852      */\r
853     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,\r
854                                                        int timeStyle, Locale locale) {\r
855         DateFormat df = getDateTimeInstance(dateStyle, timeStyle, locale);\r
856         df.setCalendar(cal);\r
857         return df;\r
858     }\r
859 \r
860     /**\r
861      * Create a {@link DateFormat} object that can be used to format dates and times in\r
862      * the calendar system specified by <code>cal</code>.\r
863      * <p>\r
864      * <b>Note:</b> When this functionality is moved into the core JDK, this method\r
865      * will probably be replaced by a new overload of {@link DateFormat#getInstance}.\r
866      * <p>\r
867      * @param cal   The calendar system for which a date/time format is desired.\r
868      *\r
869      * @param dateStyle The type of date format desired.  This can be\r
870      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
871      *              etc.\r
872      *\r
873      * @param timeStyle The type of time format desired.  This can be\r
874      *              {@link DateFormat#SHORT}, {@link DateFormat#MEDIUM},\r
875      *              etc.\r
876      *\r
877      * @param locale The locale for which the date/time format is desired.\r
878      *\r
879      * @see DateFormat#getDateTimeInstance\r
880      * @stable ICU 3.2\r
881      */\r
882     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle,\r
883                                                        int timeStyle, ULocale locale) {\r
884         DateFormat df = getDateTimeInstance(dateStyle, timeStyle, locale);\r
885         df.setCalendar(cal);\r
886         return df;\r
887     }\r
888 \r
889     /**\r
890      * Convenience overload\r
891      * @stable ICU 2.0\r
892      */\r
893     static final public DateFormat getInstance(Calendar cal, Locale locale) {\r
894         return getDateTimeInstance(cal, MEDIUM, SHORT, locale);\r
895     }\r
896 \r
897     /**\r
898      * Convenience overload\r
899      * @stable ICU 3.2\r
900      */\r
901     public static final DateFormat getInstance(Calendar cal, ULocale locale) {\r
902         return getDateTimeInstance(cal, MEDIUM, SHORT, locale);\r
903     }\r
904 \r
905     /**\r
906      * Convenience overload\r
907      * @stable ICU 2.0\r
908      */\r
909     public static final DateFormat getInstance(Calendar cal) {\r
910         return getDateTimeInstance(cal, MEDIUM, SHORT, ULocale.getDefault());\r
911     }\r
912 \r
913     /**\r
914      * Convenience overload\r
915      * @stable ICU 2.0\r
916      */\r
917     public static final DateFormat getDateInstance(Calendar cal, int dateStyle) {\r
918         DateFormat df = getDateInstance(dateStyle);\r
919         df.setCalendar(cal);\r
920         return df;\r
921     }\r
922 \r
923     /**\r
924      * Convenience overload\r
925      * @stable ICU 2.0\r
926      */\r
927     static final public DateFormat getTimeInstance(Calendar cal, int timeStyle) {\r
928         DateFormat df = getTimeInstance(timeStyle);\r
929         df.setCalendar(cal);\r
930         return df;\r
931     }\r
932 \r
933     /**\r
934      * Convenience overload\r
935      * @stable ICU 2.0\r
936      */\r
937     static final public DateFormat getDateTimeInstance(Calendar cal, int dateStyle, int timeStyle) {\r
938         DateFormat df = getDateTimeInstance(dateStyle, timeStyle);\r
939         df.setCalendar(cal);\r
940         return df;\r
941     }\r
942 \r
943     /**\r
944      * Return a string suitable for debugging.\r
945      * @return a string suitable for debugging\r
946      * @stable ICU 3.4.3\r
947      */\r
948     public String toString() {\r
949         return dateFormat.toString();\r
950     }\r
951         \r
952     /**\r
953      * Return a clone of this DateFormat.\r
954      * @return a clone of this DateFormat\r
955      * @stable ICU 3.4.3\r
956      */\r
957     public Object clone() {\r
958         return new DateFormat((java.text.DateFormat)dateFormat.clone());\r
959     }\r
960 \r
961     /**\r
962      * Return true if rhs is a DateFormatSymbols and has the same symbols as this.\r
963      * @return true if rhs equals this\r
964      * @stable ICU 3.4.3\r
965      */\r
966     public boolean equals(Object arg0) {\r
967         try {\r
968             return dateFormat.equals(((DateFormat)arg0).dateFormat);\r
969         }\r
970         catch (Exception e) {\r
971             return false;\r
972         }\r
973     }\r
974         \r
975     /**\r
976      * Return a hashCode.\r
977      * @return a hashCode\r
978      * @stable ICU 3.4.3\r
979      */\r
980     public int hashCode() {\r
981         return dateFormat.hashCode();\r
982     }\r
983 }\r