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