77 import java.text.ParseException; |
77 import java.text.ParseException; |
78 import java.text.ParsePosition; |
78 import java.text.ParsePosition; |
79 import java.time.DateTimeException; |
79 import java.time.DateTimeException; |
80 import java.time.ZoneId; |
80 import java.time.ZoneId; |
81 import java.time.ZoneOffset; |
81 import java.time.ZoneOffset; |
|
82 import java.time.chrono.Chronology; |
|
83 import java.time.chrono.IsoChronology; |
82 import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser; |
84 import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser; |
83 import java.time.chrono.Chronology; |
|
84 import java.time.temporal.ChronoField; |
85 import java.time.temporal.ChronoField; |
85 import java.time.temporal.IsoFields; |
86 import java.time.temporal.IsoFields; |
86 import java.time.temporal.TemporalAccessor; |
87 import java.time.temporal.TemporalAccessor; |
|
88 import java.time.temporal.TemporalField; |
87 import java.time.temporal.TemporalQuery; |
89 import java.time.temporal.TemporalQuery; |
|
90 import java.util.Arrays; |
|
91 import java.util.Collections; |
88 import java.util.HashMap; |
92 import java.util.HashMap; |
|
93 import java.util.HashSet; |
89 import java.util.Locale; |
94 import java.util.Locale; |
90 import java.util.Map; |
95 import java.util.Map; |
91 import java.util.Objects; |
96 import java.util.Objects; |
|
97 import java.util.Set; |
92 |
98 |
93 /** |
99 /** |
94 * Formatter for printing and parsing date-time objects. |
100 * Formatter for printing and parsing date-time objects. |
95 * <p> |
101 * <p> |
96 * This class provides the main application entry point for printing and parsing |
102 * This class provides the main application entry point for printing and parsing |
97 * and provides common implementations of {@code DateTimeFormatter}: |
103 * and provides common implementations of {@code DateTimeFormatter}: |
98 * <p><ul> |
104 * <ul> |
99 * <li>Using pattern letters, such as {@code yyyy-MMM-dd} |
105 * <li>Using predefined constants, such as {@link #ISO_LOCAL_DATE}</li> |
100 * <li>Using localized styles, such as {@code long} or {@code medium} |
106 * <li>Using pattern letters, such as {@code uuuu-MMM-dd}</li> |
101 * <li>Using predefined constants, such as {@code ISO_LOCAL_DATE} |
107 * <li>Using localized styles, such as {@code long} or {@code medium}</li> |
102 * </ul></p> |
108 * </ul> |
|
109 * <p> |
|
110 * More complex formatters are provided by |
|
111 * {@link DateTimeFormatterBuilder DateTimeFormatterBuilder}. |
103 * |
112 * |
104 * <p> |
113 * <p> |
105 * In most cases, provided formatters will be sufficient. |
114 * The main date-time classes provide two methods - one for formatting, |
106 * For more complex formatters, a {@link DateTimeFormatterBuilder builder} is provided. |
115 * {@code format(DateTimeFormatter formatter)}, and one for parsing, |
107 * The main date-time classes provide two methods - one for printing, |
|
108 * {@code toString(DateTimeFormatter formatter)}, and one for parsing, |
|
109 * {@code parse(CharSequence text, DateTimeFormatter formatter)}. |
116 * {@code parse(CharSequence text, DateTimeFormatter formatter)}. |
110 * For example: |
117 * <p>For example: |
111 * <pre> |
118 * <blockquote><pre> |
112 * String text = date.toString(formatter); |
119 * String text = date.toString(formatter); |
113 * LocalDate date = LocalDate.parse(text, formatter); |
120 * LocalDate date = LocalDate.parse(text, formatter); |
|
121 * </pre></blockquote> |
|
122 * <p> |
|
123 * In addition to the format, formatters can be created with desired Locale, |
|
124 * Chronology, ZoneId, and formatting symbols. |
|
125 * <p> |
|
126 * The {@link #withLocale withLocale} method returns a new formatter that |
|
127 * overrides the locale. The locale affects some aspects of formatting and |
|
128 * parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a |
|
129 * formatter that uses the locale specific date format. |
|
130 * <p> |
|
131 * The {@link #withChronology withChronology} method returns a new formatter |
|
132 * that overrides the chronology. If overridden, the date-time value is |
|
133 * converted to the chronology before formatting. During parsing the date-time |
|
134 * value is converted to the chronology before it is returned. |
|
135 * <p> |
|
136 * The {@link #withZone withZone} method returns a new formatter that overrides |
|
137 * the zone. If overridden, the date-time value is converted to a ZonedDateTime |
|
138 * with the requested ZoneId before formatting. During parsing the ZoneId is |
|
139 * applied before the value is returned. |
|
140 * <p> |
|
141 * The {@link #withSymbols withSymbols} method returns a new formatter that |
|
142 * overrides the {@link DateTimeFormatSymbols}. The symbols are used for |
|
143 * formatting and parsing. |
|
144 * <p> |
|
145 * Some applications may need to use the older {@link Format java.text.Format} |
|
146 * class for formatting. The {@link #toFormat()} method returns an |
|
147 * implementation of {@code java.text.Format}. |
|
148 * <p> |
|
149 * <h3 id="predefined">Predefined Formatters</h3> |
|
150 * <table summary="Predefined Formatters" cellpadding="2" cellspacing="3" border="0" > |
|
151 * <thead> |
|
152 * <tr class="tableSubHeadingColor"> |
|
153 * <th class="colFirst" align="left">Formatter</th> |
|
154 * <th class="colFirst" align="left">Description</th> |
|
155 * <th class="colLast" align="left">Example</th> |
|
156 * </tr> |
|
157 * </thead> |
|
158 * <tbody> |
|
159 * <tr class="rowColor"> |
|
160 * <td>{@link #ofLocalizedDate ofLocalizedDate(dateStyle)} </td> |
|
161 * <td> Formatter with date style from the locale </td> |
|
162 * <td> '2011-12-03'</td> |
|
163 * </tr> |
|
164 * <tr class="altColor"> |
|
165 * <td> {@link #ofLocalizedTime ofLocalizedTime(timeStyle)} </td> |
|
166 * <td> Formatter with time style from the locale </td> |
|
167 * <td> '10:15:30'</td> |
|
168 * </tr> |
|
169 * <tr class="rowColor"> |
|
170 * <td> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateTimeStyle)} </td> |
|
171 * <td> Formatter with a style for date and time from the locale</td> |
|
172 * <td> '3 Jun 2008 11:05:30'</td> |
|
173 * </tr> |
|
174 * <tr class="altColor"> |
|
175 * <td> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateStyle,timeStyle)} |
|
176 * </td> |
|
177 * <td> Formatter with date and time styles from the locale </td> |
|
178 * <td> '3 Jun 2008 11:05'</td> |
|
179 * </tr> |
|
180 * <tr class="rowColor"> |
|
181 * <td> {@link #BASIC_ISO_DATE}</td> |
|
182 * <td>Basic ISO date </td> <td>'20111203'</td> |
|
183 * </tr> |
|
184 * <tr class="altColor"> |
|
185 * <td> {@link #ISO_LOCAL_DATE}</td> |
|
186 * <td> ISO Local Date </td> |
|
187 * <td>'2011-12-03'</td> |
|
188 * </tr> |
|
189 * <tr class="rowColor"> |
|
190 * <td> {@link #ISO_OFFSET_DATE}</td> |
|
191 * <td> ISO Date with offset </td> |
|
192 * <td>'2011-12-03+01:00'</td> |
|
193 * </tr> |
|
194 * <tr class="altColor"> |
|
195 * <td> {@link #ISO_DATE}</td> |
|
196 * <td> ISO Date with or without offset </td> |
|
197 * <td> '2011-12-03+01:00'; '2011-12-03'</td> |
|
198 * </tr> |
|
199 * <tr class="rowColor"> |
|
200 * <td> {@link #ISO_LOCAL_TIME}</td> |
|
201 * <td> Time without offset </td> |
|
202 * <td>'10:15:30'</td> |
|
203 * </tr> |
|
204 * <tr class="altColor"> |
|
205 * <td> {@link #ISO_OFFSET_TIME}</td> |
|
206 * <td> Time with offset </td> |
|
207 * <td>'10:15:30+01:00'</td> |
|
208 * </tr> |
|
209 * <tr class="rowColor"> |
|
210 * <td> {@link #ISO_TIME}</td> |
|
211 * <td> Time with or without offset </td> |
|
212 * <td>'10:15:30+01:00'; '10:15:30'</td> |
|
213 * </tr> |
|
214 * <tr class="altColor"> |
|
215 * <td> {@link #ISO_LOCAL_DATE_TIME}</td> |
|
216 * <td> ISO Local Date and Time </td> |
|
217 * <td>'2011-12-03T10:15:30'</td> |
|
218 * </tr> |
|
219 * <tr class="rowColor"> |
|
220 * <td> {@link #ISO_OFFSET_DATE_TIME}</td> |
|
221 * <td> Date Time with Offset |
|
222 * </td><td>2011-12-03T10:15:30+01:00'</td> |
|
223 * </tr> |
|
224 * <tr class="altColor"> |
|
225 * <td> {@link #ISO_ZONED_DATE_TIME}</td> |
|
226 * <td> Zoned Date Time </td> |
|
227 * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td> |
|
228 * </tr> |
|
229 * <tr class="rowColor"> |
|
230 * <td> {@link #ISO_DATE_TIME}</td> |
|
231 * <td> Date and time with ZoneId </td> |
|
232 * <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td> |
|
233 * </tr> |
|
234 * <tr class="altColor"> |
|
235 * <td> {@link #ISO_ORDINAL_DATE}</td> |
|
236 * <td> Year and day of year </td> |
|
237 * <td>'2012-337'</td> |
|
238 * </tr> |
|
239 * <tr class="rowColor"> |
|
240 * <td> {@link #ISO_WEEK_DATE}</td> |
|
241 * <td> Year and Week </td> |
|
242 * <td>2012-W48-6'</td></tr> |
|
243 * <tr class="altColor"> |
|
244 * <td> {@link #ISO_INSTANT}</td> |
|
245 * <td> Date and Time of an Instant </td> |
|
246 * <td>'2011-12-03T10:15:30Z' </td> |
|
247 * </tr> |
|
248 * <tr class="rowColor"> |
|
249 * <td> {@link #RFC_1123_DATE_TIME}</td> |
|
250 * <td> RFC 1123 / RFC 822 </td> |
|
251 * <td>'Tue, 3 Jun 2008 11:05:30 GMT'</td> |
|
252 * </tr> |
|
253 * </tbody> |
|
254 * </table> |
|
255 * |
|
256 * <h3 id="patterns">Patterns for Formatting and Parsing</h3> |
|
257 * Patterns are based on a simple sequence of letters and symbols. |
|
258 * A pattern is used to create a Formatter using the |
|
259 * {@link #ofPattern(String)} and {@link #ofPattern(String, Locale)} methods. |
|
260 * For example, |
|
261 * {@code "d MMM uuuu"} will format 2011-12-03 as '3 Dec 2011'. |
|
262 * A formatter created from a pattern can be used as many times as necessary, |
|
263 * it is immutable and is thread-safe. |
|
264 * <p> |
|
265 * For example: |
|
266 * <blockquote><pre> |
|
267 * DateTimeFormatter formatter = DateTimeFormatter.pattern("yyyy MM dd"); |
|
268 * String text = date.toString(formatter); |
|
269 * LocalDate date = LocalDate.parse(text, formatter); |
|
270 * </pre></blockquote> |
|
271 * <p> |
|
272 * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The |
|
273 * following pattern letters are defined: |
|
274 * <pre> |
|
275 * Symbol Meaning Presentation Examples |
|
276 * ------ ------- ------------ ------- |
|
277 * G era text AD; Anno Domini; A |
|
278 * u year year 2004; 04 |
|
279 * y year-of-era year 2004; 04 |
|
280 * D day-of-year number 189 |
|
281 * M/L month-of-year number/text 7; 07; Jul; July; J |
|
282 * d day-of-month number 10 |
|
283 * |
|
284 * Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter |
|
285 * Y week-based-year year 1996; 96 |
|
286 * w week-of-week-based-year number 27 |
|
287 * W week-of-month number 4 |
|
288 * E day-of-week text Tue; Tuesday; T |
|
289 * e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T |
|
290 * F week-of-month number 3 |
|
291 * |
|
292 * a am-pm-of-day text PM |
|
293 * h clock-hour-of-am-pm (1-12) number 12 |
|
294 * K hour-of-am-pm (0-11) number 0 |
|
295 * k clock-hour-of-am-pm (1-24) number 0 |
|
296 * |
|
297 * H hour-of-day (0-23) number 0 |
|
298 * m minute-of-hour number 30 |
|
299 * s second-of-minute number 55 |
|
300 * S fraction-of-second fraction 978 |
|
301 * A milli-of-day number 1234 |
|
302 * n nano-of-second number 987654321 |
|
303 * N nano-of-day number 1234000000 |
|
304 * |
|
305 * V time-zone ID zone-id America/Los_Angeles; Z; -08:30 |
|
306 * z time-zone name zone-name Pacific Standard Time; PST |
|
307 * O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00; |
|
308 * X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; |
|
309 * x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; |
|
310 * Z zone-offset offset-Z +0000; -0800; -08:00; |
|
311 * |
|
312 * p pad next pad modifier 1 |
|
313 * |
|
314 * ' escape for text delimiter |
|
315 * '' single quote literal ' |
|
316 * [ optional section start |
|
317 * ] optional section end |
|
318 * # reserved for future use |
|
319 * { reserved for future use |
|
320 * } reserved for future use |
114 * </pre> |
321 * </pre> |
115 * Some aspects of formatting and parsing are dependent on the locale. |
322 * <p> |
116 * The locale can be changed using the {@link #withLocale(Locale)} method |
323 * The count of pattern letters determines the format. |
117 * which returns a new formatter in the requested locale. |
324 * <p> |
118 * <p> |
325 * <b>Text</b>: The text style is determined based on the number of pattern |
119 * Some applications may need to use the older {@link Format} class for formatting. |
326 * letters used. Less than 4 pattern letters will use the |
120 * The {@link #toFormat()} method returns an implementation of the old API. |
327 * {@link TextStyle#SHORT short form}. Exactly 4 pattern letters will use the |
|
328 * {@link TextStyle#FULL full form}. Exactly 5 pattern letters will use the |
|
329 * {@link TextStyle#NARROW narrow form}. |
|
330 * Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles. |
|
331 * <p> |
|
332 * <b>Number</b>: If the count of letters is one, then the value is output using |
|
333 * the minimum number of digits and without padding. Otherwise, the count of digits |
|
334 * is used as the width of the output field, with the value zero-padded as necessary. |
|
335 * The following pattern letters have constraints on the count of letters. |
|
336 * Only one letter of 'c' and 'F' can be specified. |
|
337 * Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified. |
|
338 * Up to three letters of 'D' can be specified. |
|
339 * <p> |
|
340 * <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the |
|
341 * Text rules above. Otherwise use the Number rules above. |
|
342 * <p> |
|
343 * <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second. |
|
344 * The nano-of-second value has nine digits, thus the count of pattern letters |
|
345 * is from 1 to 9. If it is less than 9, then the nano-of-second value is |
|
346 * truncated, with only the most significant digits being output. When parsing |
|
347 * in strict mode, the number of parsed digits must match the count of pattern |
|
348 * letters. When parsing in lenient mode, the number of parsed digits must be at |
|
349 * least the count of pattern letters, up to 9 digits. |
|
350 * <p> |
|
351 * <b>Year</b>: The count of letters determines the minimum field width below |
|
352 * which padding is used. If the count of letters is two, then a |
|
353 * {@link DateTimeFormatterBuilder#appendValueReduced reduced} two digit form is |
|
354 * used. For printing, this outputs the rightmost two digits. For parsing, this |
|
355 * will parse using the base value of 2000, resulting in a year within the range |
|
356 * 2000 to 2099 inclusive. If the count of letters is less than four (but not |
|
357 * two), then the sign is only output for negative years as per |
|
358 * {@link SignStyle#NORMAL}. Otherwise, the sign is output if the pad width is |
|
359 * exceeded, as per {@link SignStyle#EXCEEDS_PAD}. |
|
360 * <p> |
|
361 * <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'. If the |
|
362 * count of letters is two, then the time-zone ID is output. Any other count of |
|
363 * letters throws {@code IllegalArgumentException}. |
|
364 * <p> |
|
365 * <b>Zone names</b>: This outputs the display name of the time-zone ID. If the |
|
366 * count of letters is one, two or three, then the short name is output. If the |
|
367 * count of letters is four, then the full name is output. Five or more letters |
|
368 * throws {@code IllegalArgumentException}. |
|
369 * <p> |
|
370 * <b>Offset X and x</b>: This formats the offset based on the number of pattern |
|
371 * letters. One letter outputs just the hour, such as '+01', unless the minute |
|
372 * is non-zero in which case the minute is also output, such as '+0130'. Two |
|
373 * letters outputs the hour and minute, without a colon, such as '+0130'. Three |
|
374 * letters outputs the hour and minute, with a colon, such as '+01:30'. Four |
|
375 * letters outputs the hour and minute and optional second, without a colon, |
|
376 * such as '+013015'. Five letters outputs the hour and minute and optional |
|
377 * second, with a colon, such as '+01:30:15'. Six or more letters throws |
|
378 * {@code IllegalArgumentException}. Pattern letter 'X' (upper case) will output |
|
379 * 'Z' when the offset to be output would be zero, whereas pattern letter 'x' |
|
380 * (lower case) will output '+00', '+0000', or '+00:00'. |
|
381 * <p> |
|
382 * <b>Offset O</b>: This formats the localized offset based on the number of |
|
383 * pattern letters. One letter outputs the {@linkplain TextStyle#SHORT short} |
|
384 * form of the localized offset, which is localized offset text, such as 'GMT', |
|
385 * with hour without leading zero, optional 2-digit minute and second if |
|
386 * non-zero, and colon, for example 'GMT+8'. Four letters outputs the |
|
387 * {@linkplain TextStyle#FULL full} form, which is localized offset text, |
|
388 * such as 'GMT, with 2-digit hour and minute field, optional second field |
|
389 * if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters |
|
390 * throws {@code IllegalArgumentException}. |
|
391 * <p> |
|
392 * <b>Offset Z</b>: This formats the offset based on the number of pattern |
|
393 * letters. One, two or three letters outputs the hour and minute, without a |
|
394 * colon, such as '+0130'. The output will be '+0000' when the offset is zero. |
|
395 * Four letters outputs the {@linkplain TextStyle#FULL full} form of localized |
|
396 * offset, equivalent to four letters of Offset-O. The output will be the |
|
397 * corresponding localized offset text if the offset is zero. Five |
|
398 * letters outputs the hour, minute, with optional second if non-zero, with |
|
399 * colon. It outputs 'Z' if the offset is zero. |
|
400 * Six or more letters throws {@code IllegalArgumentException}. |
|
401 * <p> |
|
402 * <b>Optional section</b>: The optional section markers work exactly like |
|
403 * calling {@link DateTimeFormatterBuilder#optionalStart()} and |
|
404 * {@link DateTimeFormatterBuilder#optionalEnd()}. |
|
405 * <p> |
|
406 * <b>Pad modifier</b>: Modifies the pattern that immediately follows to be |
|
407 * padded with spaces. The pad width is determined by the number of pattern |
|
408 * letters. This is the same as calling |
|
409 * {@link DateTimeFormatterBuilder#padNext(int)}. |
|
410 * <p> |
|
411 * For example, 'ppH' outputs the hour-of-day padded on the left with spaces to |
|
412 * a width of 2. |
|
413 * <p> |
|
414 * Any unrecognized letter is an error. Any non-letter character, other than |
|
415 * '[', ']', '{', '}', '#' and the single quote will be output directly. |
|
416 * Despite this, it is recommended to use single quotes around all characters |
|
417 * that you want to output directly to ensure that future changes do not break |
|
418 * your application. |
121 * |
419 * |
122 * <h3>Specification for implementors</h3> |
420 * <h3>Specification for implementors</h3> |
123 * This class is immutable and thread-safe. |
421 * This class is immutable and thread-safe. |
124 * |
422 * |
125 * @since 1.8 |
423 * @since 1.8 |
149 |
455 |
150 //----------------------------------------------------------------------- |
456 //----------------------------------------------------------------------- |
151 /** |
457 /** |
152 * Creates a formatter using the specified pattern. |
458 * Creates a formatter using the specified pattern. |
153 * <p> |
459 * <p> |
154 * This method will create a formatter based on a simple pattern of letters and symbols. |
460 * This method will create a formatter based on a simple |
155 * For example, {@code d MMM yyyy} will format 2011-12-03 as '3 Dec 2011'. |
461 * <a href="#patterns">pattern of letters and symbols</a> |
156 * <p> |
462 * as described in the class documentation. |
157 * The returned formatter will use the default locale, but this can be changed |
463 * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. |
158 * using {@link DateTimeFormatter#withLocale(Locale)}. |
464 * <p> |
159 * <p> |
465 * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. |
160 * All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. |
466 * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter |
161 * The following pattern letters are defined: |
467 * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. |
162 * <pre> |
468 * <p> |
163 * Symbol Meaning Presentation Examples |
469 * The returned formatter has no override chronology or zone. |
164 * ------ ------- ------------ ------- |
470 * It uses {@link ResolverStyle#SMART SMART} resolver style. |
165 * G era text A; AD; Anno Domini |
|
166 * y year year 2004; 04 |
|
167 * D day-of-year number 189 |
|
168 * M month-of-year number/text 7; 07; Jul; July; J |
|
169 * d day-of-month number 10 |
|
170 * |
|
171 * Q quarter-of-year number/text 3; 03; Q3 |
|
172 * Y week-based-year year 1996; 96 |
|
173 * w week-of-year number 27 |
|
174 * W week-of-month number 27 |
|
175 * e localized day-of-week number 2; Tue; Tuesday; T |
|
176 * E day-of-week number/text 2; Tue; Tuesday; T |
|
177 * F week-of-month number 3 |
|
178 * |
|
179 * a am-pm-of-day text PM |
|
180 * h clock-hour-of-am-pm (1-12) number 12 |
|
181 * K hour-of-am-pm (0-11) number 0 |
|
182 * k clock-hour-of-am-pm (1-24) number 0 |
|
183 * |
|
184 * H hour-of-day (0-23) number 0 |
|
185 * m minute-of-hour number 30 |
|
186 * s second-of-minute number 55 |
|
187 * S fraction-of-second fraction 978 |
|
188 * A milli-of-day number 1234 |
|
189 * n nano-of-second number 987654321 |
|
190 * N nano-of-day number 1234000000 |
|
191 * |
|
192 * V time-zone ID zone-id America/Los_Angeles; Z; -08:30 |
|
193 * z time-zone name zone-name Pacific Standard Time; PST |
|
194 * X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15; |
|
195 * x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15; |
|
196 * Z zone-offset offset-Z +0000; -0800; -08:00; |
|
197 * |
|
198 * p pad next pad modifier 1 |
|
199 * |
|
200 * ' escape for text delimiter |
|
201 * '' single quote literal ' |
|
202 * [ optional section start |
|
203 * ] optional section end |
|
204 * {} reserved for future use |
|
205 * </pre> |
|
206 * <p> |
|
207 * The count of pattern letters determine the format. |
|
208 * <p> |
|
209 * <b>Text</b>: The text style is determined based on the number of pattern letters used. |
|
210 * Less than 4 pattern letters will use the {@link TextStyle#SHORT short form}. |
|
211 * Exactly 4 pattern letters will use the {@link TextStyle#FULL full form}. |
|
212 * Exactly 5 pattern letters will use the {@link TextStyle#NARROW narrow form}. |
|
213 * <p> |
|
214 * <b>Number</b>: If the count of letters is one, then the value is output using the minimum number |
|
215 * of digits and without padding as per {@link DateTimeFormatterBuilder#appendValue(java.time.temporal.TemporalField)}. |
|
216 * Otherwise, the count of digits is used as the width of the output field as per |
|
217 * {@link DateTimeFormatterBuilder#appendValue(java.time.temporal.TemporalField, int)}. |
|
218 * <p> |
|
219 * <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the Text rules above. |
|
220 * Otherwise use the Number rules above. |
|
221 * <p> |
|
222 * <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second. |
|
223 * The nano-of-second value has nine digits, thus the count of pattern letters is from 1 to 9. |
|
224 * If it is less than 9, then the nano-of-second value is truncated, with only the most |
|
225 * significant digits being output. |
|
226 * When parsing in strict mode, the number of parsed digits must match the count of pattern letters. |
|
227 * When parsing in lenient mode, the number of parsed digits must be at least the count of pattern |
|
228 * letters, up to 9 digits. |
|
229 * <p> |
|
230 * <b>Year</b>: The count of letters determines the minimum field width below which padding is used. |
|
231 * If the count of letters is two, then a {@link DateTimeFormatterBuilder#appendValueReduced reduced} |
|
232 * two digit form is used. |
|
233 * For printing, this outputs the rightmost two digits. For parsing, this will parse using the |
|
234 * base value of 2000, resulting in a year within the range 2000 to 2099 inclusive. |
|
235 * If the count of letters is less than four (but not two), then the sign is only output for negative |
|
236 * years as per {@link SignStyle#NORMAL}. |
|
237 * Otherwise, the sign is output if the pad width is exceeded, as per {@link SignStyle#EXCEEDS_PAD} |
|
238 * <p> |
|
239 * <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'. |
|
240 * If the count of letters is two, then the time-zone ID is output. |
|
241 * Any other count of letters throws {@code IllegalArgumentException}. |
|
242 * <p> |
|
243 * <b>Zone names</b>: This outputs the display name of the time-zone ID. |
|
244 * If the count of letters is one, two or three, then the short name is output. |
|
245 * If the count of letters is four, then the full name is output. |
|
246 * Five or more letters throws {@code IllegalArgumentException}. |
|
247 * <p> |
|
248 * <b>Offset X and x</b>: This formats the offset based on the number of pattern letters. |
|
249 * One letter outputs just the hour', such as '+01', unless the minute is non-zero |
|
250 * in which case the minute is also output, such as '+0130'. |
|
251 * Two letters outputs the hour and minute, without a colon, such as '+0130'. |
|
252 * Three letters outputs the hour and minute, with a colon, such as '+01:30'. |
|
253 * Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'. |
|
254 * Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'. |
|
255 * Six or more letters throws {@code IllegalArgumentException}. |
|
256 * Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero, |
|
257 * whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'. |
|
258 * <p> |
|
259 * <b>Offset Z</b>: This formats the offset based on the number of pattern letters. |
|
260 * One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. |
|
261 * Four or more letters throws {@code IllegalArgumentException}. |
|
262 * The output will be '+0000' when the offset is zero. |
|
263 * <p> |
|
264 * <b>Optional section</b>: The optional section markers work exactly like calling |
|
265 * {@link DateTimeFormatterBuilder#optionalStart()} and {@link DateTimeFormatterBuilder#optionalEnd()}. |
|
266 * <p> |
|
267 * <b>Pad modifier</b>: Modifies the pattern that immediately follows to be padded with spaces. |
|
268 * The pad width is determined by the number of pattern letters. |
|
269 * This is the same as calling {@link DateTimeFormatterBuilder#padNext(int)}. |
|
270 * <p> |
|
271 * For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2. |
|
272 * <p> |
|
273 * Any unrecognized letter is an error. |
|
274 * Any non-letter character, other than '[', ']', '{', '}' and the single quote will be output directly. |
|
275 * Despite this, it is recommended to use single quotes around all characters that you want to |
|
276 * output directly to ensure that future changes do not break your application. |
|
277 * |
471 * |
278 * @param pattern the pattern to use, not null |
472 * @param pattern the pattern to use, not null |
279 * @return the formatter based on the pattern, not null |
473 * @return the formatter based on the pattern, not null |
280 * @throws IllegalArgumentException if the pattern is invalid |
474 * @throws IllegalArgumentException if the pattern is invalid |
281 * @see DateTimeFormatterBuilder#appendPattern(String) |
475 * @see DateTimeFormatterBuilder#appendPattern(String) |
416 * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}. |
633 * <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}. |
417 * This is pre-padded by zero to ensure two digits. |
634 * This is pre-padded by zero to ensure two digits. |
418 * <li>A dash |
635 * <li>A dash |
419 * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}. |
636 * <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}. |
420 * This is pre-padded by zero to ensure two digits. |
637 * This is pre-padded by zero to ensure two digits. |
421 * </ul></p> |
638 * </ul> |
|
639 * <p> |
|
640 * The returned formatter has a chronology of ISO set to ensure dates in |
|
641 * other calendar systems are correctly converted. |
|
642 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
422 */ |
643 */ |
423 public static final DateTimeFormatter ISO_LOCAL_DATE; |
644 public static final DateTimeFormatter ISO_LOCAL_DATE; |
424 static { |
645 static { |
425 ISO_LOCAL_DATE = new DateTimeFormatterBuilder() |
646 ISO_LOCAL_DATE = new DateTimeFormatterBuilder() |
426 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) |
647 .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) |
427 .appendLiteral('-') |
648 .appendLiteral('-') |
428 .appendValue(MONTH_OF_YEAR, 2) |
649 .appendValue(MONTH_OF_YEAR, 2) |
429 .appendLiteral('-') |
650 .appendLiteral('-') |
430 .appendValue(DAY_OF_MONTH, 2) |
651 .appendValue(DAY_OF_MONTH, 2) |
431 .toFormatter(); |
652 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
432 } |
653 } |
433 |
654 |
434 //----------------------------------------------------------------------- |
655 //----------------------------------------------------------------------- |
435 /** |
656 /** |
436 * Returns the ISO date formatter that formats or parses a date with an offset, |
657 * The ISO date formatter that formats or parses a date with an |
437 * such as '2011-12-03+01:00'. |
658 * offset, such as '2011-12-03+01:00'. |
438 * <p> |
659 * <p> |
439 * This returns an immutable formatter capable of formatting and parsing |
660 * This returns an immutable formatter capable of formatting and parsing |
440 * the ISO-8601 extended offset date format. |
661 * the ISO-8601 extended offset date format. |
441 * The format consists of: |
662 * The format consists of: |
442 * <p><ul> |
663 * <p><ul> |
443 * <li>The {@link #ISO_LOCAL_DATE} |
664 * <li>The {@link #ISO_LOCAL_DATE} |
444 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
665 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
445 * they will be handled even though this is not part of the ISO-8601 standard. |
666 * they will be handled even though this is not part of the ISO-8601 standard. |
446 * Parsing is case insensitive. |
667 * Parsing is case insensitive. |
447 * </ul></p> |
668 * </ul> |
|
669 * <p> |
|
670 * The returned formatter has a chronology of ISO set to ensure dates in |
|
671 * other calendar systems are correctly converted. |
|
672 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
448 */ |
673 */ |
449 public static final DateTimeFormatter ISO_OFFSET_DATE; |
674 public static final DateTimeFormatter ISO_OFFSET_DATE; |
450 static { |
675 static { |
451 ISO_OFFSET_DATE = new DateTimeFormatterBuilder() |
676 ISO_OFFSET_DATE = new DateTimeFormatterBuilder() |
452 .parseCaseInsensitive() |
677 .parseCaseInsensitive() |
453 .append(ISO_LOCAL_DATE) |
678 .append(ISO_LOCAL_DATE) |
454 .appendOffsetId() |
679 .appendOffsetId() |
455 .toFormatter(); |
680 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
456 } |
681 } |
457 |
682 |
458 //----------------------------------------------------------------------- |
683 //----------------------------------------------------------------------- |
459 /** |
684 /** |
460 * Returns the ISO date formatter that formats or parses a date with the |
685 * The ISO date formatter that formats or parses a date with the |
461 * offset if available, such as '2011-12-03' or '2011-12-03+01:00'. |
686 * offset if available, such as '2011-12-03' or '2011-12-03+01:00'. |
462 * <p> |
687 * <p> |
463 * This returns an immutable formatter capable of formatting and parsing |
688 * This returns an immutable formatter capable of formatting and parsing |
464 * the ISO-8601 extended date format. |
689 * the ISO-8601 extended date format. |
465 * The format consists of: |
690 * The format consists of: |
504 * This is pre-padded by zero to ensure two digits. |
733 * This is pre-padded by zero to ensure two digits. |
505 * <li>If the nano-of-second is zero or not available then the format is complete. |
734 * <li>If the nano-of-second is zero or not available then the format is complete. |
506 * <li>A decimal point |
735 * <li>A decimal point |
507 * <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}. |
736 * <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}. |
508 * As many digits will be output as required. |
737 * As many digits will be output as required. |
509 * </ul></p> |
738 * </ul> |
|
739 * <p> |
|
740 * The returned formatter has no override chronology or zone. |
|
741 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
510 */ |
742 */ |
511 public static final DateTimeFormatter ISO_LOCAL_TIME; |
743 public static final DateTimeFormatter ISO_LOCAL_TIME; |
512 static { |
744 static { |
513 ISO_LOCAL_TIME = new DateTimeFormatterBuilder() |
745 ISO_LOCAL_TIME = new DateTimeFormatterBuilder() |
514 .appendValue(HOUR_OF_DAY, 2) |
746 .appendValue(HOUR_OF_DAY, 2) |
515 .appendLiteral(':') |
747 .appendLiteral(':') |
516 .appendValue(MINUTE_OF_HOUR, 2) |
748 .appendValue(MINUTE_OF_HOUR, 2) |
517 .optionalStart() |
749 .optionalStart() |
518 .appendLiteral(':') |
750 .appendLiteral(':') |
519 .appendValue(SECOND_OF_MINUTE, 2) |
751 .appendValue(SECOND_OF_MINUTE, 2) |
520 .optionalStart() |
752 .optionalStart() |
521 .appendFraction(NANO_OF_SECOND, 0, 9, true) |
753 .appendFraction(NANO_OF_SECOND, 0, 9, true) |
522 .toFormatter(); |
754 .toFormatter(ResolverStyle.STRICT, null); |
523 } |
755 } |
524 |
756 |
525 //----------------------------------------------------------------------- |
757 //----------------------------------------------------------------------- |
526 /** |
758 /** |
527 * Returns the ISO time formatter that formats or parses a time with an offset, |
759 * The ISO time formatter that formats or parses a time with an |
528 * such as '10:15+01:00' or '10:15:30+01:00'. |
760 * offset, such as '10:15+01:00' or '10:15:30+01:00'. |
529 * <p> |
761 * <p> |
530 * This returns an immutable formatter capable of formatting and parsing |
762 * This returns an immutable formatter capable of formatting and parsing |
531 * the ISO-8601 extended offset time format. |
763 * the ISO-8601 extended offset time format. |
532 * The format consists of: |
764 * The format consists of: |
533 * <p><ul> |
765 * <p><ul> |
534 * <li>The {@link #ISO_LOCAL_TIME} |
766 * <li>The {@link #ISO_LOCAL_TIME} |
535 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
767 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
536 * they will be handled even though this is not part of the ISO-8601 standard. |
768 * they will be handled even though this is not part of the ISO-8601 standard. |
537 * Parsing is case insensitive. |
769 * Parsing is case insensitive. |
538 * </ul></p> |
770 * </ul> |
|
771 * <p> |
|
772 * The returned formatter has no override chronology or zone. |
|
773 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
539 */ |
774 */ |
540 public static final DateTimeFormatter ISO_OFFSET_TIME; |
775 public static final DateTimeFormatter ISO_OFFSET_TIME; |
541 static { |
776 static { |
542 ISO_OFFSET_TIME = new DateTimeFormatterBuilder() |
777 ISO_OFFSET_TIME = new DateTimeFormatterBuilder() |
543 .parseCaseInsensitive() |
778 .parseCaseInsensitive() |
544 .append(ISO_LOCAL_TIME) |
779 .append(ISO_LOCAL_TIME) |
545 .appendOffsetId() |
780 .appendOffsetId() |
546 .toFormatter(); |
781 .toFormatter(ResolverStyle.STRICT, null); |
547 } |
782 } |
548 |
783 |
549 //----------------------------------------------------------------------- |
784 //----------------------------------------------------------------------- |
550 /** |
785 /** |
551 * Returns the ISO time formatter that formats or parses a time, with the |
786 * The ISO time formatter that formats or parses a time, with the |
552 * offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'. |
787 * offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'. |
553 * <p> |
788 * <p> |
554 * This returns an immutable formatter capable of formatting and parsing |
789 * This returns an immutable formatter capable of formatting and parsing |
555 * the ISO-8601 extended offset time format. |
790 * the ISO-8601 extended offset time format. |
556 * The format consists of: |
791 * The format consists of: |
558 * <li>The {@link #ISO_LOCAL_TIME} |
793 * <li>The {@link #ISO_LOCAL_TIME} |
559 * <li>If the offset is not available then the format is complete. |
794 * <li>If the offset is not available then the format is complete. |
560 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
795 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
561 * they will be handled even though this is not part of the ISO-8601 standard. |
796 * they will be handled even though this is not part of the ISO-8601 standard. |
562 * Parsing is case insensitive. |
797 * Parsing is case insensitive. |
563 * </ul></p> |
798 * </ul> |
564 * <p> |
799 * <p> |
565 * As this formatter has an optional element, it may be necessary to parse using |
800 * As this formatter has an optional element, it may be necessary to parse using |
566 * {@link DateTimeFormatter#parseBest}. |
801 * {@link DateTimeFormatter#parseBest}. |
|
802 * <p> |
|
803 * The returned formatter has no override chronology or zone. |
|
804 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
567 */ |
805 */ |
568 public static final DateTimeFormatter ISO_TIME; |
806 public static final DateTimeFormatter ISO_TIME; |
569 static { |
807 static { |
570 ISO_TIME = new DateTimeFormatterBuilder() |
808 ISO_TIME = new DateTimeFormatterBuilder() |
571 .parseCaseInsensitive() |
809 .parseCaseInsensitive() |
572 .append(ISO_LOCAL_TIME) |
810 .append(ISO_LOCAL_TIME) |
573 .optionalStart() |
811 .optionalStart() |
574 .appendOffsetId() |
812 .appendOffsetId() |
575 .toFormatter(); |
813 .toFormatter(ResolverStyle.STRICT, null); |
576 } |
814 } |
577 |
815 |
578 //----------------------------------------------------------------------- |
816 //----------------------------------------------------------------------- |
579 /** |
817 /** |
580 * Returns the ISO date formatter that formats or parses a date-time |
818 * The ISO date-time formatter that formats or parses a date-time without |
581 * without an offset, such as '2011-12-03T10:15:30'. |
819 * an offset, such as '2011-12-03T10:15:30'. |
582 * <p> |
820 * <p> |
583 * This returns an immutable formatter capable of formatting and parsing |
821 * This returns an immutable formatter capable of formatting and parsing |
584 * the ISO-8601 extended offset date-time format. |
822 * the ISO-8601 extended offset date-time format. |
585 * The format consists of: |
823 * The format consists of: |
586 * <p><ul> |
824 * <p><ul> |
587 * <li>The {@link #ISO_LOCAL_DATE} |
825 * <li>The {@link #ISO_LOCAL_DATE} |
588 * <li>The letter 'T'. Parsing is case insensitive. |
826 * <li>The letter 'T'. Parsing is case insensitive. |
589 * <li>The {@link #ISO_LOCAL_TIME} |
827 * <li>The {@link #ISO_LOCAL_TIME} |
590 * </ul></p> |
828 * </ul> |
|
829 * <p> |
|
830 * The returned formatter has a chronology of ISO set to ensure dates in |
|
831 * other calendar systems are correctly converted. |
|
832 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
591 */ |
833 */ |
592 public static final DateTimeFormatter ISO_LOCAL_DATE_TIME; |
834 public static final DateTimeFormatter ISO_LOCAL_DATE_TIME; |
593 static { |
835 static { |
594 ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder() |
836 ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder() |
595 .parseCaseInsensitive() |
837 .parseCaseInsensitive() |
596 .append(ISO_LOCAL_DATE) |
838 .append(ISO_LOCAL_DATE) |
597 .appendLiteral('T') |
839 .appendLiteral('T') |
598 .append(ISO_LOCAL_TIME) |
840 .append(ISO_LOCAL_TIME) |
599 .toFormatter(); |
841 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
600 } |
842 } |
601 |
843 |
602 //----------------------------------------------------------------------- |
844 //----------------------------------------------------------------------- |
603 /** |
845 /** |
604 * Returns the ISO date formatter that formats or parses a date-time |
846 * The ISO date-time formatter that formats or parses a date-time with an |
605 * with an offset, such as '2011-12-03T10:15:30+01:00'. |
847 * offset, such as '2011-12-03T10:15:30+01:00'. |
606 * <p> |
848 * <p> |
607 * This returns an immutable formatter capable of formatting and parsing |
849 * This returns an immutable formatter capable of formatting and parsing |
608 * the ISO-8601 extended offset date-time format. |
850 * the ISO-8601 extended offset date-time format. |
609 * The format consists of: |
851 * The format consists of: |
610 * <p><ul> |
852 * <p><ul> |
611 * <li>The {@link #ISO_LOCAL_DATE_TIME} |
853 * <li>The {@link #ISO_LOCAL_DATE_TIME} |
612 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
854 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
613 * they will be handled even though this is not part of the ISO-8601 standard. |
855 * they will be handled even though this is not part of the ISO-8601 standard. |
614 * Parsing is case insensitive. |
856 * Parsing is case insensitive. |
615 * </ul></p> |
857 * </ul> |
|
858 * <p> |
|
859 * The returned formatter has a chronology of ISO set to ensure dates in |
|
860 * other calendar systems are correctly converted. |
|
861 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
616 */ |
862 */ |
617 public static final DateTimeFormatter ISO_OFFSET_DATE_TIME; |
863 public static final DateTimeFormatter ISO_OFFSET_DATE_TIME; |
618 static { |
864 static { |
619 ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder() |
865 ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder() |
620 .parseCaseInsensitive() |
866 .parseCaseInsensitive() |
621 .append(ISO_LOCAL_DATE_TIME) |
867 .append(ISO_LOCAL_DATE_TIME) |
622 .appendOffsetId() |
868 .appendOffsetId() |
623 .toFormatter(); |
869 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
624 } |
870 } |
625 |
871 |
626 //----------------------------------------------------------------------- |
872 //----------------------------------------------------------------------- |
627 /** |
873 /** |
628 * Returns the ISO date formatter that formats or parses a date-time with |
874 * The ISO-like date-time formatter that formats or parses a date-time with |
629 * offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'. |
875 * offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'. |
630 * <p> |
876 * <p> |
631 * This returns an immutable formatter capable of formatting and parsing |
877 * This returns an immutable formatter capable of formatting and parsing |
632 * a format that extends the ISO-8601 extended offset date-time format |
878 * a format that extends the ISO-8601 extended offset date-time format |
633 * to add the time-zone. |
879 * to add the time-zone. |
|
880 * The section in square brackets is not part of the ISO-8601 standard. |
634 * The format consists of: |
881 * The format consists of: |
635 * <p><ul> |
882 * <p><ul> |
636 * <li>The {@link #ISO_OFFSET_DATE_TIME} |
883 * <li>The {@link #ISO_OFFSET_DATE_TIME} |
637 * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete. |
884 * <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete. |
638 * <li>An open square bracket '['. |
885 * <li>An open square bracket '['. |
639 * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard. |
886 * <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard. |
640 * Parsing is case sensitive. |
887 * Parsing is case sensitive. |
641 * <li>A close square bracket ']'. |
888 * <li>A close square bracket ']'. |
642 * </ul></p> |
889 * </ul> |
|
890 * <p> |
|
891 * The returned formatter has a chronology of ISO set to ensure dates in |
|
892 * other calendar systems are correctly converted. |
|
893 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
643 */ |
894 */ |
644 public static final DateTimeFormatter ISO_ZONED_DATE_TIME; |
895 public static final DateTimeFormatter ISO_ZONED_DATE_TIME; |
645 static { |
896 static { |
646 ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder() |
897 ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder() |
647 .append(ISO_OFFSET_DATE_TIME) |
898 .append(ISO_OFFSET_DATE_TIME) |
648 .optionalStart() |
899 .optionalStart() |
649 .appendLiteral('[') |
900 .appendLiteral('[') |
650 .parseCaseSensitive() |
901 .parseCaseSensitive() |
651 .appendZoneRegionId() |
902 .appendZoneRegionId() |
652 .appendLiteral(']') |
903 .appendLiteral(']') |
653 .toFormatter(); |
904 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
654 } |
905 } |
655 |
906 |
656 //----------------------------------------------------------------------- |
907 //----------------------------------------------------------------------- |
657 /** |
908 /** |
658 * Returns the ISO date formatter that formats or parses a date-time |
909 * The ISO-like date-time formatter that formats or parses a date-time with |
659 * with the offset and zone if available, such as '2011-12-03T10:15:30', |
910 * the offset and zone if available, such as '2011-12-03T10:15:30', |
660 * '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. |
911 * '2011-12-03T10:15:30+01:00' or '2011-12-03T10:15:30+01:00[Europe/Paris]'. |
661 * <p> |
912 * <p> |
662 * This returns an immutable formatter capable of formatting and parsing |
913 * This returns an immutable formatter capable of formatting and parsing |
663 * the ISO-8601 extended offset date-time format. |
914 * the ISO-8601 extended local or offset date-time format, as well as the |
|
915 * extended non-ISO form specifying the time-zone. |
664 * The format consists of: |
916 * The format consists of: |
665 * <p><ul> |
917 * <p><ul> |
666 * <li>The {@link #ISO_LOCAL_DATE_TIME} |
918 * <li>The {@link #ISO_LOCAL_DATE_TIME} |
667 * <li>If the offset is not available to format or parse then the format is complete. |
919 * <li>If the offset is not available to format or parse then the format is complete. |
668 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
920 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
748 * The value run from Monday (1) to Sunday (7). |
1008 * The value run from Monday (1) to Sunday (7). |
749 * <li>If the offset is not available to format or parse then the format is complete. |
1009 * <li>If the offset is not available to format or parse then the format is complete. |
750 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
1010 * <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then |
751 * they will be handled even though this is not part of the ISO-8601 standard. |
1011 * they will be handled even though this is not part of the ISO-8601 standard. |
752 * Parsing is case insensitive. |
1012 * Parsing is case insensitive. |
753 * </ul></p> |
1013 * </ul> |
754 * <p> |
1014 * <p> |
755 * As this formatter has an optional element, it may be necessary to parse using |
1015 * As this formatter has an optional element, it may be necessary to parse using |
756 * {@link DateTimeFormatter#parseBest}. |
1016 * {@link DateTimeFormatter#parseBest}. |
|
1017 * <p> |
|
1018 * The returned formatter has a chronology of ISO set to ensure dates in |
|
1019 * other calendar systems are correctly converted. |
|
1020 * It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
757 */ |
1021 */ |
758 public static final DateTimeFormatter ISO_WEEK_DATE; |
1022 public static final DateTimeFormatter ISO_WEEK_DATE; |
759 static { |
1023 static { |
760 ISO_WEEK_DATE = new DateTimeFormatterBuilder() |
1024 ISO_WEEK_DATE = new DateTimeFormatterBuilder() |
761 .parseCaseInsensitive() |
1025 .parseCaseInsensitive() |
762 .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) |
1026 .appendValue(IsoFields.WEEK_BASED_YEAR, 4, 10, SignStyle.EXCEEDS_PAD) |
763 .appendLiteral("-W") |
1027 .appendLiteral("-W") |
764 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2) |
1028 .appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR, 2) |
765 .appendLiteral('-') |
1029 .appendLiteral('-') |
766 .appendValue(DAY_OF_WEEK, 1) |
1030 .appendValue(DAY_OF_WEEK, 1) |
767 .optionalStart() |
1031 .optionalStart() |
768 .appendOffsetId() |
1032 .appendOffsetId() |
769 .toFormatter(); |
1033 .toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE); |
770 } |
1034 } |
771 |
1035 |
772 //----------------------------------------------------------------------- |
1036 //----------------------------------------------------------------------- |
773 /** |
1037 /** |
774 * Returns the ISO instant formatter that formats or parses an instant in UTC. |
1038 * The ISO instant formatter that formats or parses an instant in UTC, |
|
1039 * such as '2011-12-03T10:15:30Z'. |
775 * <p> |
1040 * <p> |
776 * This returns an immutable formatter capable of formatting and parsing |
1041 * This returns an immutable formatter capable of formatting and parsing |
777 * the ISO-8601 instant format. |
1042 * the ISO-8601 instant format. |
|
1043 * <p> |
|
1044 * This is a special case formatter intended to allow a human readable form |
|
1045 * of an {@link java.time.Instant}. The {@code Instant} class is designed to |
|
1046 * only represent a point in time and internally stores a value in nanoseconds |
|
1047 * from a fixed epoch of 1970-01-01Z. As such, an {@code Instant} cannot be |
|
1048 * formatted as a date or time without providing some form of time-zone. |
|
1049 * This formatter allows the {@code Instant} to be formatted, by providing |
|
1050 * a suitable conversion using {@code ZoneOffset.UTC}. |
|
1051 * <p> |
778 * The format consists of: |
1052 * The format consists of: |
779 * <p><ul> |
1053 * <p><ul> |
780 * <li>The {@link #ISO_OFFSET_DATE_TIME} where the instant is converted from |
1054 * <li>The {@link #ISO_OFFSET_DATE_TIME} where the instant is converted from |
781 * {@link ChronoField#INSTANT_SECONDS} and {@link ChronoField#NANO_OF_SECOND} |
1055 * {@link ChronoField#INSTANT_SECONDS} and {@link ChronoField#NANO_OF_SECOND} |
782 * using the {@code UTC} offset. Parsing is case insensitive. |
1056 * using the {@code UTC} offset. Parsing is case insensitive. |
783 * </ul></p> |
1057 * </ul> |
|
1058 * <p> |
|
1059 * The returned formatter has no override chronology or zone. |
|
1060 * It uses the {@link ResolverStyle#STRICT STRICT} resolver style. |
784 */ |
1061 */ |
785 public static final DateTimeFormatter ISO_INSTANT; |
1062 public static final DateTimeFormatter ISO_INSTANT; |
786 static { |
1063 static { |
787 ISO_INSTANT = new DateTimeFormatterBuilder() |
1064 ISO_INSTANT = new DateTimeFormatterBuilder() |
788 .parseCaseInsensitive() |
1065 .parseCaseInsensitive() |
789 .appendInstant() |
1066 .appendInstant() |
790 .toFormatter(); |
1067 .toFormatter(ResolverStyle.STRICT, null); |
791 } |
1068 } |
792 |
1069 |
793 //----------------------------------------------------------------------- |
1070 //----------------------------------------------------------------------- |
794 /** |
1071 /** |
795 * Returns the ISO date formatter that formats or parses a date without an offset, |
1072 * The ISO date formatter that formats or parses a date without an |
796 * such as '20111203'. |
1073 * offset, such as '20111203'. |
797 * <p> |
1074 * <p> |
798 * This returns an immutable formatter capable of formatting and parsing |
1075 * This returns an immutable formatter capable of formatting and parsing |
799 * the ISO-8601 basic local date format. |
1076 * the ISO-8601 basic local date format. |
800 * The format consists of: |
1077 * The format consists of: |
801 * <p><ul> |
1078 * <p><ul> |
890 moy.put(9L, "Sep"); |
1175 moy.put(9L, "Sep"); |
891 moy.put(10L, "Oct"); |
1176 moy.put(10L, "Oct"); |
892 moy.put(11L, "Nov"); |
1177 moy.put(11L, "Nov"); |
893 moy.put(12L, "Dec"); |
1178 moy.put(12L, "Dec"); |
894 RFC_1123_DATE_TIME = new DateTimeFormatterBuilder() |
1179 RFC_1123_DATE_TIME = new DateTimeFormatterBuilder() |
895 .parseCaseInsensitive() |
1180 .parseCaseInsensitive() |
896 .parseLenient() |
1181 .parseLenient() |
897 .optionalStart() |
1182 .optionalStart() |
898 .appendText(DAY_OF_WEEK, dow) |
1183 .appendText(DAY_OF_WEEK, dow) |
899 .appendLiteral(", ") |
1184 .appendLiteral(", ") |
900 .optionalEnd() |
1185 .optionalEnd() |
901 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) |
1186 .appendValue(DAY_OF_MONTH, 1, 2, SignStyle.NOT_NEGATIVE) |
902 .appendLiteral(' ') |
1187 .appendLiteral(' ') |
903 .appendText(MONTH_OF_YEAR, moy) |
1188 .appendText(MONTH_OF_YEAR, moy) |
904 .appendLiteral(' ') |
1189 .appendLiteral(' ') |
905 .appendValue(YEAR, 4) // 2 digit year not handled |
1190 .appendValue(YEAR, 4) // 2 digit year not handled |
906 .appendLiteral(' ') |
1191 .appendLiteral(' ') |
907 .appendValue(HOUR_OF_DAY, 2) |
1192 .appendValue(HOUR_OF_DAY, 2) |
908 .appendLiteral(':') |
1193 .appendLiteral(':') |
909 .appendValue(MINUTE_OF_HOUR, 2) |
1194 .appendValue(MINUTE_OF_HOUR, 2) |
910 .optionalStart() |
1195 .optionalStart() |
911 .appendLiteral(':') |
1196 .appendLiteral(':') |
912 .appendValue(SECOND_OF_MINUTE, 2) |
1197 .appendValue(SECOND_OF_MINUTE, 2) |
913 .optionalEnd() |
1198 .optionalEnd() |
914 .appendLiteral(' ') |
1199 .appendLiteral(' ') |
915 .appendOffset("+HHMM", "GMT") // should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT |
1200 .appendOffset("+HHMM", "GMT") // should handle UT/Z/EST/EDT/CST/CDT/MST/MDT/PST/MDT |
916 .toFormatter(); |
1201 .toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE); |
917 } |
1202 } |
918 |
1203 |
919 /** |
1204 /** |
920 * Constructor. |
1205 * Constructor. |
921 * |
1206 * |
922 * @param printerParser the printer/parser to use, not null |
1207 * @param printerParser the printer/parser to use, not null |
923 * @param locale the locale to use, not null |
1208 * @param locale the locale to use, not null |
924 * @param symbols the symbols to use, not null |
1209 * @param symbols the symbols to use, not null |
|
1210 * @param resolverStyle the resolver style to use, not null |
|
1211 * @param resolverFields the fields to use during resolving, null for all fields |
925 * @param chrono the chronology to use, null for no override |
1212 * @param chrono the chronology to use, null for no override |
926 * @param zone the zone to use, null for no override |
1213 * @param zone the zone to use, null for no override |
927 */ |
1214 */ |
928 DateTimeFormatter(CompositePrinterParser printerParser, Locale locale, |
1215 DateTimeFormatter(CompositePrinterParser printerParser, |
929 DateTimeFormatSymbols symbols, Chronology chrono, ZoneId zone) { |
1216 Locale locale, DateTimeFormatSymbols symbols, |
|
1217 ResolverStyle resolverStyle, Set<TemporalField> resolverFields, |
|
1218 Chronology chrono, ZoneId zone) { |
930 this.printerParser = Objects.requireNonNull(printerParser, "printerParser"); |
1219 this.printerParser = Objects.requireNonNull(printerParser, "printerParser"); |
|
1220 this.resolverFields = resolverFields; |
931 this.locale = Objects.requireNonNull(locale, "locale"); |
1221 this.locale = Objects.requireNonNull(locale, "locale"); |
932 this.symbols = Objects.requireNonNull(symbols, "symbols"); |
1222 this.symbols = Objects.requireNonNull(symbols, "symbols"); |
|
1223 this.resolverStyle = Objects.requireNonNull(resolverStyle, "resolverStyle"); |
933 this.chrono = chrono; |
1224 this.chrono = chrono; |
934 this.zone = zone; |
1225 this.zone = zone; |
935 } |
1226 } |
936 |
1227 |
937 //----------------------------------------------------------------------- |
1228 //----------------------------------------------------------------------- |
1011 * with the override chronology set. |
1302 * with the override chronology set. |
1012 * By default, a formatter has no override chronology, returning null. |
1303 * By default, a formatter has no override chronology, returning null. |
1013 * <p> |
1304 * <p> |
1014 * If an override is added, then any date that is formatted or parsed will be affected. |
1305 * If an override is added, then any date that is formatted or parsed will be affected. |
1015 * <p> |
1306 * <p> |
1016 * When formatting, if the {@code Temporal} object contains a date then it will |
1307 * When formatting, if the temporal object contains a date, then it will |
1017 * be converted to a date in the override chronology. |
1308 * be converted to a date in the override chronology. |
1018 * Any time or zone will be retained unless overridden. |
1309 * Whether the temporal contains a date is determined by querying the |
1019 * The converted result will behave in a manner equivalent to an implementation |
1310 * {@link ChronoField#EPOCH_DAY EPOCH_DAY} field. |
1020 * of {@code ChronoLocalDate},{@code ChronoLocalDateTime} or {@code ChronoZonedDateTime}. |
1311 * Any time or zone will be retained unaltered unless overridden. |
1021 * <p> |
1312 * <p> |
1022 * When parsing, the override chronology will be used to interpret the |
1313 * If the temporal object does not contain a date, but does contain one |
1023 * {@link java.time.temporal.ChronoField fields} into a date unless the |
1314 * or more {@code ChronoField} date fields, then a {@code DateTimeException} |
1024 * formatter directly parses a valid chronology. |
1315 * is thrown. In all other cases, the override chronology is added to the temporal, |
|
1316 * replacing any previous chronology, but without changing the date/time. |
|
1317 * <p> |
|
1318 * When parsing, there are two distinct cases to consider. |
|
1319 * If a chronology has been parsed directly from the text, perhaps because |
|
1320 * {@link DateTimeFormatterBuilder#appendChronologyId()} was used, then |
|
1321 * this override chronology has no effect. |
|
1322 * If no zone has been parsed, then this override chronology will be used |
|
1323 * to interpret the {@code ChronoField} values into a date according to the |
|
1324 * date resolving rules of the chronology. |
1025 * <p> |
1325 * <p> |
1026 * This instance is immutable and unaffected by this method call. |
1326 * This instance is immutable and unaffected by this method call. |
1027 * |
1327 * |
1028 * @param chrono the new chronology, not null |
1328 * @param chrono the new chronology, null if no override |
1029 * @return a formatter based on this formatter with the requested override chronology, not null |
1329 * @return a formatter based on this formatter with the requested override chronology, not null |
1030 */ |
1330 */ |
1031 public DateTimeFormatter withChronology(Chronology chrono) { |
1331 public DateTimeFormatter withChronology(Chronology chrono) { |
1032 if (Objects.equals(this.chrono, chrono)) { |
1332 if (Objects.equals(this.chrono, chrono)) { |
1033 return this; |
1333 return this; |
1034 } |
1334 } |
1035 return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone); |
1335 return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone); |
1036 } |
1336 } |
1037 |
1337 |
1038 //----------------------------------------------------------------------- |
1338 //----------------------------------------------------------------------- |
1039 /** |
1339 /** |
1040 * Gets the overriding zone to be used during formatting. |
1340 * Gets the overriding zone to be used during formatting. |
1041 * <p> |
1341 * <p> |
1042 * This returns the override zone, used to convert instants. |
1342 * This returns the override zone, used to convert instants. |
1043 * By default, a formatter has no override zone, returning null. |
1343 * By default, a formatter has no override zone, returning null. |
1044 * See {@link #withZone(ZoneId)} for more details on overriding. |
1344 * See {@link #withZone(ZoneId)} for more details on overriding. |
1045 * |
1345 * |
1046 * @return the chronology of this formatter, null if no override |
1346 * @return the override zone of this formatter, null if no override |
1047 */ |
1347 */ |
1048 public ZoneId getZone() { |
1348 public ZoneId getZone() { |
1049 return zone; |
1349 return zone; |
1050 } |
1350 } |
1051 |
1351 |
1056 * with the override zone set. |
1356 * with the override zone set. |
1057 * By default, a formatter has no override zone, returning null. |
1357 * By default, a formatter has no override zone, returning null. |
1058 * <p> |
1358 * <p> |
1059 * If an override is added, then any instant that is formatted or parsed will be affected. |
1359 * If an override is added, then any instant that is formatted or parsed will be affected. |
1060 * <p> |
1360 * <p> |
1061 * When formatting, if the {@code Temporal} object contains an instant then it will |
1361 * When formatting, if the temporal object contains an instant, then it will |
1062 * be converted to a zoned date-time using the override zone. |
1362 * be converted to a zoned date-time using the override zone. |
|
1363 * Whether the temporal is an instant is determined by querying the |
|
1364 * {@link ChronoField#INSTANT_SECONDS INSTANT_SECONDS} field. |
1063 * If the input has a chronology then it will be retained unless overridden. |
1365 * If the input has a chronology then it will be retained unless overridden. |
1064 * If the input does not have a chronology, such as {@code Instant}, then |
1366 * If the input does not have a chronology, such as {@code Instant}, then |
1065 * the ISO chronology will be used. |
1367 * the ISO chronology will be used. |
1066 * The converted result will behave in a manner equivalent to an implementation |
1368 * <p> |
1067 * of {@code ChronoZonedDateTime}. |
1369 * If the temporal object does not contain an instant, but does contain |
1068 * <p> |
1370 * an offset then an additional check is made. If the normalized override |
1069 * When parsing, the override zone will be used to interpret the |
1371 * zone is an offset that differs from the offset of the temporal, then |
1070 * {@link java.time.temporal.ChronoField fields} into an instant unless the |
1372 * a {@code DateTimeException} is thrown. In all other cases, the override |
1071 * formatter directly parses a valid zone. |
1373 * zone is added to the temporal, replacing any previous zone, but without |
|
1374 * changing the date/time. |
|
1375 * <p> |
|
1376 * When parsing, there are two distinct cases to consider. |
|
1377 * If a zone has been parsed directly from the text, perhaps because |
|
1378 * {@link DateTimeFormatterBuilder#appendZoneId()} was used, then |
|
1379 * this override zone has no effect. |
|
1380 * If no zone has been parsed, then this override zone will be included in |
|
1381 * the result of the parse where it can be used to build instants and date-times. |
1072 * <p> |
1382 * <p> |
1073 * This instance is immutable and unaffected by this method call. |
1383 * This instance is immutable and unaffected by this method call. |
1074 * |
1384 * |
1075 * @param zone the new override zone, not null |
1385 * @param zone the new override zone, null if no override |
1076 * @return a formatter based on this formatter with the requested override zone, not null |
1386 * @return a formatter based on this formatter with the requested override zone, not null |
1077 */ |
1387 */ |
1078 public DateTimeFormatter withZone(ZoneId zone) { |
1388 public DateTimeFormatter withZone(ZoneId zone) { |
1079 if (Objects.equals(this.zone, zone)) { |
1389 if (Objects.equals(this.zone, zone)) { |
1080 return this; |
1390 return this; |
1081 } |
1391 } |
1082 return new DateTimeFormatter(printerParser, locale, symbols, chrono, zone); |
1392 return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone); |
|
1393 } |
|
1394 |
|
1395 //----------------------------------------------------------------------- |
|
1396 /** |
|
1397 * Gets the resolver style to use during parsing. |
|
1398 * <p> |
|
1399 * This returns the resolver style, used during the second phase of parsing |
|
1400 * when fields are resolved into dates and times. |
|
1401 * By default, a formatter has the {@link ResolverStyle#SMART SMART} resolver style. |
|
1402 * See {@link #withResolverStyle(ResolverStyle)} for more details. |
|
1403 * |
|
1404 * @return the resolver style of this formatter, not null |
|
1405 */ |
|
1406 public ResolverStyle getResolverStyle() { |
|
1407 return resolverStyle; |
|
1408 } |
|
1409 |
|
1410 /** |
|
1411 * Returns a copy of this formatter with a new resolver style. |
|
1412 * <p> |
|
1413 * This returns a formatter with similar state to this formatter but |
|
1414 * with the resolver style set. By default, a formatter has the |
|
1415 * {@link ResolverStyle#SMART SMART} resolver style. |
|
1416 * <p> |
|
1417 * Changing the resolver style only has an effect during parsing. |
|
1418 * Parsing a text string occurs in two phases. |
|
1419 * Phase 1 is a basic text parse according to the fields added to the builder. |
|
1420 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. |
|
1421 * The resolver style is used to control how phase 2, resolving, happens. |
|
1422 * See {@code ResolverStyle} for more information on the options available. |
|
1423 * <p> |
|
1424 * This instance is immutable and unaffected by this method call. |
|
1425 * |
|
1426 * @param resolverStyle the new resolver style, not null |
|
1427 * @return a formatter based on this formatter with the requested resolver style, not null |
|
1428 */ |
|
1429 public DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle) { |
|
1430 Objects.requireNonNull(resolverStyle, "resolverStyle"); |
|
1431 if (Objects.equals(this.resolverStyle, resolverStyle)) { |
|
1432 return this; |
|
1433 } |
|
1434 return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone); |
|
1435 } |
|
1436 |
|
1437 //----------------------------------------------------------------------- |
|
1438 /** |
|
1439 * Gets the resolver fields to use during parsing. |
|
1440 * <p> |
|
1441 * This returns the resolver fields, used during the second phase of parsing |
|
1442 * when fields are resolved into dates and times. |
|
1443 * By default, a formatter has no resolver fields, and thus returns null. |
|
1444 * See {@link #withResolverFields(Set)} for more details. |
|
1445 * |
|
1446 * @return the immutable set of resolver fields of this formatter, null if no fields |
|
1447 */ |
|
1448 public Set<TemporalField> getResolverFields() { |
|
1449 return resolverFields; |
|
1450 } |
|
1451 |
|
1452 /** |
|
1453 * Returns a copy of this formatter with a new set of resolver fields. |
|
1454 * <p> |
|
1455 * This returns a formatter with similar state to this formatter but with |
|
1456 * the resolver fields set. By default, a formatter has no resolver fields. |
|
1457 * <p> |
|
1458 * Changing the resolver fields only has an effect during parsing. |
|
1459 * Parsing a text string occurs in two phases. |
|
1460 * Phase 1 is a basic text parse according to the fields added to the builder. |
|
1461 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. |
|
1462 * The resolver fields are used to filter the field-value pairs between phase 1 and 2. |
|
1463 * <p> |
|
1464 * This can be used to select between two or more ways that a date or time might |
|
1465 * be resolved. For example, if the formatter consists of year, month, day-of-month |
|
1466 * and day-of-year, then there are two ways to resolve a date. |
|
1467 * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and |
|
1468 * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is |
|
1469 * resolved using the year and day-of-year, effectively meaning that the month |
|
1470 * and day-of-month are ignored during the resolving phase. |
|
1471 * <p> |
|
1472 * In a similar manner, this method can be used to ignore secondary fields that |
|
1473 * would otherwise be cross-checked. For example, if the formatter consists of year, |
|
1474 * month, day-of-month and day-of-week, then there is only one way to resolve a |
|
1475 * date, but the parsed value for day-of-week will be cross-checked against the |
|
1476 * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR}, |
|
1477 * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and |
|
1478 * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is |
|
1479 * resolved correctly, but without any cross-check for the day-of-week. |
|
1480 * <p> |
|
1481 * In implementation terms, this method behaves as follows. The result of the |
|
1482 * parsing phase can be considered to be a map of field to value. The behavior |
|
1483 * of this method is to cause that map to be filtered between phase 1 and 2, |
|
1484 * removing all fields other than those specified as arguments to this method. |
|
1485 * <p> |
|
1486 * This instance is immutable and unaffected by this method call. |
|
1487 * |
|
1488 * @param resolverFields the new set of resolver fields, null if no fields |
|
1489 * @return a formatter based on this formatter with the requested resolver style, not null |
|
1490 */ |
|
1491 public DateTimeFormatter withResolverFields(TemporalField... resolverFields) { |
|
1492 Objects.requireNonNull(resolverFields, "resolverFields"); |
|
1493 Set<TemporalField> fields = new HashSet<>(Arrays.asList(resolverFields)); |
|
1494 if (Objects.equals(this.resolverFields, fields)) { |
|
1495 return this; |
|
1496 } |
|
1497 fields = Collections.unmodifiableSet(fields); |
|
1498 return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, fields, chrono, zone); |
|
1499 } |
|
1500 |
|
1501 /** |
|
1502 * Returns a copy of this formatter with a new set of resolver fields. |
|
1503 * <p> |
|
1504 * This returns a formatter with similar state to this formatter but with |
|
1505 * the resolver fields set. By default, a formatter has no resolver fields. |
|
1506 * <p> |
|
1507 * Changing the resolver fields only has an effect during parsing. |
|
1508 * Parsing a text string occurs in two phases. |
|
1509 * Phase 1 is a basic text parse according to the fields added to the builder. |
|
1510 * Phase 2 resolves the parsed field-value pairs into date and/or time objects. |
|
1511 * The resolver fields are used to filter the field-value pairs between phase 1 and 2. |
|
1512 * <p> |
|
1513 * This can be used to select between two or more ways that a date or time might |
|
1514 * be resolved. For example, if the formatter consists of year, month, day-of-month |
|
1515 * and day-of-year, then there are two ways to resolve a date. |
|
1516 * Calling this method with the arguments {@link ChronoField#YEAR YEAR} and |
|
1517 * {@link ChronoField#DAY_OF_YEAR DAY_OF_YEAR} will ensure that the date is |
|
1518 * resolved using the year and day-of-year, effectively meaning that the month |
|
1519 * and day-of-month are ignored during the resolving phase. |
|
1520 * <p> |
|
1521 * In a similar manner, this method can be used to ignore secondary fields that |
|
1522 * would otherwise be cross-checked. For example, if the formatter consists of year, |
|
1523 * month, day-of-month and day-of-week, then there is only one way to resolve a |
|
1524 * date, but the parsed value for day-of-week will be cross-checked against the |
|
1525 * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR}, |
|
1526 * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and |
|
1527 * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is |
|
1528 * resolved correctly, but without any cross-check for the day-of-week. |
|
1529 * <p> |
|
1530 * In implementation terms, this method behaves as follows. The result of the |
|
1531 * parsing phase can be considered to be a map of field to value. The behavior |
|
1532 * of this method is to cause that map to be filtered between phase 1 and 2, |
|
1533 * removing all fields other than those specified as arguments to this method. |
|
1534 * <p> |
|
1535 * This instance is immutable and unaffected by this method call. |
|
1536 * |
|
1537 * @param resolverFields the new set of resolver fields, null if no fields |
|
1538 * @return a formatter based on this formatter with the requested resolver style, not null |
|
1539 */ |
|
1540 public DateTimeFormatter withResolverFields(Set<TemporalField> resolverFields) { |
|
1541 Objects.requireNonNull(resolverFields, "resolverFields"); |
|
1542 if (Objects.equals(this.resolverFields, resolverFields)) { |
|
1543 return this; |
|
1544 } |
|
1545 resolverFields = Collections.unmodifiableSet(new HashSet<>(resolverFields)); |
|
1546 return new DateTimeFormatter(printerParser, locale, symbols, resolverStyle, resolverFields, chrono, zone); |
1083 } |
1547 } |
1084 |
1548 |
1085 //----------------------------------------------------------------------- |
1549 //----------------------------------------------------------------------- |
1086 /** |
1550 /** |
1087 * Formats a date-time object using this formatter. |
1551 * Formats a date-time object using this formatter. |