1 /*
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time.format;
61
62 import static org.testng.Assert.assertEquals;
63
64 import java.time.LocalDateTime;
65 import java.time.OffsetDateTime;
66 import java.time.ZoneId;
67 import java.time.ZoneOffset;
68 import java.time.ZonedDateTime;
69 import java.time.format.DateTimeFormatter;
70 import java.time.format.DateTimeFormatterBuilder;
71 import java.time.format.TextStyle;
72
73 import org.testng.annotations.BeforeMethod;
74 import org.testng.annotations.DataProvider;
75 import org.testng.annotations.Test;
76
77 /**
78 * Test DateTimeFormatterBuilder.appendOffset().
79 */
80 @Test
81 public class TCKOffsetPrinterParser {
82
83 private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
84 private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
85 private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
86 private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
87 private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
88 private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45);
89 private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
90 private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
91 private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);
92 private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45);
93 private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45);
94 private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
95
96 private static final ZoneOffset OFFSET_P1100 = ZoneOffset.ofHours(11);
97 private static final ZoneOffset OFFSET_P1123 = ZoneOffset.ofHoursMinutes(11, 23);
98 private static final ZoneOffset OFFSET_P1023 = ZoneOffset.ofHoursMinutes(10, 23);
99 private static final ZoneOffset OFFSET_P112345 = ZoneOffset.ofHoursMinutesSeconds(11, 23, 45);
100 private static final ZoneOffset OFFSET_P100045 = ZoneOffset.ofHoursMinutesSeconds(10, 0, 45);
101 private static final ZoneOffset OFFSET_M1100 = ZoneOffset.ofHours(-11);
102 private static final ZoneOffset OFFSET_M1123 = ZoneOffset.ofHoursMinutes(-11, -23);
103 private static final ZoneOffset OFFSET_M112345 = ZoneOffset.ofHoursMinutesSeconds(-11, -23, -45);
104 private DateTimeFormatterBuilder builder;
105
106 @BeforeMethod
107 public void setUp() {
108 builder = new DateTimeFormatterBuilder();
109 }
110
111 //-----------------------------------------------------------------------
112 @DataProvider(name="print")
113 Object[][] data_print() {
114 return new Object[][] {
115 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
116 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01"},
117 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01"},
118 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "Z"},
119 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01"},
120 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
121 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01"},
122 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01"},
123 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "Z"},
426 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1023, "+10:23"},
427 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P112345, "+11:23:45"},
428 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1100, "-11"},
429 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1123, "-11:23"},
430 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M112345, "-11:23:45"},
431
432 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1100, "+11"},
433 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1123, "+1123"},
434 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1023, "+1023"},
435 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P112345, "+112345"},
436 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P100045, "+100045"},
437 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1100, "-11"},
438 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1123, "-1123"},
439 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M112345, "-112345"},
440 };
441 }
442
443 @DataProvider(name="print_localized")
444 Object[][] data_print_localized() {
445 return new Object[][] {
446 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"},
447 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+01:00"},
448 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+01:23"},
449 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+00:23"},
450 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+01:23:45"},
451 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"},
452 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-01:00"},
453 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-01:23"},
454 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-00:23"},
455 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-01:23:45"},
456 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"},
457 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"},
458 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+1"},
459 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+1:23"},
460 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+0:23"},
461 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+1:23:45"},
462 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"},
463 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-1"},
464 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-1:23"},
465 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-0:23"},
466 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-1:23:45"},
467 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"},
468 };
469 }
470
471 @Test(dataProvider="print")
472 public void test_print(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
473 ZonedDateTime zdt = ldt.atZone(zone);
474 builder.appendOffset(offsetPattern, noOffset);
475 String output = builder.toFormatter().format(zdt);
476 assertEquals(output, expected);
477 }
478
479 //-----------------------------------------------------------------------
480 @Test(dataProvider="print")
481 public void test_print_pattern_X(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
482 String pattern = null;
483 if (offsetPattern.equals("+HHmm") && noOffset.equals("Z")) {
484 pattern = "X";
485 } else if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
486 pattern = "XX";
487 } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
512 } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
513 pattern = "xxx";
514 zero = "+00:00";
515 } else if (offsetPattern.equals("+HHMMss") && noOffset.equals("Z")) {
516 pattern = "xxxx";
517 zero = "+0000";
518 } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
519 pattern = "xxxxx";
520 zero = "+00:00";
521 }
522 if (pattern != null) {
523 ZonedDateTime zdt = ldt.atZone(zone);
524 builder.appendPattern(pattern);
525 String output = builder.toFormatter().format(zdt);
526 assertEquals(output, (expected.equals("Z") ? zero : expected));
527 }
528 }
529
530 @Test(dataProvider="print")
531 public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
532 String pattern = null;
533 if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
534 ZonedDateTime zdt = ldt.atZone(zone);
535 DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter();
536 String output1 = f1.format(zdt);
537 assertEquals(output1, (expected.equals("Z") ? "+0000" : expected));
538
539 DateTimeFormatter f2 = new DateTimeFormatterBuilder().appendPattern("ZZ").toFormatter();
540 String output2 = f2.format(zdt);
541 assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
542
543 DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
544 String output3 = f3.format(zdt);
545 assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
546 } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
547 ZonedDateTime zdt = ldt.atZone(zone);
548 DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
549 String output = f.format(zdt);
550 assertEquals(output, expected);
551 }
552 }
553
554 @Test(dataProvider="print_localized")
555 public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
556 OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
557 ZonedDateTime zdt = ldt.atZone(offset);
558
559 DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style)
560 .toFormatter();
561 assertEquals(f.format(odt), expected);
562 assertEquals(f.format(zdt), expected);
563 assertEquals(f.parse(expected, ZoneOffset::from), offset);
564
565 if (style == TextStyle.FULL) {
566 f = new DateTimeFormatterBuilder().appendPattern("ZZZZ")
567 .toFormatter();
568 assertEquals(f.format(odt), expected);
569 assertEquals(f.format(zdt), expected);
570 assertEquals(f.parse(expected, ZoneOffset::from), offset);
571
572 f = new DateTimeFormatterBuilder().appendPattern("OOOO")
573 .toFormatter();
574 assertEquals(f.format(odt), expected);
575 assertEquals(f.format(zdt), expected);
576 assertEquals(f.parse(expected, ZoneOffset::from), offset);
577 }
578
579 if (style == TextStyle.SHORT) {
580 f = new DateTimeFormatterBuilder().appendPattern("O")
581 .toFormatter();
582 assertEquals(f.format(odt), expected);
583 assertEquals(f.format(zdt), expected);
584 assertEquals(f.parse(expected, ZoneOffset::from), offset);
585 }
586 }
587
588 //-----------------------------------------------------------------------
589 @Test(expectedExceptions=IllegalArgumentException.class)
590 public void test_print_pattern_X6rejected() {
591 builder.appendPattern("XXXXXX");
592 }
593
594 @Test(expectedExceptions=IllegalArgumentException.class)
595 public void test_print_pattern_x6rejected() {
596 builder.appendPattern("xxxxxx");
597 }
598
599 @Test(expectedExceptions=IllegalArgumentException.class)
600 public void test_print_pattern_Z6rejected() {
601 builder.appendPattern("ZZZZZZ");
602 }
603
604 @Test(expectedExceptions=IllegalArgumentException.class)
605 public void test_print_pattern_O2rejected() {
|
1 /*
2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
52 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60 package tck.java.time.format;
61
62 import static org.testng.Assert.assertEquals;
63
64 import java.time.LocalDateTime;
65 import java.time.OffsetDateTime;
66 import java.time.ZoneId;
67 import java.time.ZoneOffset;
68 import java.time.ZonedDateTime;
69 import java.time.format.DateTimeFormatter;
70 import java.time.format.DateTimeFormatterBuilder;
71 import java.time.format.TextStyle;
72 import java.util.Locale;
73
74 import org.testng.annotations.BeforeMethod;
75 import org.testng.annotations.DataProvider;
76 import org.testng.annotations.Test;
77
78 /**
79 * Test DateTimeFormatterBuilder.appendOffset().
80 */
81 @Test
82 public class TCKOffsetPrinterParser {
83
84 private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
85
86 private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
87 private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
88 private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
89 private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
90 private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
91 private static final ZoneOffset OFFSET_P000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, 45);
92 private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
93 private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
94 private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);
95 private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45);
96 private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45);
97
98 private static final ZoneOffset OFFSET_P1100 = ZoneOffset.ofHours(11);
99 private static final ZoneOffset OFFSET_P1123 = ZoneOffset.ofHoursMinutes(11, 23);
100 private static final ZoneOffset OFFSET_P1023 = ZoneOffset.ofHoursMinutes(10, 23);
101 private static final ZoneOffset OFFSET_P112345 = ZoneOffset.ofHoursMinutesSeconds(11, 23, 45);
102 private static final ZoneOffset OFFSET_P100045 = ZoneOffset.ofHoursMinutesSeconds(10, 0, 45);
103 private static final ZoneOffset OFFSET_M1100 = ZoneOffset.ofHours(-11);
104 private static final ZoneOffset OFFSET_M1123 = ZoneOffset.ofHoursMinutes(-11, -23);
105 private static final ZoneOffset OFFSET_M112345 = ZoneOffset.ofHoursMinutesSeconds(-11, -23, -45);
106
107 private static final Locale LOCALE_US = Locale.US;
108
109 private DateTimeFormatterBuilder builder;
110
111 @BeforeMethod
112 public void setUp() {
113 builder = new DateTimeFormatterBuilder();
114 }
115
116 //-----------------------------------------------------------------------
117 @DataProvider(name="print")
118 Object[][] data_print() {
119 return new Object[][] {
120 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_UTC, "Z"},
121 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0100, "+01"},
122 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0123, "+01"},
123 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P0023, "Z"},
124 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P012345, "+01"},
125 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_P000045, "Z"},
126 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0100, "-01"},
127 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0123, "-01"},
128 {"+HH", "Z", DT_2012_06_30_12_30_40, OFFSET_M0023, "Z"},
431 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1023, "+10:23"},
432 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_P112345, "+11:23:45"},
433 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1100, "-11"},
434 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1123, "-11:23"},
435 {"+H:mm:ss", "Z", DT_2012_06_30_12_30_40, OFFSET_M112345, "-11:23:45"},
436
437 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1100, "+11"},
438 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1123, "+1123"},
439 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P1023, "+1023"},
440 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P112345, "+112345"},
441 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_P100045, "+100045"},
442 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1100, "-11"},
443 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M1123, "-1123"},
444 {"+Hmmss", "Z", DT_2012_06_30_12_30_40, OFFSET_M112345, "-112345"},
445 };
446 }
447
448 @DataProvider(name="print_localized")
449 Object[][] data_print_localized() {
450 return new Object[][] {
451 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"},
452 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+01:00"},
453 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+01:23"},
454 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+00:23"},
455 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+01:23:45"},
456 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"},
457 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-01:00"},
458 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-01:23"},
459 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-00:23"},
460 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-01:23:45"},
461 {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"},
462 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"},
463 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+1"},
464 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+1:23"},
465 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+0:23"},
466 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+1:23:45"},
467 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"},
468 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-1"},
469 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-1:23"},
470 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-0:23"},
471 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-1:23:45"},
472 {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"}
473 };
474 }
475
476 @Test(dataProvider="print")
477 public void test_print(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
478 ZonedDateTime zdt = ldt.atZone(zone);
479 builder.appendOffset(offsetPattern, noOffset);
480 String output = builder.toFormatter().format(zdt);
481 assertEquals(output, expected);
482 }
483
484 //-----------------------------------------------------------------------
485 @Test(dataProvider="print")
486 public void test_print_pattern_X(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
487 String pattern = null;
488 if (offsetPattern.equals("+HHmm") && noOffset.equals("Z")) {
489 pattern = "X";
490 } else if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
491 pattern = "XX";
492 } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
517 } else if (offsetPattern.equals("+HH:MM") && noOffset.equals("Z")) {
518 pattern = "xxx";
519 zero = "+00:00";
520 } else if (offsetPattern.equals("+HHMMss") && noOffset.equals("Z")) {
521 pattern = "xxxx";
522 zero = "+0000";
523 } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
524 pattern = "xxxxx";
525 zero = "+00:00";
526 }
527 if (pattern != null) {
528 ZonedDateTime zdt = ldt.atZone(zone);
529 builder.appendPattern(pattern);
530 String output = builder.toFormatter().format(zdt);
531 assertEquals(output, (expected.equals("Z") ? zero : expected));
532 }
533 }
534
535 @Test(dataProvider="print")
536 public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
537 if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
538 ZonedDateTime zdt = ldt.atZone(zone);
539 DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter();
540 String output1 = f1.format(zdt);
541 assertEquals(output1, (expected.equals("Z") ? "+0000" : expected));
542
543 DateTimeFormatter f2 = new DateTimeFormatterBuilder().appendPattern("ZZ").toFormatter();
544 String output2 = f2.format(zdt);
545 assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
546
547 DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
548 String output3 = f3.format(zdt);
549 assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
550 } else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
551 ZonedDateTime zdt = ldt.atZone(zone);
552 DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
553 String output = f.format(zdt);
554 assertEquals(output, expected);
555 }
556 }
557
558 @Test(dataProvider="print_localized")
559 public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) {
560
561 OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
562 ZonedDateTime zdt = ldt.atZone(offset);
563
564 builder = new DateTimeFormatterBuilder().appendLocalizedOffset(style);
565 DateTimeFormatter f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
566 assertEquals(f.format(odt), expected);
567 assertEquals(f.format(zdt), expected);
568 assertEquals(f.parse(expected, ZoneOffset::from), offset);
569
570 if (style == TextStyle.FULL) {
571 builder = new DateTimeFormatterBuilder().appendPattern("ZZZZ");
572 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
573 assertEquals(f.format(odt), expected);
574 assertEquals(f.format(zdt), expected);
575 assertEquals(f.parse(expected, ZoneOffset::from), offset);
576
577 builder = new DateTimeFormatterBuilder().appendPattern("OOOO");
578 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
579 assertEquals(f.format(odt), expected);
580 assertEquals(f.format(zdt), expected);
581 assertEquals(f.parse(expected, ZoneOffset::from), offset);
582 }
583
584 if (style == TextStyle.SHORT) {
585 builder = new DateTimeFormatterBuilder().appendPattern("O");
586 f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
587 assertEquals(f.format(odt), expected);
588 assertEquals(f.format(zdt), expected);
589 assertEquals(f.parse(expected, ZoneOffset::from), offset);
590 }
591
592 }
593
594 //-----------------------------------------------------------------------
595 @Test(expectedExceptions=IllegalArgumentException.class)
596 public void test_print_pattern_X6rejected() {
597 builder.appendPattern("XXXXXX");
598 }
599
600 @Test(expectedExceptions=IllegalArgumentException.class)
601 public void test_print_pattern_x6rejected() {
602 builder.appendPattern("xxxxxx");
603 }
604
605 @Test(expectedExceptions=IllegalArgumentException.class)
606 public void test_print_pattern_Z6rejected() {
607 builder.appendPattern("ZZZZZZ");
608 }
609
610 @Test(expectedExceptions=IllegalArgumentException.class)
611 public void test_print_pattern_O2rejected() {
|