Search in sources :

Example 11 with TemporalField

use of java.time.temporal.TemporalField 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 12 with TemporalField

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

the class TCKThaiBuddhistChronology method test_resolve_yd_lenient.

@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_lenient(int y, int d, ThaiBuddhistDate expected, boolean smart, boolean strict) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR, (long) y);
    fieldValues.put(ChronoField.DAY_OF_YEAR, (long) d);
    ThaiBuddhistDate date = ThaiBuddhistChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.LENIENT);
    assertEquals(date, expected);
    assertEquals(fieldValues.size(), 0);
}
Also used : ThaiBuddhistDate(java.time.chrono.ThaiBuddhistDate) TemporalField(java.time.temporal.TemporalField) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 13 with TemporalField

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

the class AbstractDateTimeTest method basicTest_getLong_TemporalField_unsupported.

@Test()
public void basicTest_getLong_TemporalField_unsupported() {
    for (TemporalAccessor sample : samples()) {
        for (TemporalField field : invalidFields()) {
            try {
                sample.getLong(field);
                fail("Failed on " + sample + " " + field);
            } catch (DateTimeException ex) {
            // expected
            }
        }
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) Test(org.testng.annotations.Test)

Example 14 with TemporalField

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

the class TCKChronoLocalDate method test_badTemporalFieldChrono.

@Test(dataProvider = "calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDate date = chrono.date(refDate);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDate date2 = chrono2.date(refDate);
        TemporalField adjuster = new FixedTemporalField(date2);
        if (chrono != chrono2) {
            try {
                date.with(adjuster, 1);
                Assert.fail("TemporalField doSet should have thrown a ClassCastException" + date.getClass() + ", can not be cast to " + date2.getClass());
            } catch (ClassCastException cce) {
            // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDate result = date.with(adjuster, 1);
            assertEquals(result, date2, "TemporalField doSet failed to replace date");
        }
    }
}
Also used : ChronoLocalDate(java.time.chrono.ChronoLocalDate) TemporalField(java.time.temporal.TemporalField) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) ChronoLocalDate(java.time.chrono.ChronoLocalDate) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 15 with TemporalField

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

the class TCKChronoZonedDateTime method test_badTemporalFieldChrono.

@Test(dataProvider = "calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC);
        TemporalField adjuster = new FixedTemporalField(czdt2);
        if (chrono != chrono2) {
            try {
                czdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException, " + czdt.getClass() + " can not be cast to " + czdt2.getClass());
            } catch (ClassCastException cce) {
            // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoZonedDateTime<?> result = czdt.with(adjuster, 1);
            assertEquals(result, czdt2, "TemporalField doWith() failed to replace date");
        }
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) HijrahChronology(java.time.chrono.HijrahChronology) ThaiBuddhistChronology(java.time.chrono.ThaiBuddhistChronology) IsoChronology(java.time.chrono.IsoChronology) Chronology(java.time.chrono.Chronology) MinguoChronology(java.time.chrono.MinguoChronology) JapaneseChronology(java.time.chrono.JapaneseChronology) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Aggregations

TemporalField (java.time.temporal.TemporalField)104 Test (org.testng.annotations.Test)95 HashMap (java.util.HashMap)56 DateTimeException (java.time.DateTimeException)48 LocalDate (java.time.LocalDate)38 WeekFields (java.time.temporal.WeekFields)25 AbstractTCKTest (tck.java.time.AbstractTCKTest)25 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)20 DateTimeFormatter (java.time.format.DateTimeFormatter)19 TemporalAccessor (java.time.temporal.TemporalAccessor)13 JapaneseDate (java.time.chrono.JapaneseDate)12 HijrahDate (java.time.chrono.HijrahDate)11 MinguoDate (java.time.chrono.MinguoDate)10 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)10 Chronology (java.time.chrono.Chronology)4 IsoChronology (java.time.chrono.IsoChronology)4 ChronoLocalDate (java.time.chrono.ChronoLocalDate)3 HijrahChronology (java.time.chrono.HijrahChronology)3 JapaneseChronology (java.time.chrono.JapaneseChronology)3 MinguoChronology (java.time.chrono.MinguoChronology)3