Search in sources :

Example 31 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_selectOneDateResolveYD.

@Test
public void test_resolverFields_selectOneDateResolveYD() 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, DAY_OF_YEAR);
    Set<TemporalField> expected = new HashSet<>(Arrays.asList(YEAR, DAY_OF_YEAR));
    // Use set.equals();  testNG comparison of Collections is ordered
    assertTrue(f.getResolverFields().equals(expected), "ResolveFields: " + f.getResolverFields());
    try {
        // wrong month/day-of-month
        base.parse("2012-6-30-321", LocalDate::from);
        fail();
    } catch (DateTimeException ex) {
    // expected, fails as it produces two different dates
    }
    // ignored month/day-of-month
    LocalDate parsed = f.parse("2012-6-30-321", LocalDate::from);
    assertEquals(parsed, LocalDate.of(2012, 11, 16));
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 32 with DateTimeFormatterBuilder

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

the class TCKDateTimeFormatter method test_resolverFields_listOfOneMatching.

@Test
public void test_resolverFields_listOfOneMatching() throws Exception {
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).toFormatter().withResolverFields(YEAR);
    TemporalAccessor parsed = f.parse("2012");
    assertEquals(parsed.isSupported(YEAR), true);
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test)

Example 33 with DateTimeFormatterBuilder

use of java.time.format.DateTimeFormatterBuilder 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 34 with DateTimeFormatterBuilder

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

the class TCKWeekFields method test_parse_resolve_localizedWoy_strict.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoy_strict(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();
    DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(woyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(STRICT);
    String str = "2012:0:1";
    try {
        LocalDate date = LocalDate.parse(str, f);
        assertEquals(date.getYear(), 2012);
        assertEquals(date.get(woyField), 0);
        assertEquals(date.get(DAY_OF_WEEK), 1);
    } catch (DateTimeException ex) {
    // expected
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) WeekFields(java.time.temporal.WeekFields) DateTimeFormatter(java.time.format.DateTimeFormatter) LocalDate(java.time.LocalDate) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 35 with DateTimeFormatterBuilder

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

the class TCKWeekFields method test_parse_resolve_localizedWom.

//-----------------------------------------------------------------------
@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWom(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = week.weekOfMonth();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(MONTH_OF_YEAR).appendLiteral(':').appendValue(womField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(SMART);
        String str = date.getYear() + ":" + date.getMonthValue() + ":" + date.get(womField) + ":" + date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " ::" + str + "::" + i);
        date = date.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

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