Search in sources :

Example 21 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_matches.

//-------------------------------------------------------------------------
@Test
public void test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_matches() {
    LocalDateTime ldt = LocalDateTime.of(2010, 6, 30, 12, 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()), LocalTime.of(12, 30));
    assertEquals(accessor.query(TemporalQueries.chronology()), IsoChronology.INSTANCE);
}
Also used : ChronoLocalDateTime(java.time.chrono.ChronoLocalDateTime) LocalDateTime(java.time.LocalDateTime) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 22 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_resolveTwoNoChange.

@Test(dataProvider = "resolveTwoNoChange")
public void test_resolveTwoNoChange(TemporalField field1, long value1, TemporalField field2, long value2) {
    String str = value1 + " " + value2;
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).appendLiteral(' ').appendValue(field2).toFormatter();
    TemporalAccessor accessor = f.parse(str);
    assertEquals(accessor.query(TemporalQueries.localDate()), null);
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
    assertEquals(accessor.isSupported(field1), true);
    assertEquals(accessor.isSupported(field2), true);
    assertEquals(accessor.getLong(field1), value1);
    assertEquals(accessor.getLong(field2), value2);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 23 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches.

@Test
public void test_fieldResolvesToChronoZonedDateTime_overrideChrono_matches() {
    MinguoDate mdt = MinguoDate.of(100, 6, 30);
    ChronoZonedDateTime<MinguoDate> mzdt = mdt.atTime(LocalTime.NOON).atZone(EUROPE_PARIS);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(mzdt)).toFormatter();
    f = f.withChronology(MinguoChronology.INSTANCE);
    TemporalAccessor accessor = f.parse("1234567890");
    assertEquals(accessor.query(TemporalQueries.localDate()), LocalDate.from(mdt));
    assertEquals(accessor.query(TemporalQueries.localTime()), LocalTime.NOON);
    assertEquals(accessor.query(TemporalQueries.chronology()), MinguoChronology.INSTANCE);
    assertEquals(accessor.query(TemporalQueries.zoneId()), EUROPE_PARIS);
}
Also used : MinguoDate(java.time.chrono.MinguoDate) TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 24 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_wrongChrono.

@Test(expectedExceptions = DateTimeParseException.class)
public void test_fieldResolvesToChronoLocalDateTime_noOverrideChrono_wrongChrono() {
    ChronoLocalDateTime<?> cldt = ThaiBuddhistChronology.INSTANCE.dateNow().atTime(LocalTime.NOON);
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(new ResolvingField(cldt)).toFormatter();
    f.parse("1234567890");
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 25 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_parse_fromField_InstantSeconds_NanoOfSecond.

@Test
public void test_parse_fromField_InstantSeconds_NanoOfSecond() {
    DateTimeFormatter fmt = new DateTimeFormatterBuilder().appendValue(INSTANT_SECONDS).appendLiteral('.').appendValue(NANO_OF_SECOND).toFormatter();
    TemporalAccessor acc = fmt.parse("86402.123456789");
    Instant expected = Instant.ofEpochSecond(86402, 123456789);
    assertEquals(acc.isSupported(INSTANT_SECONDS), true);
    assertEquals(acc.isSupported(NANO_OF_SECOND), true);
    assertEquals(acc.isSupported(MICRO_OF_SECOND), true);
    assertEquals(acc.isSupported(MILLI_OF_SECOND), true);
    assertEquals(acc.getLong(INSTANT_SECONDS), 86402L);
    assertEquals(acc.getLong(NANO_OF_SECOND), 123456789L);
    assertEquals(acc.getLong(MICRO_OF_SECOND), 123456L);
    assertEquals(acc.getLong(MILLI_OF_SECOND), 123L);
    assertEquals(Instant.from(acc), expected);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) Instant(java.time.Instant) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) 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