use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKResolverStyle method test_resolverStyle.
@Test(dataProvider = "resolverStyle")
public void test_resolverStyle(String str, ResolverStyle style, Class<?> expectedEx, int year, int month, int day) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
builder.appendValue(ChronoField.YEAR_OF_ERA);
builder.appendLiteral("/");
builder.appendValue(ChronoField.MONTH_OF_YEAR);
builder.appendLiteral("/");
builder.appendValue(ChronoField.DAY_OF_MONTH);
Map<Long, String> eraMap = new HashMap<Long, String>();
eraMap.put(1L, "CE");
eraMap.put(0L, "BCE");
DateTimeFormatter optionalFormatter = new DateTimeFormatterBuilder().appendLiteral(" ").appendText(ChronoField.ERA, eraMap).toFormatter();
DateTimeFormatter formatter = builder.appendOptional(optionalFormatter).toFormatter();
formatter = formatter.withResolverStyle(style);
if (expectedEx == null) {
TemporalAccessor accessor = formatter.parse(str);
assertEquals(accessor.get(ChronoField.YEAR_OF_ERA), year);
assertEquals(accessor.get(ChronoField.MONTH_OF_YEAR), month);
assertEquals(accessor.get(ChronoField.DAY_OF_MONTH), day);
} else {
try {
formatter.parse(str);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKSignStyle method test_signStyle.
@Test(dataProvider = "signStyle")
public void test_signStyle(LocalDate localDate, SignStyle style, Class<?> expectedEx, String expectedStr) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR, 2, 4, style).toFormatter();
formatter = formatter.withZone(ZoneOffset.UTC);
if (expectedEx == null) {
String output = formatter.format(localDate);
assertEquals(output, expectedStr);
} else {
try {
formatter.format(localDate);
fail();
} catch (Exception ex) {
assertTrue(expectedEx.isInstance(ex));
}
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKOffsetPrinterParser method test_print_localized.
@Test(dataProvider = "print_localized")
public void test_print_localized(TextStyle style, LocalDateTime ldt, ZoneOffset offset, String expected) {
OffsetDateTime odt = OffsetDateTime.of(ldt, offset);
ZonedDateTime zdt = ldt.atZone(offset);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendLocalizedOffset(style).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();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
f = new DateTimeFormatterBuilder().appendPattern("OOOO").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();
assertEquals(f.format(odt), expected);
assertEquals(f.format(zdt), expected);
assertEquals(f.parse(expected, ZoneOffset::from), offset);
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKOffsetPrinterParser method test_print_pattern_Z.
@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));
DateTimeFormatter f2 = new DateTimeFormatterBuilder().appendPattern("ZZ").toFormatter();
String output2 = f2.format(zdt);
assertEquals(output2, (expected.equals("Z") ? "+0000" : expected));
DateTimeFormatter f3 = new DateTimeFormatterBuilder().appendPattern("ZZZ").toFormatter();
String output3 = f3.format(zdt);
assertEquals(output3, (expected.equals("Z") ? "+0000" : expected));
} else if (offsetPattern.equals("+HH:MM:ss") && noOffset.equals("Z")) {
ZonedDateTime zdt = ldt.atZone(zone);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendPattern("ZZZZZ").toFormatter();
String output = f.format(zdt);
assertEquals(output, expected);
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKZoneIdPrinterParser method setUp.
@BeforeMethod
public void setUp() {
builder = new DateTimeFormatterBuilder();
pos = new ParsePosition(0);
}
Aggregations