Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/text/DateFormat.java
41152 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
/*
27
* (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
28
* (C) Copyright IBM Corp. 1996 - All Rights Reserved
29
*
30
* The original version of this source code and documentation is copyrighted
31
* and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
32
* materials are provided under terms of a License Agreement between Taligent
33
* and Sun. This technology is protected by multiple US and International
34
* patents. This notice and attribution to Taligent may not be removed.
35
* Taligent is a registered trademark of Taligent, Inc.
36
*
37
*/
38
39
package java.text;
40
41
import java.io.InvalidObjectException;
42
import java.text.spi.DateFormatProvider;
43
import java.util.Calendar;
44
import java.util.Date;
45
import java.util.GregorianCalendar;
46
import java.util.HashMap;
47
import java.util.Locale;
48
import java.util.Map;
49
import java.util.MissingResourceException;
50
import java.util.ResourceBundle;
51
import java.util.TimeZone;
52
import java.util.spi.LocaleServiceProvider;
53
import sun.util.locale.provider.LocaleProviderAdapter;
54
import sun.util.locale.provider.LocaleServiceProviderPool;
55
56
/**
57
* {@code DateFormat} is an abstract class for date/time formatting subclasses which
58
* formats and parses dates or time in a language-independent manner.
59
* The date/time formatting subclass, such as {@link SimpleDateFormat}, allows for
60
* formatting (i.e., date → text), parsing (text → date), and
61
* normalization. The date is represented as a {@code Date} object or
62
* as the milliseconds since January 1, 1970, 00:00:00 GMT.
63
*
64
* <p>{@code DateFormat} provides many class methods for obtaining default date/time
65
* formatters based on the default or a given locale and a number of formatting
66
* styles. The formatting styles include {@link #FULL}, {@link #LONG}, {@link #MEDIUM}, and {@link #SHORT}. More
67
* detail and examples of using these styles are provided in the method
68
* descriptions.
69
*
70
* <p>{@code DateFormat} helps you to format and parse dates for any locale.
71
* Your code can be completely independent of the locale conventions for
72
* months, days of the week, or even the calendar format: lunar vs. solar.
73
*
74
* <p>To format a date for the current Locale, use one of the
75
* static factory methods:
76
* <blockquote>
77
* <pre>{@code
78
* myString = DateFormat.getDateInstance().format(myDate);
79
* }</pre>
80
* </blockquote>
81
* <p>If you are formatting multiple dates, it is
82
* more efficient to get the format and use it multiple times so that
83
* the system doesn't have to fetch the information about the local
84
* language and country conventions multiple times.
85
* <blockquote>
86
* <pre>{@code
87
* DateFormat df = DateFormat.getDateInstance();
88
* for (int i = 0; i < myDate.length; ++i) {
89
* output.println(df.format(myDate[i]) + "; ");
90
* }
91
* }</pre>
92
* </blockquote>
93
* <p>To format a date for a different Locale, specify it in the
94
* call to {@link #getDateInstance(int, Locale) getDateInstance()}.
95
* <blockquote>
96
* <pre>{@code
97
* DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
98
* }</pre>
99
* </blockquote>
100
*
101
* <p>If the specified locale contains "ca" (calendar), "rg" (region override),
102
* and/or "tz" (timezone) <a href="../util/Locale.html#def_locale_extension">Unicode
103
* extensions</a>, the calendar, the country and/or the time zone for formatting
104
* are overridden. If both "ca" and "rg" are specified, the calendar from the "ca"
105
* extension supersedes the implicit one from the "rg" extension.
106
*
107
* <p>You can use a DateFormat to parse also.
108
* <blockquote>
109
* <pre>{@code
110
* myDate = df.parse(myString);
111
* }</pre>
112
* </blockquote>
113
* <p>Use {@code getDateInstance} to get the normal date format for that country.
114
* There are other static factory methods available.
115
* Use {@code getTimeInstance} to get the time format for that country.
116
* Use {@code getDateTimeInstance} to get a date and time format. You can pass in
117
* different options to these factory methods to control the length of the
118
* result; from {@link #SHORT} to {@link #MEDIUM} to {@link #LONG} to {@link #FULL}. The exact result depends
119
* on the locale, but generally:
120
* <ul><li>{@link #SHORT} is completely numeric, such as {@code 12.13.52} or {@code 3:30pm}
121
* <li>{@link #MEDIUM} is longer, such as {@code Jan 12, 1952}
122
* <li>{@link #LONG} is longer, such as {@code January 12, 1952} or {@code 3:30:32pm}
123
* <li>{@link #FULL} is pretty completely specified, such as
124
* {@code Tuesday, April 12, 1952 AD or 3:30:42pm PST}.
125
* </ul>
126
*
127
* <p>You can also set the time zone on the format if you wish.
128
* If you want even more control over the format or parsing,
129
* (or want to give your users more control),
130
* you can try casting the {@code DateFormat} you get from the factory methods
131
* to a {@link SimpleDateFormat}. This will work for the majority
132
* of countries; just remember to put it in a {@code try} block in case you
133
* encounter an unusual one.
134
*
135
* <p>You can also use forms of the parse and format methods with
136
* {@link ParsePosition} and {@link FieldPosition} to
137
* allow you to
138
* <ul><li>progressively parse through pieces of a string.
139
* <li>align any particular field, or find out where it is for selection
140
* on the screen.
141
* </ul>
142
*
143
* <h2><a id="synchronization">Synchronization</a></h2>
144
*
145
* <p>
146
* Date formats are not synchronized.
147
* It is recommended to create separate format instances for each thread.
148
* If multiple threads access a format concurrently, it must be synchronized
149
* externally.
150
* @apiNote Consider using {@link java.time.format.DateTimeFormatter} as an
151
* immutable and thread-safe alternative.
152
*
153
* @implSpec
154
* <ul><li>The {@link #format(Date, StringBuffer, FieldPosition)} and
155
* {@link #parse(String, ParsePosition)} methods may throw
156
* {@code NullPointerException}, if any of their parameter is {@code null}.
157
* The subclass may provide its own implementation and specification about
158
* {@code NullPointerException}.</li>
159
* <li>The {@link #setCalendar(Calendar)}, {@link
160
* #setNumberFormat(NumberFormat)} and {@link #setTimeZone(TimeZone)} methods
161
* do not throw {@code NullPointerException} when their parameter is
162
* {@code null}, but any subsequent operations on the same instance may throw
163
* {@code NullPointerException}.</li>
164
* <li>The {@link #getCalendar()}, {@link #getNumberFormat()} and
165
* {@link getTimeZone()} methods may return {@code null}, if the respective
166
* values of this instance is set to {@code null} through the corresponding
167
* setter methods. For Example: {@link #getTimeZone()} may return {@code null},
168
* if the {@code TimeZone} value of this instance is set as
169
* {@link #setTimeZone(java.util.TimeZone) setTimeZone(null)}.</li>
170
* </ul>
171
*
172
* @see Format
173
* @see NumberFormat
174
* @see SimpleDateFormat
175
* @see java.util.Calendar
176
* @see java.util.GregorianCalendar
177
* @see java.util.TimeZone
178
* @see java.time.format.DateTimeFormatter
179
* @author Mark Davis, Chen-Lieh Huang, Alan Liu
180
* @since 1.1
181
*/
182
public abstract class DateFormat extends Format {
183
184
/**
185
* The {@link Calendar} instance used for calculating the date-time fields
186
* and the instant of time. This field is used for both formatting and
187
* parsing.
188
*
189
* <p>Subclasses should initialize this field to a {@link Calendar}
190
* appropriate for the {@link Locale} associated with this
191
* {@code DateFormat}.
192
* @serial
193
*/
194
protected Calendar calendar;
195
196
/**
197
* The number formatter that {@code DateFormat} uses to format numbers
198
* in dates and times. Subclasses should initialize this to a number format
199
* appropriate for the locale associated with this {@code DateFormat}.
200
* @serial
201
*/
202
protected NumberFormat numberFormat;
203
204
/**
205
* Useful constant for ERA field alignment.
206
* Used in FieldPosition of date/time formatting.
207
*/
208
public static final int ERA_FIELD = 0;
209
/**
210
* Useful constant for YEAR field alignment.
211
* Used in FieldPosition of date/time formatting.
212
*/
213
public static final int YEAR_FIELD = 1;
214
/**
215
* Useful constant for MONTH field alignment.
216
* Used in FieldPosition of date/time formatting.
217
*/
218
public static final int MONTH_FIELD = 2;
219
/**
220
* Useful constant for DATE field alignment.
221
* Used in FieldPosition of date/time formatting.
222
*/
223
public static final int DATE_FIELD = 3;
224
/**
225
* Useful constant for one-based HOUR_OF_DAY field alignment.
226
* Used in FieldPosition of date/time formatting.
227
* HOUR_OF_DAY1_FIELD is used for the one-based 24-hour clock.
228
* For example, 23:59 + 01:00 results in 24:59.
229
*/
230
public static final int HOUR_OF_DAY1_FIELD = 4;
231
/**
232
* Useful constant for zero-based HOUR_OF_DAY field alignment.
233
* Used in FieldPosition of date/time formatting.
234
* HOUR_OF_DAY0_FIELD is used for the zero-based 24-hour clock.
235
* For example, 23:59 + 01:00 results in 00:59.
236
*/
237
public static final int HOUR_OF_DAY0_FIELD = 5;
238
/**
239
* Useful constant for MINUTE field alignment.
240
* Used in FieldPosition of date/time formatting.
241
*/
242
public static final int MINUTE_FIELD = 6;
243
/**
244
* Useful constant for SECOND field alignment.
245
* Used in FieldPosition of date/time formatting.
246
*/
247
public static final int SECOND_FIELD = 7;
248
/**
249
* Useful constant for MILLISECOND field alignment.
250
* Used in FieldPosition of date/time formatting.
251
*/
252
public static final int MILLISECOND_FIELD = 8;
253
/**
254
* Useful constant for DAY_OF_WEEK field alignment.
255
* Used in FieldPosition of date/time formatting.
256
*/
257
public static final int DAY_OF_WEEK_FIELD = 9;
258
/**
259
* Useful constant for DAY_OF_YEAR field alignment.
260
* Used in FieldPosition of date/time formatting.
261
*/
262
public static final int DAY_OF_YEAR_FIELD = 10;
263
/**
264
* Useful constant for DAY_OF_WEEK_IN_MONTH field alignment.
265
* Used in FieldPosition of date/time formatting.
266
*/
267
public static final int DAY_OF_WEEK_IN_MONTH_FIELD = 11;
268
/**
269
* Useful constant for WEEK_OF_YEAR field alignment.
270
* Used in FieldPosition of date/time formatting.
271
*/
272
public static final int WEEK_OF_YEAR_FIELD = 12;
273
/**
274
* Useful constant for WEEK_OF_MONTH field alignment.
275
* Used in FieldPosition of date/time formatting.
276
*/
277
public static final int WEEK_OF_MONTH_FIELD = 13;
278
/**
279
* Useful constant for AM_PM field alignment.
280
* Used in FieldPosition of date/time formatting.
281
*/
282
public static final int AM_PM_FIELD = 14;
283
/**
284
* Useful constant for one-based HOUR field alignment.
285
* Used in FieldPosition of date/time formatting.
286
* HOUR1_FIELD is used for the one-based 12-hour clock.
287
* For example, 11:30 PM + 1 hour results in 12:30 AM.
288
*/
289
public static final int HOUR1_FIELD = 15;
290
/**
291
* Useful constant for zero-based HOUR field alignment.
292
* Used in FieldPosition of date/time formatting.
293
* HOUR0_FIELD is used for the zero-based 12-hour clock.
294
* For example, 11:30 PM + 1 hour results in 00:30 AM.
295
*/
296
public static final int HOUR0_FIELD = 16;
297
/**
298
* Useful constant for TIMEZONE field alignment.
299
* Used in FieldPosition of date/time formatting.
300
*/
301
public static final int TIMEZONE_FIELD = 17;
302
303
// Proclaim serial compatibility with 1.1 FCS
304
@java.io.Serial
305
private static final long serialVersionUID = 7218322306649953788L;
306
307
/**
308
* Formats the given {@code Object} into a date-time string. The formatted
309
* string is appended to the given {@code StringBuffer}.
310
*
311
* @param obj Must be a {@code Date} or a {@code Number} representing a
312
* millisecond offset from the <a href="../util/Calendar.html#Epoch">Epoch</a>.
313
* @param toAppendTo The string buffer for the returning date-time string.
314
* @param fieldPosition keeps track on the position of the field within
315
* the returned string. For example, given a date-time text
316
* {@code "1996.07.10 AD at 15:08:56 PDT"}, if the given {@code fieldPosition}
317
* is {@link DateFormat#YEAR_FIELD}, the begin index and end index of
318
* {@code fieldPosition} will be set to 0 and 4, respectively.
319
* Notice that if the same date-time field appears more than once in a
320
* pattern, the {@code fieldPosition} will be set for the first occurrence
321
* of that date-time field. For instance, formatting a {@code Date} to the
322
* date-time string {@code "1 PM PDT (Pacific Daylight Time)"} using the
323
* pattern {@code "h a z (zzzz)"} and the alignment field
324
* {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
325
* {@code fieldPosition} will be set to 5 and 8, respectively, for the
326
* first occurrence of the timezone pattern character {@code 'z'}.
327
* @return the string buffer passed in as {@code toAppendTo},
328
* with formatted text appended.
329
* @throws IllegalArgumentException if the {@code Format} cannot format
330
* the given {@code obj}.
331
* @see java.text.Format
332
*/
333
public final StringBuffer format(Object obj, StringBuffer toAppendTo,
334
FieldPosition fieldPosition)
335
{
336
if (obj instanceof Date)
337
return format( (Date)obj, toAppendTo, fieldPosition );
338
else if (obj instanceof Number)
339
return format( new Date(((Number)obj).longValue()),
340
toAppendTo, fieldPosition );
341
else
342
throw new IllegalArgumentException("Cannot format given Object as a Date");
343
}
344
345
/**
346
* Formats a {@link Date} into a date-time string. The formatted
347
* string is appended to the given {@code StringBuffer}.
348
*
349
* @param date a Date to be formatted into a date-time string.
350
* @param toAppendTo the string buffer for the returning date-time string.
351
* @param fieldPosition keeps track on the position of the field within
352
* the returned string. For example, given a date-time text
353
* {@code "1996.07.10 AD at 15:08:56 PDT"}, if the given {@code fieldPosition}
354
* is {@link DateFormat#YEAR_FIELD}, the begin index and end index of
355
* {@code fieldPosition} will be set to 0 and 4, respectively.
356
* Notice that if the same date-time field appears more than once in a
357
* pattern, the {@code fieldPosition} will be set for the first occurrence
358
* of that date-time field. For instance, formatting a {@code Date} to the
359
* date-time string {@code "1 PM PDT (Pacific Daylight Time)"} using the
360
* pattern {@code "h a z (zzzz)"} and the alignment field
361
* {@link DateFormat#TIMEZONE_FIELD}, the begin index and end index of
362
* {@code fieldPosition} will be set to 5 and 8, respectively, for the
363
* first occurrence of the timezone pattern character {@code 'z'}.
364
* @return the string buffer passed in as {@code toAppendTo}, with formatted
365
* text appended.
366
*/
367
public abstract StringBuffer format(Date date, StringBuffer toAppendTo,
368
FieldPosition fieldPosition);
369
370
/**
371
* Formats a {@link Date} into a date-time string.
372
*
373
* @param date the time value to be formatted into a date-time string.
374
* @return the formatted date-time string.
375
*/
376
public final String format(Date date)
377
{
378
return format(date, new StringBuffer(),
379
DontCareFieldPosition.INSTANCE).toString();
380
}
381
382
/**
383
* Parses text from the beginning of the given string to produce a date.
384
* The method may not use the entire text of the given string.
385
* <p>
386
* See the {@link #parse(String, ParsePosition)} method for more information
387
* on date parsing.
388
*
389
* @param source A {@code String} whose beginning should be parsed.
390
* @return A {@code Date} parsed from the string.
391
* @throws ParseException if the beginning of the specified string
392
* cannot be parsed.
393
*/
394
public Date parse(String source) throws ParseException
395
{
396
ParsePosition pos = new ParsePosition(0);
397
Date result = parse(source, pos);
398
if (pos.index == 0)
399
throw new ParseException("Unparseable date: \"" + source + "\"" ,
400
pos.errorIndex);
401
return result;
402
}
403
404
/**
405
* Parse a date/time string according to the given parse position. For
406
* example, a time text {@code "07/10/96 4:5 PM, PDT"} will be parsed into a {@code Date}
407
* that is equivalent to {@code Date(837039900000L)}.
408
*
409
* <p> By default, parsing is lenient: If the input is not in the form used
410
* by this object's format method but can still be parsed as a date, then
411
* the parse succeeds. Clients may insist on strict adherence to the
412
* format by calling {@link #setLenient(boolean) setLenient(false)}.
413
*
414
* <p>This parsing operation uses the {@link #calendar} to produce
415
* a {@code Date}. As a result, the {@code calendar}'s date-time
416
* fields and the {@code TimeZone} value may have been
417
* overwritten, depending on subclass implementations. Any {@code
418
* TimeZone} value that has previously been set by a call to
419
* {@link #setTimeZone(java.util.TimeZone) setTimeZone} may need
420
* to be restored for further operations.
421
*
422
* @param source The date/time string to be parsed
423
*
424
* @param pos On input, the position at which to start parsing; on
425
* output, the position at which parsing terminated, or the
426
* start position if the parse failed.
427
*
428
* @return A {@code Date}, or {@code null} if the input could not be parsed
429
*/
430
public abstract Date parse(String source, ParsePosition pos);
431
432
/**
433
* Parses text from a string to produce a {@code Date}.
434
* <p>
435
* The method attempts to parse text starting at the index given by
436
* {@code pos}.
437
* If parsing succeeds, then the index of {@code pos} is updated
438
* to the index after the last character used (parsing does not necessarily
439
* use all characters up to the end of the string), and the parsed
440
* date is returned. The updated {@code pos} can be used to
441
* indicate the starting point for the next call to this method.
442
* If an error occurs, then the index of {@code pos} is not
443
* changed, the error index of {@code pos} is set to the index of
444
* the character where the error occurred, and null is returned.
445
* <p>
446
* See the {@link #parse(String, ParsePosition)} method for more information
447
* on date parsing.
448
*
449
* @param source A {@code String}, part of which should be parsed.
450
* @param pos A {@code ParsePosition} object with index and error
451
* index information as described above.
452
* @return A {@code Date} parsed from the string. In case of
453
* error, returns null.
454
* @throws NullPointerException if {@code source} or {@code pos} is null.
455
*/
456
public Object parseObject(String source, ParsePosition pos) {
457
return parse(source, pos);
458
}
459
460
/**
461
* Constant for full style pattern.
462
*/
463
public static final int FULL = 0;
464
/**
465
* Constant for long style pattern.
466
*/
467
public static final int LONG = 1;
468
/**
469
* Constant for medium style pattern.
470
*/
471
public static final int MEDIUM = 2;
472
/**
473
* Constant for short style pattern.
474
*/
475
public static final int SHORT = 3;
476
/**
477
* Constant for default style pattern. Its value is MEDIUM.
478
*/
479
public static final int DEFAULT = MEDIUM;
480
481
/**
482
* Gets the time formatter with the default formatting style
483
* for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
484
* <p>This is equivalent to calling
485
* {@link #getTimeInstance(int, Locale) getTimeInstance(DEFAULT,
486
* Locale.getDefault(Locale.Category.FORMAT))}.
487
* @see java.util.Locale#getDefault(java.util.Locale.Category)
488
* @see java.util.Locale.Category#FORMAT
489
* @return a time formatter.
490
*/
491
public static final DateFormat getTimeInstance()
492
{
493
return get(DEFAULT, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
494
}
495
496
/**
497
* Gets the time formatter with the given formatting style
498
* for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
499
* <p>This is equivalent to calling
500
* {@link #getTimeInstance(int, Locale) getTimeInstance(style,
501
* Locale.getDefault(Locale.Category.FORMAT))}.
502
* @see java.util.Locale#getDefault(java.util.Locale.Category)
503
* @see java.util.Locale.Category#FORMAT
504
* @param style the given formatting style. For example,
505
* SHORT for "h:mm a" in the US locale.
506
* @return a time formatter.
507
*/
508
public static final DateFormat getTimeInstance(int style)
509
{
510
return get(style, 0, 1, Locale.getDefault(Locale.Category.FORMAT));
511
}
512
513
/**
514
* Gets the time formatter with the given formatting style
515
* for the given locale.
516
* @param style the given formatting style. For example,
517
* SHORT for "h:mm a" in the US locale.
518
* @param aLocale the given locale.
519
* @return a time formatter.
520
*/
521
public static final DateFormat getTimeInstance(int style,
522
Locale aLocale)
523
{
524
return get(style, 0, 1, aLocale);
525
}
526
527
/**
528
* Gets the date formatter with the default formatting style
529
* for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
530
* <p>This is equivalent to calling
531
* {@link #getDateInstance(int, Locale) getDateInstance(DEFAULT,
532
* Locale.getDefault(Locale.Category.FORMAT))}.
533
* @see java.util.Locale#getDefault(java.util.Locale.Category)
534
* @see java.util.Locale.Category#FORMAT
535
* @return a date formatter.
536
*/
537
public static final DateFormat getDateInstance()
538
{
539
return get(0, DEFAULT, 2, Locale.getDefault(Locale.Category.FORMAT));
540
}
541
542
/**
543
* Gets the date formatter with the given formatting style
544
* for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
545
* <p>This is equivalent to calling
546
* {@link #getDateInstance(int, Locale) getDateInstance(style,
547
* Locale.getDefault(Locale.Category.FORMAT))}.
548
* @see java.util.Locale#getDefault(java.util.Locale.Category)
549
* @see java.util.Locale.Category#FORMAT
550
* @param style the given formatting style. For example,
551
* SHORT for "M/d/yy" in the US locale.
552
* @return a date formatter.
553
*/
554
public static final DateFormat getDateInstance(int style)
555
{
556
return get(0, style, 2, Locale.getDefault(Locale.Category.FORMAT));
557
}
558
559
/**
560
* Gets the date formatter with the given formatting style
561
* for the given locale.
562
* @param style the given formatting style. For example,
563
* SHORT for "M/d/yy" in the US locale.
564
* @param aLocale the given locale.
565
* @return a date formatter.
566
*/
567
public static final DateFormat getDateInstance(int style,
568
Locale aLocale)
569
{
570
return get(0, style, 2, aLocale);
571
}
572
573
/**
574
* Gets the date/time formatter with the default formatting style
575
* for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
576
* <p>This is equivalent to calling
577
* {@link #getDateTimeInstance(int, int, Locale) getDateTimeInstance(DEFAULT,
578
* DEFAULT, Locale.getDefault(Locale.Category.FORMAT))}.
579
* @see java.util.Locale#getDefault(java.util.Locale.Category)
580
* @see java.util.Locale.Category#FORMAT
581
* @return a date/time formatter.
582
*/
583
public static final DateFormat getDateTimeInstance()
584
{
585
return get(DEFAULT, DEFAULT, 3, Locale.getDefault(Locale.Category.FORMAT));
586
}
587
588
/**
589
* Gets the date/time formatter with the given date and time
590
* formatting styles for the default {@link java.util.Locale.Category#FORMAT FORMAT} locale.
591
* <p>This is equivalent to calling
592
* {@link #getDateTimeInstance(int, int, Locale) getDateTimeInstance(dateStyle,
593
* timeStyle, Locale.getDefault(Locale.Category.FORMAT))}.
594
* @see java.util.Locale#getDefault(java.util.Locale.Category)
595
* @see java.util.Locale.Category#FORMAT
596
* @param dateStyle the given date formatting style. For example,
597
* SHORT for "M/d/yy" in the US locale.
598
* @param timeStyle the given time formatting style. For example,
599
* SHORT for "h:mm a" in the US locale.
600
* @return a date/time formatter.
601
*/
602
public static final DateFormat getDateTimeInstance(int dateStyle,
603
int timeStyle)
604
{
605
return get(timeStyle, dateStyle, 3, Locale.getDefault(Locale.Category.FORMAT));
606
}
607
608
/**
609
* Gets the date/time formatter with the given formatting styles
610
* for the given locale.
611
* @param dateStyle the given date formatting style.
612
* @param timeStyle the given time formatting style.
613
* @param aLocale the given locale.
614
* @return a date/time formatter.
615
*/
616
public static final DateFormat
617
getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale)
618
{
619
return get(timeStyle, dateStyle, 3, aLocale);
620
}
621
622
/**
623
* Get a default date/time formatter that uses the SHORT style for both the
624
* date and the time.
625
*
626
* @return a date/time formatter
627
*/
628
public static final DateFormat getInstance() {
629
return getDateTimeInstance(SHORT, SHORT);
630
}
631
632
/**
633
* Returns an array of all locales for which the
634
* {@code get*Instance} methods of this class can return
635
* localized instances.
636
* The returned array represents the union of locales supported by the Java
637
* runtime and by installed
638
* {@link java.text.spi.DateFormatProvider DateFormatProvider} implementations.
639
* It must contain at least a {@code Locale} instance equal to
640
* {@link java.util.Locale#US Locale.US}.
641
*
642
* @return An array of locales for which localized
643
* {@code DateFormat} instances are available.
644
*/
645
public static Locale[] getAvailableLocales()
646
{
647
LocaleServiceProviderPool pool =
648
LocaleServiceProviderPool.getPool(DateFormatProvider.class);
649
return pool.getAvailableLocales();
650
}
651
652
/**
653
* Set the calendar to be used by this date format. Initially, the default
654
* calendar for the specified or default locale is used.
655
*
656
* <p>Any {@link java.util.TimeZone TimeZone} and {@linkplain
657
* #isLenient() leniency} values that have previously been set are
658
* overwritten by {@code newCalendar}'s values.
659
*
660
* @param newCalendar the new {@code Calendar} to be used by the date format
661
*/
662
public void setCalendar(Calendar newCalendar)
663
{
664
this.calendar = newCalendar;
665
}
666
667
/**
668
* Gets the calendar associated with this date/time formatter.
669
*
670
* @return the calendar associated with this date/time formatter.
671
*/
672
public Calendar getCalendar()
673
{
674
return calendar;
675
}
676
677
/**
678
* Allows you to set the number formatter.
679
* @param newNumberFormat the given new NumberFormat.
680
*/
681
public void setNumberFormat(NumberFormat newNumberFormat)
682
{
683
this.numberFormat = newNumberFormat;
684
}
685
686
/**
687
* Gets the number formatter which this date/time formatter uses to
688
* format and parse a time.
689
* @return the number formatter which this date/time formatter uses.
690
*/
691
public NumberFormat getNumberFormat()
692
{
693
return numberFormat;
694
}
695
696
/**
697
* Sets the time zone for the calendar of this {@code DateFormat} object.
698
* This method is equivalent to the following call.
699
* <blockquote><pre>{@code
700
* getCalendar().setTimeZone(zone)
701
* }</pre></blockquote>
702
*
703
* <p>The {@code TimeZone} set by this method is overwritten by a
704
* {@link #setCalendar(java.util.Calendar) setCalendar} call.
705
*
706
* <p>The {@code TimeZone} set by this method may be overwritten as
707
* a result of a call to the parse method.
708
*
709
* @param zone the given new time zone.
710
*/
711
public void setTimeZone(TimeZone zone)
712
{
713
calendar.setTimeZone(zone);
714
}
715
716
/**
717
* Gets the time zone.
718
* This method is equivalent to the following call.
719
* <blockquote><pre>{@code
720
* getCalendar().getTimeZone()
721
* }</pre></blockquote>
722
*
723
* @return the time zone associated with the calendar of DateFormat.
724
*/
725
public TimeZone getTimeZone()
726
{
727
return calendar.getTimeZone();
728
}
729
730
/**
731
* Specify whether or not date/time parsing is to be lenient. With
732
* lenient parsing, the parser may use heuristics to interpret inputs that
733
* do not precisely match this object's format. With strict parsing,
734
* inputs must match this object's format.
735
*
736
* <p>This method is equivalent to the following call.
737
* <blockquote><pre>{@code
738
* getCalendar().setLenient(lenient)
739
* }</pre></blockquote>
740
*
741
* <p>This leniency value is overwritten by a call to {@link
742
* #setCalendar(java.util.Calendar) setCalendar()}.
743
*
744
* @param lenient when {@code true}, parsing is lenient
745
* @see java.util.Calendar#setLenient(boolean)
746
*/
747
public void setLenient(boolean lenient)
748
{
749
calendar.setLenient(lenient);
750
}
751
752
/**
753
* Tell whether date/time parsing is to be lenient.
754
* This method is equivalent to the following call.
755
* <blockquote><pre>{@code
756
* getCalendar().isLenient()
757
* }</pre></blockquote>
758
*
759
* @return {@code true} if the {@link #calendar} is lenient;
760
* {@code false} otherwise.
761
* @see java.util.Calendar#isLenient()
762
*/
763
public boolean isLenient()
764
{
765
return calendar.isLenient();
766
}
767
768
/**
769
* Overrides hashCode
770
*/
771
public int hashCode() {
772
return numberFormat.hashCode();
773
// just enough fields for a reasonable distribution
774
}
775
776
/**
777
* Overrides equals
778
*/
779
public boolean equals(Object obj) {
780
if (this == obj) return true;
781
if (obj == null || getClass() != obj.getClass()) return false;
782
DateFormat other = (DateFormat) obj;
783
return (// calendar.equivalentTo(other.calendar) // THIS API DOESN'T EXIST YET!
784
calendar.getFirstDayOfWeek() == other.calendar.getFirstDayOfWeek() &&
785
calendar.getMinimalDaysInFirstWeek() == other.calendar.getMinimalDaysInFirstWeek() &&
786
calendar.isLenient() == other.calendar.isLenient() &&
787
calendar.getTimeZone().equals(other.calendar.getTimeZone()) &&
788
numberFormat.equals(other.numberFormat));
789
}
790
791
/**
792
* Overrides Cloneable
793
*/
794
public Object clone()
795
{
796
DateFormat other = (DateFormat) super.clone();
797
other.calendar = (Calendar) calendar.clone();
798
other.numberFormat = (NumberFormat) numberFormat.clone();
799
return other;
800
}
801
802
/**
803
* Creates a DateFormat with the given time and/or date style in the given
804
* locale.
805
* @param timeStyle a value from 0 to 3 indicating the time format,
806
* ignored if flags is 2
807
* @param dateStyle a value from 0 to 3 indicating the time format,
808
* ignored if flags is 1
809
* @param flags either 1 for a time format, 2 for a date format,
810
* or 3 for a date/time format
811
* @param loc the locale for the format
812
*/
813
private static DateFormat get(int timeStyle, int dateStyle,
814
int flags, Locale loc) {
815
if ((flags & 1) != 0) {
816
if (timeStyle < 0 || timeStyle > 3) {
817
throw new IllegalArgumentException("Illegal time style " + timeStyle);
818
}
819
} else {
820
timeStyle = -1;
821
}
822
if ((flags & 2) != 0) {
823
if (dateStyle < 0 || dateStyle > 3) {
824
throw new IllegalArgumentException("Illegal date style " + dateStyle);
825
}
826
} else {
827
dateStyle = -1;
828
}
829
830
LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, loc);
831
DateFormat dateFormat = get(adapter, timeStyle, dateStyle, loc);
832
if (dateFormat == null) {
833
dateFormat = get(LocaleProviderAdapter.forJRE(), timeStyle, dateStyle, loc);
834
}
835
return dateFormat;
836
}
837
838
private static DateFormat get(LocaleProviderAdapter adapter, int timeStyle, int dateStyle, Locale loc) {
839
DateFormatProvider provider = adapter.getDateFormatProvider();
840
DateFormat dateFormat;
841
if (timeStyle == -1) {
842
dateFormat = provider.getDateInstance(dateStyle, loc);
843
} else {
844
if (dateStyle == -1) {
845
dateFormat = provider.getTimeInstance(timeStyle, loc);
846
} else {
847
dateFormat = provider.getDateTimeInstance(dateStyle, timeStyle, loc);
848
}
849
}
850
return dateFormat;
851
}
852
853
/**
854
* Create a new date format.
855
*/
856
protected DateFormat() {}
857
858
/**
859
* Defines constants that are used as attribute keys in the
860
* {@code AttributedCharacterIterator} returned
861
* from {@code DateFormat.formatToCharacterIterator} and as
862
* field identifiers in {@code FieldPosition}.
863
* <p>
864
* The class also provides two methods to map
865
* between its constants and the corresponding Calendar constants.
866
*
867
* @since 1.4
868
* @see java.util.Calendar
869
*/
870
public static class Field extends Format.Field {
871
872
// Proclaim serial compatibility with 1.4 FCS
873
@java.io.Serial
874
private static final long serialVersionUID = 7441350119349544720L;
875
876
// table of all instances in this class, used by readResolve
877
private static final Map<String, Field> instanceMap = new HashMap<>(18);
878
// Maps from Calendar constant (such as Calendar.ERA) to Field
879
// constant (such as Field.ERA).
880
private static final Field[] calendarToFieldMapping =
881
new Field[Calendar.FIELD_COUNT];
882
883
/** Calendar field. */
884
private int calendarField;
885
886
/**
887
* Returns the {@code Field} constant that corresponds to
888
* the {@code Calendar} constant {@code calendarField}.
889
* If there is no direct mapping between the {@code Calendar}
890
* constant and a {@code Field}, null is returned.
891
*
892
* @throws IllegalArgumentException if {@code calendarField} is
893
* not the value of a {@code Calendar} field constant.
894
* @param calendarField Calendar field constant
895
* @return Field instance representing calendarField.
896
* @see java.util.Calendar
897
*/
898
public static Field ofCalendarField(int calendarField) {
899
if (calendarField < 0 || calendarField >=
900
calendarToFieldMapping.length) {
901
throw new IllegalArgumentException("Unknown Calendar constant "
902
+ calendarField);
903
}
904
return calendarToFieldMapping[calendarField];
905
}
906
907
/**
908
* Creates a {@code Field}.
909
*
910
* @param name the name of the {@code Field}
911
* @param calendarField the {@code Calendar} constant this
912
* {@code Field} corresponds to; any value, even one
913
* outside the range of legal {@code Calendar} values may
914
* be used, but {@code -1} should be used for values
915
* that don't correspond to legal {@code Calendar} values
916
*/
917
protected Field(String name, int calendarField) {
918
super(name);
919
this.calendarField = calendarField;
920
if (this.getClass() == DateFormat.Field.class) {
921
instanceMap.put(name, this);
922
if (calendarField >= 0) {
923
// assert(calendarField < Calendar.FIELD_COUNT);
924
calendarToFieldMapping[calendarField] = this;
925
}
926
}
927
}
928
929
/**
930
* Returns the {@code Calendar} field associated with this
931
* attribute. For example, if this represents the hours field of
932
* a {@code Calendar}, this would return
933
* {@code Calendar.HOUR}. If there is no corresponding
934
* {@code Calendar} constant, this will return -1.
935
*
936
* @return Calendar constant for this field
937
* @see java.util.Calendar
938
*/
939
public int getCalendarField() {
940
return calendarField;
941
}
942
943
/**
944
* Resolves instances being deserialized to the predefined constants.
945
*
946
* @throws InvalidObjectException if the constant could not be
947
* resolved.
948
* @return resolved DateFormat.Field constant
949
*/
950
@Override
951
@java.io.Serial
952
protected Object readResolve() throws InvalidObjectException {
953
if (this.getClass() != DateFormat.Field.class) {
954
throw new InvalidObjectException("subclass didn't correctly implement readResolve");
955
}
956
957
Object instance = instanceMap.get(getName());
958
if (instance != null) {
959
return instance;
960
} else {
961
throw new InvalidObjectException("unknown attribute name");
962
}
963
}
964
965
//
966
// The constants
967
//
968
969
/**
970
* Constant identifying the era field.
971
*/
972
public static final Field ERA = new Field("era", Calendar.ERA);
973
974
/**
975
* Constant identifying the year field.
976
*/
977
public static final Field YEAR = new Field("year", Calendar.YEAR);
978
979
/**
980
* Constant identifying the month field.
981
*/
982
public static final Field MONTH = new Field("month", Calendar.MONTH);
983
984
/**
985
* Constant identifying the day of month field.
986
*/
987
public static final Field DAY_OF_MONTH = new
988
Field("day of month", Calendar.DAY_OF_MONTH);
989
990
/**
991
* Constant identifying the hour of day field, where the legal values
992
* are 1 to 24.
993
*/
994
public static final Field HOUR_OF_DAY1 = new Field("hour of day 1",-1);
995
996
/**
997
* Constant identifying the hour of day field, where the legal values
998
* are 0 to 23.
999
*/
1000
public static final Field HOUR_OF_DAY0 = new
1001
Field("hour of day", Calendar.HOUR_OF_DAY);
1002
1003
/**
1004
* Constant identifying the minute field.
1005
*/
1006
public static final Field MINUTE =new Field("minute", Calendar.MINUTE);
1007
1008
/**
1009
* Constant identifying the second field.
1010
*/
1011
public static final Field SECOND =new Field("second", Calendar.SECOND);
1012
1013
/**
1014
* Constant identifying the millisecond field.
1015
*/
1016
public static final Field MILLISECOND = new
1017
Field("millisecond", Calendar.MILLISECOND);
1018
1019
/**
1020
* Constant identifying the day of week field.
1021
*/
1022
public static final Field DAY_OF_WEEK = new
1023
Field("day of week", Calendar.DAY_OF_WEEK);
1024
1025
/**
1026
* Constant identifying the day of year field.
1027
*/
1028
public static final Field DAY_OF_YEAR = new
1029
Field("day of year", Calendar.DAY_OF_YEAR);
1030
1031
/**
1032
* Constant identifying the day of week field.
1033
*/
1034
public static final Field DAY_OF_WEEK_IN_MONTH =
1035
new Field("day of week in month",
1036
Calendar.DAY_OF_WEEK_IN_MONTH);
1037
1038
/**
1039
* Constant identifying the week of year field.
1040
*/
1041
public static final Field WEEK_OF_YEAR = new
1042
Field("week of year", Calendar.WEEK_OF_YEAR);
1043
1044
/**
1045
* Constant identifying the week of month field.
1046
*/
1047
public static final Field WEEK_OF_MONTH = new
1048
Field("week of month", Calendar.WEEK_OF_MONTH);
1049
1050
/**
1051
* Constant identifying the time of day indicator
1052
* (e.g. "a.m." or "p.m.") field.
1053
*/
1054
public static final Field AM_PM = new
1055
Field("am pm", Calendar.AM_PM);
1056
1057
/**
1058
* Constant identifying the hour field, where the legal values are
1059
* 1 to 12.
1060
*/
1061
public static final Field HOUR1 = new Field("hour 1", -1);
1062
1063
/**
1064
* Constant identifying the hour field, where the legal values are
1065
* 0 to 11.
1066
*/
1067
public static final Field HOUR0 = new
1068
Field("hour", Calendar.HOUR);
1069
1070
/**
1071
* Constant identifying the time zone field.
1072
*/
1073
public static final Field TIME_ZONE = new Field("time zone", -1);
1074
}
1075
}
1076
1077