use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TestReducedParser method test_reducedWithChronoYear.
@Test(dataProvider = "ReducedWithChrono")
public void test_reducedWithChronoYear(ChronoLocalDate date) {
Chronology chrono = date.getChronology();
DateTimeFormatter df = new DateTimeFormatterBuilder().appendValueReduced(YEAR, 2, 2, LocalDate.of(2000, 1, 1)).toFormatter().withChronology(chrono);
int expected = date.get(YEAR);
String input = df.format(date);
ParsePosition pos = new ParsePosition(0);
TemporalAccessor parsed = df.parseUnresolved(input, pos);
int actual = parsed.get(YEAR);
assertEquals(actual, expected, String.format("Wrong date parsed, chrono: %s, input: %s", chrono, input));
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeFormatter method test_format_withZone_withChronology.
@Test(dataProvider = "formatWithZoneWithChronology")
public void test_format_withZone_withChronology(Chronology overrideChrono, ZoneId overrideZone, TemporalAccessor temporal, String expected) {
DateTimeFormatter test = new DateTimeFormatterBuilder().optionalStart().appendValue(YEAR, 4).optionalEnd().appendLiteral(':').optionalStart().appendValue(HOUR_OF_DAY, 2).optionalEnd().appendLiteral(':').optionalStart().appendOffsetId().optionalEnd().appendLiteral(':').optionalStart().appendZoneId().optionalEnd().appendLiteral(':').optionalStart().appendChronologyId().optionalEnd().toFormatter(Locale.ENGLISH).withChronology(overrideChrono).withZone(overrideZone);
if (expected != null) {
String result = test.format(temporal);
assertEquals(result, expected);
} else {
try {
test.format(temporal);
fail("Formatting should have failed");
} catch (DateTimeException ex) {
// expected
}
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeFormatter method test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue.
@Test
public void test_parseUnresolved_StringParsePosition_duplicateFieldDifferentValue() {
DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter();
ParsePosition pos = new ParsePosition(3);
TemporalAccessor result = test.parseUnresolved("XXX6-7", pos);
assertEquals(pos.getIndex(), 3);
assertEquals(pos.getErrorIndex(), 5);
assertEquals(result, null);
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeFormatter method test_resolverFields_selectOneDateResolveYMD.
//-----------------------------------------------------------------------
@Test
public void test_resolverFields_selectOneDateResolveYMD() throws Exception {
DateTimeFormatter base = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(DAY_OF_MONTH).appendLiteral('-').appendValue(DAY_OF_YEAR).toFormatter();
DateTimeFormatter f = base.withResolverFields(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH);
try {
// wrong day-of-year
base.parse("2012-6-30-321", LocalDate::from);
fail();
} catch (DateTimeException ex) {
// expected, fails as it produces two different dates
}
// ignored day-of-year
LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from);
assertEquals(parsed, LocalDate.of(2012, 6, 30));
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeFormatter method test_resolverFields_emptyList.
@Test
public void test_resolverFields_emptyList() throws Exception {
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).toFormatter().withResolverFields();
TemporalAccessor parsed = f.parse("2012");
// not in the list of resolverFields
assertEquals(parsed.isSupported(YEAR), false);
}
Aggregations