Search in sources :

Example 26 with DateTimeFormatterBuilder

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);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) LocalDate(java.time.LocalDate) ChronoLocalDate(java.time.chrono.ChronoLocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 27 with DateTimeFormatterBuilder

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);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 28 with DateTimeFormatterBuilder

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);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 29 with DateTimeFormatterBuilder

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();
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 30 with DateTimeFormatterBuilder

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);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Aggregations

DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)244 DateTimeFormatter (java.time.format.DateTimeFormatter)227 TemporalAccessor (java.time.temporal.TemporalAccessor)115 Test (org.testng.annotations.Test)115 Test (org.junit.Test)104 LocalDate (java.time.LocalDate)77 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)69 TemporalField (java.time.temporal.TemporalField)40 AbstractTCKTest (tck.java.time.AbstractTCKTest)36 WeekFields (java.time.temporal.WeekFields)34 DateTimeException (java.time.DateTimeException)26 ParsePosition (java.text.ParsePosition)25 DateTimeParseException (java.time.format.DateTimeParseException)20 Instant (java.time.Instant)16 ZonedDateTime (java.time.ZonedDateTime)10 ResolverStyle (java.time.format.ResolverStyle)10 LocalDateTime (java.time.LocalDateTime)6 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)6 Chronology (java.time.chrono.Chronology)5