Search in sources :

Example 26 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatter method test_parse_CharSequence_ParsePosition_resolved.

@Test
public void test_parse_CharSequence_ParsePosition_resolved() {
    DateTimeFormatter test = DateTimeFormatter.ISO_DATE;
    ParsePosition pos = new ParsePosition(3);
    TemporalAccessor result = test.parse("XXX2012-06-30XXX", pos);
    assertEquals(pos.getIndex(), 13);
    assertEquals(pos.getErrorIndex(), -1);
    assertEquals(result.isSupported(YEAR), true);
    assertEquals(result.isSupported(MONTH_OF_YEAR), true);
    assertEquals(result.isSupported(DAY_OF_MONTH), true);
    assertEquals(result.isSupported(HOUR_OF_DAY), false);
    assertEquals(result.getLong(YEAR), 2012L);
    assertEquals(result.getLong(MONTH_OF_YEAR), 6L);
    assertEquals(result.getLong(DAY_OF_MONTH), 30L);
    assertEquals(result.query(LocalDate::from), LocalDate.of(2012, 6, 30));
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) ParsePosition(java.text.ParsePosition) Test(org.testng.annotations.Test)

Example 27 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatter method test_resolverFields_listOfOneNotMatching.

@Test
public void test_resolverFields_listOfOneNotMatching() throws Exception {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).toFormatter().withResolverFields(MONTH_OF_YEAR);
    TemporalAccessor parsed = f.parse("2012");
    // not in the list of resolverFields
    assertEquals(parsed.isSupported(YEAR), false);
    assertEquals(parsed.isSupported(MONTH_OF_YEAR), false);
}
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 TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatters method test_print_isoLocalDate.

@Test(dataProvider = "sample_isoLocalDate")
public void test_print_isoLocalDate(Integer year, Integer month, Integer day, String offsetId, String zoneId, String expected, Class<?> expectedEx) {
    TemporalAccessor test = buildAccessor(year, month, day, null, null, null, null, offsetId, zoneId);
    if (expectedEx == null) {
        assertEquals(DateTimeFormatter.ISO_LOCAL_DATE.format(test), expected);
    } else {
        try {
            DateTimeFormatter.ISO_LOCAL_DATE.format(test);
            fail();
        } catch (Exception ex) {
            assertTrue(expectedEx.isInstance(ex));
        }
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) DateTimeParseException(java.time.format.DateTimeParseException) Test(org.testng.annotations.Test)

Example 29 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TCKDateTimeFormatters method test_print_rfc1123.

@Test(dataProvider = "rfc")
public void test_print_rfc1123(LocalDateTime base, String offsetId, String expected) {
    TemporalAccessor test = buildAccessor(base, offsetId, null);
    assertEquals(DateTimeFormatter.RFC_1123_DATE_TIME.format(test), expected);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) Test(org.testng.annotations.Test)

Example 30 with TemporalAccessor

use of java.time.temporal.TemporalAccessor in project jdk8u_jdk by JetBrains.

the class TCKDateTimeParseResolver method test_resolveFourToDate.

@Test(dataProvider = "resolveFourToDate")
public void test_resolveFourToDate(TemporalField field1, long value1, TemporalField field2, long value2, TemporalField field3, long value3, TemporalField field4, long value4, LocalDate expectedDate) {
    String str = value1 + " " + value2 + " " + value3 + " " + value4;
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(field1).appendLiteral(' ').appendValue(field2).appendLiteral(' ').appendValue(field3).appendLiteral(' ').appendValue(field4).toFormatter();
    TemporalAccessor accessor = f.parse(str);
    assertEquals(accessor.query(TemporalQueries.localDate()), expectedDate);
    assertEquals(accessor.query(TemporalQueries.localTime()), null);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Aggregations

TemporalAccessor (java.time.temporal.TemporalAccessor)217 Test (org.testng.annotations.Test)189 DateTimeFormatter (java.time.format.DateTimeFormatter)101 ParsePosition (java.text.ParsePosition)82 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)61 DateTimeException (java.time.DateTimeException)25 DateTimeParseException (java.time.format.DateTimeParseException)21 TemporalField (java.time.temporal.TemporalField)13 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)5 ChronoLocalDate (java.time.chrono.ChronoLocalDate)5 Chronology (java.time.chrono.Chronology)5 ResolverStyle (java.time.format.ResolverStyle)5 ChronoZonedDateTime (java.time.chrono.ChronoZonedDateTime)4 IsoChronology (java.time.chrono.IsoChronology)4 Format (java.text.Format)3 LocalTime (java.time.LocalTime)3 ZonedDateTime (java.time.ZonedDateTime)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3