use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeParseResolver method test_fieldResolvesToChronoLocalDate_noOverrideChrono_matches.
//-------------------------------------------------------------------------
@Test
public void test_fieldResolvesToChronoLocalDate_noOverrideChrono_matches() {
LocalDate ldt = LocalDate.of(2010, 6, 30);
DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(ldt)).toFormatter();
TemporalAccessor accessor = f.parse("1234567890");
assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(2010, 6, 30));
assertEquals(accessor.query(TemporalQueries.localTime()), null);
assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeParseResolver method test_withZone_parsedZone_noOverride.
@Test
public void test_withZone_parsedZone_noOverride() {
DateTimeFormatter f = new DateTimeFormatterBuilder().parseDefaulting(EPOCH_DAY, 2).appendZoneId().toFormatter();
TemporalAccessor accessor = f.parse("Europe/Paris");
assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.of(1970, 1, 3));
assertEquals(accessor.query(TemporalQueries.localTime()), null);
assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TestZoneTextPrinterParser method test_ParseText.
@Test(dataProvider = "preferredZones")
public void test_ParseText(String expected, String text, Set<ZoneId> preferred, Locale locale, TextStyle style) {
DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendZoneText(style, preferred).toFormatter(locale).withDecimalStyle(DecimalStyle.of(locale));
String ret = fmt.parse(text, TemporalQueries.zone()).getId();
System.out.printf("[%-5s %s] %24s -> %s(%s)%n", locale.toString(), style == TextStyle.FULL ? " full" : "short", text, ret, expected);
assertEquals(ret, expected);
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TestDateTimeBuilderCombinations method test_derive.
@Test(dataProvider = "combine")
public void test_derive(final TemporalField field1, final Number value1, final TemporalField field2, final Number value2, final TemporalField field3, final Number value3, final TemporalField field4, final Number value4, Class<?> query, Object expectedVal) {
// mock for testing that does not fully comply with TemporalAccessor contract
TemporalAccessor test = new TemporalAccessor() {
@Override
public boolean isSupported(TemporalField field) {
return field == field1 || field == field2 || field == field3 || field == field4;
}
@Override
public long getLong(TemporalField field) {
if (field == field1) {
return value1.longValue();
}
if (field == field2) {
return value2.longValue();
}
if (field == field3) {
return value3.longValue();
}
if (field == field4) {
return value4.longValue();
}
throw new DateTimeException("Unsupported");
}
};
String str = "";
DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
dtfb.appendValue(field1).appendLiteral('-');
str += value1 + "-";
if (field2 != null) {
dtfb.appendValue(field2).appendLiteral('-');
str += value2 + "-";
}
if (field3 != null) {
dtfb.appendValue(field3).appendLiteral('-');
str += value3 + "-";
}
if (field4 != null) {
dtfb.appendValue(field4).appendLiteral('-');
str += value4 + "-";
}
TemporalAccessor parsed = dtfb.toFormatter().parse(str);
if (query == LocalDate.class) {
if (expectedVal != null) {
assertEquals(parsed.query(LocalDate::from), expectedVal);
} else {
try {
parsed.query(LocalDate::from);
fail();
} catch (DateTimeException ex) {
// expected
}
}
} else {
throw new IllegalArgumentException();
}
}
use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.
the class TCKDateTimeFormatter method test_parseUnresolved_StringParsePosition_duplicateFieldSameValue.
@Test
public void test_parseUnresolved_StringParsePosition_duplicateFieldSameValue() {
DateTimeFormatter test = new DateTimeFormatterBuilder().appendValue(MONTH_OF_YEAR).appendLiteral('-').appendValue(MONTH_OF_YEAR).toFormatter();
ParsePosition pos = new ParsePosition(3);
TemporalAccessor result = test.parseUnresolved("XXX6-6", pos);
assertEquals(pos.getIndex(), 6);
assertEquals(pos.getErrorIndex(), -1);
assertEquals(result.getLong(MONTH_OF_YEAR), 6);
}
Aggregations