< prev index next >
test/jdk/java/time/tck/java/time/format/TCKOffsetPrinterParser.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, 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.
@@ -67,10 +67,11 @@
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.TextStyle;
+import java.util.Locale;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -78,10 +79,12 @@
* Test DateTimeFormatterBuilder.appendOffset().
*/
@Test
public class TCKOffsetPrinterParser {
+ private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
+
private static final ZoneOffset OFFSET_UTC = ZoneOffset.UTC;
private static final ZoneOffset OFFSET_P0100 = ZoneOffset.ofHours(1);
private static final ZoneOffset OFFSET_P0123 = ZoneOffset.ofHoursMinutes(1, 23);
private static final ZoneOffset OFFSET_P0023 = ZoneOffset.ofHoursMinutes(0, 23);
private static final ZoneOffset OFFSET_P012345 = ZoneOffset.ofHoursMinutesSeconds(1, 23, 45);
@@ -89,20 +92,22 @@
private static final ZoneOffset OFFSET_M0100 = ZoneOffset.ofHours(-1);
private static final ZoneOffset OFFSET_M0123 = ZoneOffset.ofHoursMinutes(-1, -23);
private static final ZoneOffset OFFSET_M0023 = ZoneOffset.ofHoursMinutes(0, -23);
private static final ZoneOffset OFFSET_M012345 = ZoneOffset.ofHoursMinutesSeconds(-1, -23, -45);
private static final ZoneOffset OFFSET_M000045 = ZoneOffset.ofHoursMinutesSeconds(0, 0, -45);
- private static final LocalDateTime DT_2012_06_30_12_30_40 = LocalDateTime.of(2012, 6, 30, 12, 30, 40);
private static final ZoneOffset OFFSET_P1100 = ZoneOffset.ofHours(11);
private static final ZoneOffset OFFSET_P1123 = ZoneOffset.ofHoursMinutes(11, 23);
private static final ZoneOffset OFFSET_P1023 = ZoneOffset.ofHoursMinutes(10, 23);
private static final ZoneOffset OFFSET_P112345 = ZoneOffset.ofHoursMinutesSeconds(11, 23, 45);
private static final ZoneOffset OFFSET_P100045 = ZoneOffset.ofHoursMinutesSeconds(10, 0, 45);
private static final ZoneOffset OFFSET_M1100 = ZoneOffset.ofHours(-11);
private static final ZoneOffset OFFSET_M1123 = ZoneOffset.ofHoursMinutes(-11, -23);
private static final ZoneOffset OFFSET_M112345 = ZoneOffset.ofHoursMinutesSeconds(-11, -23, -45);
+
+ private static final Locale LOCALE_US = Locale.US;
+
private DateTimeFormatterBuilder builder;
@BeforeMethod
public void setUp() {
builder = new DateTimeFormatterBuilder();
@@ -441,32 +446,32 @@
}
@DataProvider(name="print_localized")
Object[][] data_print_localized() {
return new Object[][] {
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+01:00"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+01:23"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+00:23"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+01:23:45"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-01:00"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-01:23"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-00:23"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-01:23:45"},
- {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-00:00:45"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, "GMT"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, "GMT+1"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, "GMT+1:23"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, "GMT+0:23"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, "GMT+1:23:45"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, "GMT-1"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, "GMT-1:23"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, "GMT-0:23"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, "GMT-1:23:45"},
- {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, "GMT-0:00:45"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+01:00"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+01:23"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+00:23"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+01:23:45"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-01:00"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-01:23"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-00:23"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-01:23:45"},
+ {TextStyle.FULL, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-00:00:45"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_UTC, LOCALE_US, "GMT"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0100, LOCALE_US, "GMT+1"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0123, LOCALE_US, "GMT+1:23"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P0023, LOCALE_US, "GMT+0:23"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_P012345, LOCALE_US, "GMT+1:23:45"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0100, LOCALE_US, "GMT-1"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0123, LOCALE_US, "GMT-1:23"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M0023, LOCALE_US, "GMT-0:23"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M012345, LOCALE_US, "GMT-1:23:45"},
+ {TextStyle.SHORT, DT_2012_06_30_12_30_40, OFFSET_M000045, LOCALE_US, "GMT-0:00:45"}
};
}
@Test(dataProvider="print")
public void test_print(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
@@ -527,11 +532,10 @@
}
}
@Test(dataProvider="print")
public void test_print_pattern_Z(String offsetPattern, String noOffset, LocalDateTime ldt, ZoneId zone, String expected) {
- String pattern = null;
if (offsetPattern.equals("+HHMM") && noOffset.equals("Z")) {
ZonedDateTime zdt = ldt.atZone(zone);
DateTimeFormatter f1 = new DateTimeFormatterBuilder().appendPattern("Z").toFormatter();
String output1 = f1.format(zdt);
assertEquals(output1, (expected.equals("Z") ? "+0000" : expected));
@@ -550,41 +554,43 @@
assertEquals(output, expected);
}
}
@Test(dataProvider="print_localized")
- public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
+ public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, Locale locale, String expected) {
+
OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
ZonedDateTime zdt = ldt.atZone(offset);
- DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style)
- .toFormatter();
+ builder = new DateTimeFormatterBuilder().appendLocalizedOffset(style);
+ DateTimeFormatter f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
if (style == TextStyle.FULL) {
- f = new DateTimeFormatterBuilder().appendPattern("ZZZZ")
- .toFormatter();
+ builder = new DateTimeFormatterBuilder().appendPattern("ZZZZ");
+ f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
- f = new DateTimeFormatterBuilder().appendPattern("OOOO")
- .toFormatter();
+ builder = new DateTimeFormatterBuilder().appendPattern("OOOO");
+ f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
}
if (style == TextStyle.SHORT) {
- f = new DateTimeFormatterBuilder().appendPattern("O")
- .toFormatter();
+ builder = new DateTimeFormatterBuilder().appendPattern("O");
+ f = (locale != null) ? builder.toFormatter(locale) : builder.toFormatter();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
}
+
}
//-----------------------------------------------------------------------
@Test(expectedExceptions=IllegalArgumentException.class)
public void test_print_pattern_X6rejected() {
< prev index next >