This repository has been archived by the owner on Mar 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathDateTimeFormatter.java
2262 lines (2201 loc) · 106 KB
/
DateTimeFormatter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* This file is available under and governed by the GNU General Public
* License version 2 only, as published by the Free Software Foundation.
* However, the following notice accompanied the original version of this
* file:
*
* Copyright (c) 2008-2012, Stephen Colebourne & Michael Nascimento Santos
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of JSR-310 nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package java.time.format;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
import static java.time.temporal.ChronoField.DAY_OF_YEAR;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.YEAR;
import java.io.IOException;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.time.DateTimeException;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.Chronology;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatterBuilder.CompositePrinterParser;
import java.time.temporal.ChronoField;
import java.time.temporal.IsoFields;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;
import java.time.temporal.TemporalQuery;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import sun.util.locale.provider.TimeZoneNameUtility;
/**
* Formatter for printing and parsing date-time objects.
* <p>
* This class provides the main application entry point for printing and parsing
* and provides common implementations of {@code DateTimeFormatter}:
* <ul>
* <li>Using predefined constants, such as {@link #ISO_LOCAL_DATE}</li>
* <li>Using pattern letters, such as {@code uuuu-MMM-dd}</li>
* <li>Using localized styles, such as {@code long} or {@code medium}</li>
* </ul>
* <p>
* More complex formatters are provided by
* {@link DateTimeFormatterBuilder DateTimeFormatterBuilder}.
*
* <p>
* The main date-time classes provide two methods - one for formatting,
* {@code format(DateTimeFormatter formatter)}, and one for parsing,
* {@code parse(CharSequence text, DateTimeFormatter formatter)}.
* <p>For example:
* <blockquote><pre>
* LocalDate date = LocalDate.now();
* String text = date.format(formatter);
* LocalDate parsedDate = LocalDate.parse(text, formatter);
* </pre></blockquote>
* <p>
* In addition to the format, formatters can be created with desired Locale,
* Chronology, ZoneId, and DecimalStyle.
* <p>
* The {@link #withLocale withLocale} method returns a new formatter that
* overrides the locale. The locale affects some aspects of formatting and
* parsing. For example, the {@link #ofLocalizedDate ofLocalizedDate} provides a
* formatter that uses the locale specific date format.
* <p>
* The {@link #withChronology withChronology} method returns a new formatter
* that overrides the chronology. If overridden, the date-time value is
* converted to the chronology before formatting. During parsing the date-time
* value is converted to the chronology before it is returned.
* <p>
* The {@link #withZone withZone} method returns a new formatter that overrides
* the zone. If overridden, the date-time value is converted to a ZonedDateTime
* with the requested ZoneId before formatting. During parsing the ZoneId is
* applied before the value is returned.
* <p>
* The {@link #withDecimalStyle withDecimalStyle} method returns a new formatter that
* overrides the {@link DecimalStyle}. The DecimalStyle symbols are used for
* formatting and parsing.
* <p>
* Some applications may need to use the older {@link Format java.text.Format}
* class for formatting. The {@link #toFormat()} method returns an
* implementation of {@code java.text.Format}.
*
* <h3 id="predefined">Predefined Formatters</h3>
* <table class="striped" style="text-align:left">
* <caption>Predefined Formatters</caption>
* <thead>
* <tr>
* <th scope="col">Formatter</th>
* <th scope="col">Description</th>
* <th scope="col">Example</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <th scope="row">{@link #ofLocalizedDate ofLocalizedDate(dateStyle)} </th>
* <td> Formatter with date style from the locale </td>
* <td> '2011-12-03'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ofLocalizedTime ofLocalizedTime(timeStyle)} </th>
* <td> Formatter with time style from the locale </td>
* <td> '10:15:30'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateTimeStyle)} </th>
* <td> Formatter with a style for date and time from the locale</td>
* <td> '3 Jun 2008 11:05:30'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ofLocalizedDateTime ofLocalizedDateTime(dateStyle,timeStyle)}
* </th>
* <td> Formatter with date and time styles from the locale </td>
* <td> '3 Jun 2008 11:05'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #BASIC_ISO_DATE}</th>
* <td>Basic ISO date </td> <td>'20111203'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_LOCAL_DATE}</th>
* <td> ISO Local Date </td>
* <td>'2011-12-03'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_OFFSET_DATE}</th>
* <td> ISO Date with offset </td>
* <td>'2011-12-03+01:00'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_DATE}</th>
* <td> ISO Date with or without offset </td>
* <td> '2011-12-03+01:00'; '2011-12-03'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_LOCAL_TIME}</th>
* <td> Time without offset </td>
* <td>'10:15:30'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_OFFSET_TIME}</th>
* <td> Time with offset </td>
* <td>'10:15:30+01:00'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_TIME}</th>
* <td> Time with or without offset </td>
* <td>'10:15:30+01:00'; '10:15:30'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_LOCAL_DATE_TIME}</th>
* <td> ISO Local Date and Time </td>
* <td>'2011-12-03T10:15:30'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_OFFSET_DATE_TIME}</th>
* <td> Date Time with Offset
* </td><td>'2011-12-03T10:15:30+01:00'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_ZONED_DATE_TIME}</th>
* <td> Zoned Date Time </td>
* <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_DATE_TIME}</th>
* <td> Date and time with ZoneId </td>
* <td>'2011-12-03T10:15:30+01:00[Europe/Paris]'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_ORDINAL_DATE}</th>
* <td> Year and day of year </td>
* <td>'2012-337'</td>
* </tr>
* <tr>
* <th scope="row"> {@link #ISO_WEEK_DATE}</th>
* <td> Year and Week </td>
* <td>'2012-W48-6'</td></tr>
* <tr>
* <th scope="row"> {@link #ISO_INSTANT}</th>
* <td> Date and Time of an Instant </td>
* <td>'2011-12-03T10:15:30Z' </td>
* </tr>
* <tr>
* <th scope="row"> {@link #RFC_1123_DATE_TIME}</th>
* <td> RFC 1123 / RFC 822 </td>
* <td>'Tue, 3 Jun 2008 11:05:30 GMT'</td>
* </tr>
* </tbody>
* </table>
*
* <h3 id="patterns">Patterns for Formatting and Parsing</h3>
* Patterns are based on a simple sequence of letters and symbols.
* A pattern is used to create a Formatter using the
* {@link #ofPattern(String)} and {@link #ofPattern(String, Locale)} methods.
* For example,
* {@code "d MMM uuuu"} will format 2011-12-03 as '3 Dec 2011'.
* A formatter created from a pattern can be used as many times as necessary,
* it is immutable and is thread-safe.
* <p>
* For example:
* <blockquote><pre>
* LocalDate date = LocalDate.now();
* DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
* String text = date.format(formatter);
* LocalDate parsedDate = LocalDate.parse(text, formatter);
* </pre></blockquote>
* <p>
* All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters. The
* following pattern letters are defined:
* <table class="striped">
* <caption>Pattern Letters and Symbols</caption>
* <thead>
* <tr><th scope="col">Symbol</th> <th scope="col">Meaning</th> <th scope="col">Presentation</th> <th scope="col">Examples</th>
* </thead>
* <tbody>
* <tr><th scope="row">G</th> <td>era</td> <td>text</td> <td>AD; Anno Domini; A</td>
* <tr><th scope="row">u</th> <td>year</td> <td>year</td> <td>2004; 04</td>
* <tr><th scope="row">y</th> <td>year-of-era</td> <td>year</td> <td>2004; 04</td>
* <tr><th scope="row">D</th> <td>day-of-year</td> <td>number</td> <td>189</td>
* <tr><th scope="row">M/L</th> <td>month-of-year</td> <td>number/text</td> <td>7; 07; Jul; July; J</td>
* <tr><th scope="row">d</th> <td>day-of-month</td> <td>number</td> <td>10</td>
* <tr><th scope="row">g</th> <td>modified-julian-day</td> <td>number</td> <td>2451334</td>
*
* <tr><th scope="row">Q/q</th> <td>quarter-of-year</td> <td>number/text</td> <td>3; 03; Q3; 3rd quarter</td>
* <tr><th scope="row">Y</th> <td>week-based-year</td> <td>year</td> <td>1996; 96</td>
* <tr><th scope="row">w</th> <td>week-of-week-based-year</td> <td>number</td> <td>27</td>
* <tr><th scope="row">W</th> <td>week-of-month</td> <td>number</td> <td>4</td>
* <tr><th scope="row">E</th> <td>day-of-week</td> <td>text</td> <td>Tue; Tuesday; T</td>
* <tr><th scope="row">e/c</th> <td>localized day-of-week</td> <td>number/text</td> <td>2; 02; Tue; Tuesday; T</td>
* <tr><th scope="row">F</th> <td>day-of-week-in-month</td> <td>number</td> <td>3</td>
*
* <tr><th scope="row">a</th> <td>am-pm-of-day</td> <td>text</td> <td>PM</td>
* <tr><th scope="row">h</th> <td>clock-hour-of-am-pm (1-12)</td> <td>number</td> <td>12</td>
* <tr><th scope="row">K</th> <td>hour-of-am-pm (0-11)</td> <td>number</td> <td>0</td>
* <tr><th scope="row">k</th> <td>clock-hour-of-day (1-24)</td> <td>number</td> <td>24</td>
*
* <tr><th scope="row">H</th> <td>hour-of-day (0-23)</td> <td>number</td> <td>0</td>
* <tr><th scope="row">m</th> <td>minute-of-hour</td> <td>number</td> <td>30</td>
* <tr><th scope="row">s</th> <td>second-of-minute</td> <td>number</td> <td>55</td>
* <tr><th scope="row">S</th> <td>fraction-of-second</td> <td>fraction</td> <td>978</td>
* <tr><th scope="row">A</th> <td>milli-of-day</td> <td>number</td> <td>1234</td>
* <tr><th scope="row">n</th> <td>nano-of-second</td> <td>number</td> <td>987654321</td>
* <tr><th scope="row">N</th> <td>nano-of-day</td> <td>number</td> <td>1234000000</td>
*
* <tr><th scope="row">V</th> <td>time-zone ID</td> <td>zone-id</td> <td>America/Los_Angeles; Z; -08:30</td>
* <tr><th scope="row">v</th> <td>generic time-zone name</td> <td>zone-name</td> <td>Pacific Time; PT</td>
* <tr><th scope="row">z</th> <td>time-zone name</td> <td>zone-name</td> <td>Pacific Standard Time; PST</td>
* <tr><th scope="row">O</th> <td>localized zone-offset</td> <td>offset-O</td> <td>GMT+8; GMT+08:00; UTC-08:00</td>
* <tr><th scope="row">X</th> <td>zone-offset 'Z' for zero</td> <td>offset-X</td> <td>Z; -08; -0830; -08:30; -083015; -08:30:15</td>
* <tr><th scope="row">x</th> <td>zone-offset</td> <td>offset-x</td> <td>+0000; -08; -0830; -08:30; -083015; -08:30:15</td>
* <tr><th scope="row">Z</th> <td>zone-offset</td> <td>offset-Z</td> <td>+0000; -0800; -08:00</td>
*
* <tr><th scope="row">p</th> <td>pad next</td> <td>pad modifier</td> <td>1</td>
*
* <tr><th scope="row">'</th> <td>escape for text</td> <td>delimiter</td> <td></td>
* <tr><th scope="row">''</th> <td>single quote</td> <td>literal</td> <td>'</td>
* <tr><th scope="row">[</th> <td>optional section start</td> <td></td> <td></td>
* <tr><th scope="row">]</th> <td>optional section end</td> <td></td> <td></td>
* <tr><th scope="row">#</th> <td>reserved for future use</td> <td></td> <td></td>
* <tr><th scope="row">{</th> <td>reserved for future use</td> <td></td> <td></td>
* <tr><th scope="row">}</th> <td>reserved for future use</td> <td></td> <td></td>
* </tbody>
* </table>
* <p>
* The count of pattern letters determines the format.
* <p>
* <b>Text</b>: The text style is determined based on the number of pattern
* letters used. Less than 4 pattern letters will use the
* {@link TextStyle#SHORT short form}. Exactly 4 pattern letters will use the
* {@link TextStyle#FULL full form}. Exactly 5 pattern letters will use the
* {@link TextStyle#NARROW narrow form}.
* Pattern letters 'L', 'c', and 'q' specify the stand-alone form of the text styles.
* <p>
* <b>Number</b>: If the count of letters is one, then the value is output using
* the minimum number of digits and without padding. Otherwise, the count of digits
* is used as the width of the output field, with the value zero-padded as necessary.
* The following pattern letters have constraints on the count of letters.
* Only one letter of 'c' and 'F' can be specified.
* Up to two letters of 'd', 'H', 'h', 'K', 'k', 'm', and 's' can be specified.
* Up to three letters of 'D' can be specified.
* <p>
* <b>Number/Text</b>: If the count of pattern letters is 3 or greater, use the
* Text rules above. Otherwise use the Number rules above.
* <p>
* <b>Fraction</b>: Outputs the nano-of-second field as a fraction-of-second.
* The nano-of-second value has nine digits, thus the count of pattern letters
* is from 1 to 9. If it is less than 9, then the nano-of-second value is
* truncated, with only the most significant digits being output.
* <p>
* <b>Year</b>: The count of letters determines the minimum field width below
* which padding is used. If the count of letters is two, then a
* {@link DateTimeFormatterBuilder#appendValueReduced reduced} two digit form is
* used. For printing, this outputs the rightmost two digits. For parsing, this
* will parse using the base value of 2000, resulting in a year within the range
* 2000 to 2099 inclusive. If the count of letters is less than four (but not
* two), then the sign is only output for negative years as per
* {@link SignStyle#NORMAL}. Otherwise, the sign is output if the pad width is
* exceeded, as per {@link SignStyle#EXCEEDS_PAD}.
* <p>
* <b>ZoneId</b>: This outputs the time-zone ID, such as 'Europe/Paris'. If the
* count of letters is two, then the time-zone ID is output. Any other count of
* letters throws {@code IllegalArgumentException}.
* <p>
* <b>Zone names</b>: This outputs the display name of the time-zone ID. If the
* pattern letter is 'z' the output is the daylight savings aware zone name.
* If there is insufficient information to determine whether DST applies,
* the name ignoring daylight savings time will be used.
* If the count of letters is one, two or three, then the short name is output.
* If the count of letters is four, then the full name is output.
* Five or more letters throws {@code IllegalArgumentException}.
* <p>
* If the pattern letter is 'v' the output provides the zone name ignoring
* daylight savings time. If the count of letters is one, then the short name is output.
* If the count of letters is four, then the full name is output.
* Two, three and five or more letters throw {@code IllegalArgumentException}.
* <p>
* <b>Offset X and x</b>: This formats the offset based on the number of pattern
* letters. One letter outputs just the hour, such as '+01', unless the minute
* is non-zero in which case the minute is also output, such as '+0130'. Two
* letters outputs the hour and minute, without a colon, such as '+0130'. Three
* letters outputs the hour and minute, with a colon, such as '+01:30'. Four
* letters outputs the hour and minute and optional second, without a colon,
* such as '+013015'. Five letters outputs the hour and minute and optional
* second, with a colon, such as '+01:30:15'. Six or more letters throws
* {@code IllegalArgumentException}. Pattern letter 'X' (upper case) will output
* 'Z' when the offset to be output would be zero, whereas pattern letter 'x'
* (lower case) will output '+00', '+0000', or '+00:00'.
* <p>
* <b>Offset O</b>: This formats the localized offset based on the number of
* pattern letters. One letter outputs the {@linkplain TextStyle#SHORT short}
* form of the localized offset, which is localized offset text, such as 'GMT',
* with hour without leading zero, optional 2-digit minute and second if
* non-zero, and colon, for example 'GMT+8'. Four letters outputs the
* {@linkplain TextStyle#FULL full} form, which is localized offset text,
* such as 'GMT, with 2-digit hour and minute field, optional second field
* if non-zero, and colon, for example 'GMT+08:00'. Any other count of letters
* throws {@code IllegalArgumentException}.
* <p>
* <b>Offset Z</b>: This formats the offset based on the number of pattern
* letters. One, two or three letters outputs the hour and minute, without a
* colon, such as '+0130'. The output will be '+0000' when the offset is zero.
* Four letters outputs the {@linkplain TextStyle#FULL full} form of localized
* offset, equivalent to four letters of Offset-O. The output will be the
* corresponding localized offset text if the offset is zero. Five
* letters outputs the hour, minute, with optional second if non-zero, with
* colon. It outputs 'Z' if the offset is zero.
* Six or more letters throws {@code IllegalArgumentException}.
* <p>
* <b>Optional section</b>: The optional section markers work exactly like
* calling {@link DateTimeFormatterBuilder#optionalStart()} and
* {@link DateTimeFormatterBuilder#optionalEnd()}.
* <p>
* <b>Pad modifier</b>: Modifies the pattern that immediately follows to be
* padded with spaces. The pad width is determined by the number of pattern
* letters. This is the same as calling
* {@link DateTimeFormatterBuilder#padNext(int)}.
* <p>
* For example, 'ppH' outputs the hour-of-day padded on the left with spaces to
* a width of 2.
* <p>
* Any unrecognized letter is an error. Any non-letter character, other than
* '[', ']', '{', '}', '#' and the single quote will be output directly.
* Despite this, it is recommended to use single quotes around all characters
* that you want to output directly to ensure that future changes do not break
* your application.
*
* <h3 id="resolving">Resolving</h3>
* Parsing is implemented as a two-phase operation.
* First, the text is parsed using the layout defined by the formatter, producing
* a {@code Map} of field to value, a {@code ZoneId} and a {@code Chronology}.
* Second, the parsed data is <em>resolved</em>, by validating, combining and
* simplifying the various fields into more useful ones.
* <p>
* Five parsing methods are supplied by this class.
* Four of these perform both the parse and resolve phases.
* The fifth method, {@link #parseUnresolved(CharSequence, ParsePosition)},
* only performs the first phase, leaving the result unresolved.
* As such, it is essentially a low-level operation.
* <p>
* The resolve phase is controlled by two parameters, set on this class.
* <p>
* The {@link ResolverStyle} is an enum that offers three different approaches,
* strict, smart and lenient. The smart option is the default.
* It can be set using {@link #withResolverStyle(ResolverStyle)}.
* <p>
* The {@link #withResolverFields(TemporalField...)} parameter allows the
* set of fields that will be resolved to be filtered before resolving starts.
* For example, if the formatter has parsed a year, month, day-of-month
* and day-of-year, then there are two approaches to resolve a date:
* (year + month + day-of-month) and (year + day-of-year).
* The resolver fields allows one of the two approaches to be selected.
* If no resolver fields are set then both approaches must result in the same date.
* <p>
* Resolving separate fields to form a complete date and time is a complex
* process with behaviour distributed across a number of classes.
* It follows these steps:
* <ol>
* <li>The chronology is determined.
* The chronology of the result is either the chronology that was parsed,
* or if no chronology was parsed, it is the chronology set on this class,
* or if that is null, it is {@code IsoChronology}.
* <li>The {@code ChronoField} date fields are resolved.
* This is achieved using {@link Chronology#resolveDate(Map, ResolverStyle)}.
* Documentation about field resolution is located in the implementation
* of {@code Chronology}.
* <li>The {@code ChronoField} time fields are resolved.
* This is documented on {@link ChronoField} and is the same for all chronologies.
* <li>Any fields that are not {@code ChronoField} are processed.
* This is achieved using {@link TemporalField#resolve(Map, TemporalAccessor, ResolverStyle)}.
* Documentation about field resolution is located in the implementation
* of {@code TemporalField}.
* <li>The {@code ChronoField} date and time fields are re-resolved.
* This allows fields in step four to produce {@code ChronoField} values
* and have them be processed into dates and times.
* <li>A {@code LocalTime} is formed if there is at least an hour-of-day available.
* This involves providing default values for minute, second and fraction of second.
* <li>Any remaining unresolved fields are cross-checked against any
* date and/or time that was resolved. Thus, an earlier stage would resolve
* (year + month + day-of-month) to a date, and this stage would check that
* day-of-week was valid for the date.
* <li>If an {@linkplain #parsedExcessDays() excess number of days}
* was parsed then it is added to the date if a date is available.
* <li> If a second-based field is present, but {@code LocalTime} was not parsed,
* then the resolver ensures that milli, micro and nano second values are
* available to meet the contract of {@link ChronoField}.
* These will be set to zero if missing.
* <li>If both date and time were parsed and either an offset or zone is present,
* the field {@link ChronoField#INSTANT_SECONDS} is created.
* If an offset was parsed then the offset will be combined with the
* {@code LocalDateTime} to form the instant, with any zone ignored.
* If a {@code ZoneId} was parsed without an offset then the zone will be
* combined with the {@code LocalDateTime} to form the instant using the rules
* of {@link ChronoLocalDateTime#atZone(ZoneId)}.
* </ol>
*
* @implSpec
* This class is immutable and thread-safe.
*
* @since 1.8
*/
public final class DateTimeFormatter {
/**
* The printer and/or parser to use, not null.
*/
private final CompositePrinterParser printerParser;
/**
* The locale to use for formatting, not null.
*/
private final Locale locale;
/**
* The symbols to use for formatting, not null.
*/
private final DecimalStyle decimalStyle;
/**
* The resolver style to use, not null.
*/
private final ResolverStyle resolverStyle;
/**
* The fields to use in resolving, null for all fields.
*/
private final Set<TemporalField> resolverFields;
/**
* The chronology to use for formatting, null for no override.
*/
private final Chronology chrono;
/**
* The zone to use for formatting, null for no override.
*/
private final ZoneId zone;
//-----------------------------------------------------------------------
/**
* Creates a formatter using the specified pattern.
* <p>
* This method will create a formatter based on a simple
* <a href="#patterns">pattern of letters and symbols</a>
* as described in the class documentation.
* For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'.
* <p>
* The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}.
* This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter.
* Alternatively use the {@link #ofPattern(String, Locale)} variant of this method.
* <p>
* The returned formatter has no override chronology or zone.
* It uses {@link ResolverStyle#SMART SMART} resolver style.
*
* @param pattern the pattern to use, not null
* @return the formatter based on the pattern, not null
* @throws IllegalArgumentException if the pattern is invalid
* @see DateTimeFormatterBuilder#appendPattern(String)
*/
public static DateTimeFormatter ofPattern(String pattern) {
return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter();
}
/**
* Creates a formatter using the specified pattern and locale.
* <p>
* This method will create a formatter based on a simple
* <a href="#patterns">pattern of letters and symbols</a>
* as described in the class documentation.
* For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'.
* <p>
* The formatter will use the specified locale.
* This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter.
* <p>
* The returned formatter has no override chronology or zone.
* It uses {@link ResolverStyle#SMART SMART} resolver style.
*
* @param pattern the pattern to use, not null
* @param locale the locale to use, not null
* @return the formatter based on the pattern, not null
* @throws IllegalArgumentException if the pattern is invalid
* @see DateTimeFormatterBuilder#appendPattern(String)
*/
public static DateTimeFormatter ofPattern(String pattern, Locale locale) {
return new DateTimeFormatterBuilder().appendPattern(pattern).toFormatter(locale);
}
//-----------------------------------------------------------------------
/**
* Returns a locale specific date format for the ISO chronology.
* <p>
* This returns a formatter that will format or parse a date.
* The exact format pattern used varies by locale.
* <p>
* The locale is determined from the formatter. The formatter returned directly by
* this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}.
* The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
* on the result of this method.
* <p>
* Note that the localized pattern is looked up lazily.
* This {@code DateTimeFormatter} holds the style required and the locale,
* looking up the pattern required on demand.
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
*
* @param dateStyle the formatter style to obtain, not null
* @return the date formatter, not null
*/
public static DateTimeFormatter ofLocalizedDate(FormatStyle dateStyle) {
Objects.requireNonNull(dateStyle, "dateStyle");
return new DateTimeFormatterBuilder().appendLocalized(dateStyle, null)
.toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
}
/**
* Returns a locale specific time format for the ISO chronology.
* <p>
* This returns a formatter that will format or parse a time.
* The exact format pattern used varies by locale.
* <p>
* The locale is determined from the formatter. The formatter returned directly by
* this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}.
* The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
* on the result of this method.
* <p>
* Note that the localized pattern is looked up lazily.
* This {@code DateTimeFormatter} holds the style required and the locale,
* looking up the pattern required on demand.
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
* The {@code FULL} and {@code LONG} styles typically require a time-zone.
* When formatting using these styles, a {@code ZoneId} must be available,
* either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}.
*
* @param timeStyle the formatter style to obtain, not null
* @return the time formatter, not null
*/
public static DateTimeFormatter ofLocalizedTime(FormatStyle timeStyle) {
Objects.requireNonNull(timeStyle, "timeStyle");
return new DateTimeFormatterBuilder().appendLocalized(null, timeStyle)
.toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
}
/**
* Returns a locale specific date-time formatter for the ISO chronology.
* <p>
* This returns a formatter that will format or parse a date-time.
* The exact format pattern used varies by locale.
* <p>
* The locale is determined from the formatter. The formatter returned directly by
* this method will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}.
* The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
* on the result of this method.
* <p>
* Note that the localized pattern is looked up lazily.
* This {@code DateTimeFormatter} holds the style required and the locale,
* looking up the pattern required on demand.
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
* The {@code FULL} and {@code LONG} styles typically require a time-zone.
* When formatting using these styles, a {@code ZoneId} must be available,
* either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}.
*
* @param dateTimeStyle the formatter style to obtain, not null
* @return the date-time formatter, not null
*/
public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateTimeStyle) {
Objects.requireNonNull(dateTimeStyle, "dateTimeStyle");
return new DateTimeFormatterBuilder().appendLocalized(dateTimeStyle, dateTimeStyle)
.toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
}
/**
* Returns a locale specific date and time format for the ISO chronology.
* <p>
* This returns a formatter that will format or parse a date-time.
* The exact format pattern used varies by locale.
* <p>
* The locale is determined from the formatter. The formatter returned directly by
* this method will use the {@link Locale#getDefault() default FORMAT locale}.
* The locale can be controlled using {@link DateTimeFormatter#withLocale(Locale) withLocale(Locale)}
* on the result of this method.
* <p>
* Note that the localized pattern is looked up lazily.
* This {@code DateTimeFormatter} holds the style required and the locale,
* looking up the pattern required on demand.
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#SMART SMART} resolver style.
* The {@code FULL} and {@code LONG} styles typically require a time-zone.
* When formatting using these styles, a {@code ZoneId} must be available,
* either by using {@code ZonedDateTime} or {@link DateTimeFormatter#withZone}.
*
* @param dateStyle the date formatter style to obtain, not null
* @param timeStyle the time formatter style to obtain, not null
* @return the date, time or date-time formatter, not null
*/
public static DateTimeFormatter ofLocalizedDateTime(FormatStyle dateStyle, FormatStyle timeStyle) {
Objects.requireNonNull(dateStyle, "dateStyle");
Objects.requireNonNull(timeStyle, "timeStyle");
return new DateTimeFormatterBuilder().appendLocalized(dateStyle, timeStyle)
.toFormatter(ResolverStyle.SMART, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO date formatter that formats or parses a date without an
* offset, such as '2011-12-03'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended local date format.
* The format consists of:
* <ul>
* <li>Four digits or more for the {@link ChronoField#YEAR year}.
* Years in the range 0000 to 9999 will be pre-padded by zero to ensure four digits.
* Years outside that range will have a prefixed positive or negative symbol.
* <li>A dash
* <li>Two digits for the {@link ChronoField#MONTH_OF_YEAR month-of-year}.
* This is pre-padded by zero to ensure two digits.
* <li>A dash
* <li>Two digits for the {@link ChronoField#DAY_OF_MONTH day-of-month}.
* This is pre-padded by zero to ensure two digits.
* </ul>
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_LOCAL_DATE;
static {
ISO_LOCAL_DATE = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO date formatter that formats or parses a date with an
* offset, such as '2011-12-03+01:00'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended offset date format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_DATE}
* <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
* they will be handled even though this is not part of the ISO-8601 standard.
* Parsing is case insensitive.
* </ul>
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_OFFSET_DATE;
static {
ISO_OFFSET_DATE = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendOffsetId()
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO date formatter that formats or parses a date with the
* offset if available, such as '2011-12-03' or '2011-12-03+01:00'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended date format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_DATE}
* <li>If the offset is not available then the format is complete.
* <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
* they will be handled even though this is not part of the ISO-8601 standard.
* Parsing is case insensitive.
* </ul>
* <p>
* As this formatter has an optional element, it may be necessary to parse using
* {@link DateTimeFormatter#parseBest}.
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_DATE;
static {
ISO_DATE = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.optionalStart()
.appendOffsetId()
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO time formatter that formats or parses a time without an
* offset, such as '10:15' or '10:15:30'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended local time format.
* The format consists of:
* <ul>
* <li>Two digits for the {@link ChronoField#HOUR_OF_DAY hour-of-day}.
* This is pre-padded by zero to ensure two digits.
* <li>A colon
* <li>Two digits for the {@link ChronoField#MINUTE_OF_HOUR minute-of-hour}.
* This is pre-padded by zero to ensure two digits.
* <li>If the second-of-minute is not available then the format is complete.
* <li>A colon
* <li>Two digits for the {@link ChronoField#SECOND_OF_MINUTE second-of-minute}.
* This is pre-padded by zero to ensure two digits.
* <li>If the nano-of-second is zero or not available then the format is complete.
* <li>A decimal point
* <li>One to nine digits for the {@link ChronoField#NANO_OF_SECOND nano-of-second}.
* As many digits will be output as required.
* </ul>
* <p>
* The returned formatter has no override chronology or zone.
* It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_LOCAL_TIME;
static {
ISO_LOCAL_TIME = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 9, true)
.toFormatter(ResolverStyle.STRICT, null);
}
//-----------------------------------------------------------------------
/**
* The ISO time formatter that formats or parses a time with an
* offset, such as '10:15+01:00' or '10:15:30+01:00'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended offset time format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_TIME}
* <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
* they will be handled even though this is not part of the ISO-8601 standard.
* Parsing is case insensitive.
* </ul>
* <p>
* The returned formatter has no override chronology or zone.
* It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_OFFSET_TIME;
static {
ISO_OFFSET_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_TIME)
.appendOffsetId()
.toFormatter(ResolverStyle.STRICT, null);
}
//-----------------------------------------------------------------------
/**
* The ISO time formatter that formats or parses a time, with the
* offset if available, such as '10:15', '10:15:30' or '10:15:30+01:00'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended offset time format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_TIME}
* <li>If the offset is not available then the format is complete.
* <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
* they will be handled even though this is not part of the ISO-8601 standard.
* Parsing is case insensitive.
* </ul>
* <p>
* As this formatter has an optional element, it may be necessary to parse using
* {@link DateTimeFormatter#parseBest}.
* <p>
* The returned formatter has no override chronology or zone.
* It uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_TIME;
static {
ISO_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_TIME)
.optionalStart()
.appendOffsetId()
.toFormatter(ResolverStyle.STRICT, null);
}
//-----------------------------------------------------------------------
/**
* The ISO date-time formatter that formats or parses a date-time without
* an offset, such as '2011-12-03T10:15:30'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended offset date-time format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_DATE}
* <li>The letter 'T'. Parsing is case insensitive.
* <li>The {@link #ISO_LOCAL_TIME}
* </ul>
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME;
static {
ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.append(ISO_LOCAL_TIME)
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO date-time formatter that formats or parses a date-time with an
* offset, such as '2011-12-03T10:15:30+01:00'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* the ISO-8601 extended offset date-time format.
* The format consists of:
* <ul>
* <li>The {@link #ISO_LOCAL_DATE_TIME}
* <li>The {@link ZoneOffset#getId() offset ID}. If the offset has seconds then
* they will be handled even though this is not part of the ISO-8601 standard.
* The offset parsing is lenient, which allows the minutes and seconds to be optional.
* Parsing is case insensitive.
* </ul>
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME;
static {
ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE_TIME)
.parseLenient()
.appendOffsetId()
.parseStrict()
.toFormatter(ResolverStyle.STRICT, IsoChronology.INSTANCE);
}
//-----------------------------------------------------------------------
/**
* The ISO-like date-time formatter that formats or parses a date-time with
* offset and zone, such as '2011-12-03T10:15:30+01:00[Europe/Paris]'.
* <p>
* This returns an immutable formatter capable of formatting and parsing
* a format that extends the ISO-8601 extended offset date-time format
* to add the time-zone.
* The section in square brackets is not part of the ISO-8601 standard.
* The format consists of:
* <ul>
* <li>The {@link #ISO_OFFSET_DATE_TIME}
* <li>If the zone ID is not available or is a {@code ZoneOffset} then the format is complete.
* <li>An open square bracket '['.
* <li>The {@link ZoneId#getId() zone ID}. This is not part of the ISO-8601 standard.
* Parsing is case sensitive.
* <li>A close square bracket ']'.
* </ul>
* <p>
* The returned formatter has a chronology of ISO set to ensure dates in
* other calendar systems are correctly converted.
* It has no override zone and uses the {@link ResolverStyle#STRICT STRICT} resolver style.
*/
public static final DateTimeFormatter ISO_ZONED_DATE_TIME;
static {
ISO_ZONED_DATE_TIME = new DateTimeFormatterBuilder()
.append(ISO_OFFSET_DATE_TIME)
.optionalStart()
.appendLiteral('[')
.parseCaseSensitive()
.appendZoneRegionId()