Search in sources :

Example 81 with TemporalField

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

the class TCKWeekFields method test_dayOfWeekField.

@Test(dataProvider = "weekFields")
public void test_dayOfWeekField(DayOfWeek firstDayOfWeek, int minDays) {
    // Known to be ISO Monday
    LocalDate day = LocalDate.of(2000, 1, 10);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField f = week.dayOfWeek();
    for (int i = 1; i <= 7; i++) {
        assertEquals(day.get(f), (7 + day.getDayOfWeek().getValue() - firstDayOfWeek.getValue()) % 7 + 1);
        day = day.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest)

Example 82 with TemporalField

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

the class TCKWeekFields method test_parse_resolve_localizedWoWBYDow_lenient.

@Test(dataProvider = "weekFields")
public void test_parse_resolve_localizedWoWBYDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(yowbyField).appendLiteral(':').appendValue(wowbyField).appendLiteral(':').appendValue(dowField).toFormatter().withResolverStyle(LENIENT);
        int wowby = date.get(wowbyField);
        int dow = date.get(dowField);
        for (int j = wowby - 60; j < wowby + 60; j++) {
            String str = date.get(yowbyField) + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wowby), " ::" + str + ": :" + i + "::" + j);
        }
        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)

Example 83 with TemporalField

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

the class TCKHijrahChronology method test_resolve_yearOfEra_yearOfEraOnly_valid.

@Test(dataProvider = "resolve_styles")
public void test_resolve_yearOfEra_yearOfEraOnly_valid(ResolverStyle style) {
    Map<TemporalField, Long> fieldValues = new HashMap<>();
    fieldValues.put(ChronoField.YEAR_OF_ERA, 1343L);
    HijrahDate date = HijrahChronology.INSTANCE.resolveDate(fieldValues, style);
    assertEquals(date, null);
    assertEquals(fieldValues.get(ChronoField.YEAR_OF_ERA), (style != ResolverStyle.STRICT) ? null : (Long) 1343L);
    assertEquals(fieldValues.get(ChronoField.YEAR), (style == ResolverStyle.STRICT) ? null : (Long) 1343L);
    assertEquals(fieldValues.size(), 1);
}
Also used : HijrahDate(java.time.chrono.HijrahDate) TemporalField(java.time.temporal.TemporalField) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Example 84 with TemporalField

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

the class TCKChronoLocalDateTime method test_badTemporalFieldChrono.

@Test(dataProvider = "calendars")
public void test_badTemporalFieldChrono(Chronology chrono) {
    LocalDate refDate = LocalDate.of(2013, 1, 1);
    ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON);
    for (Chronology[] clist : data_of_calendars()) {
        Chronology chrono2 = clist[0];
        ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON);
        TemporalField adjuster = new FixedTemporalField(cdt2);
        if (chrono != chrono2) {
            try {
                cdt.with(adjuster, 1);
                Assert.fail("TemporalField doWith() should have thrown a ClassCastException" + cdt.getClass() + ", can not be cast to " + cdt2.getClass());
            } catch (ClassCastException cce) {
            // Expected exception; not an error
            }
        } else {
            // Same chronology,
            ChronoLocalDateTime<?> result = cdt.with(adjuster, 1);
            assertEquals(result, cdt2, "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)

Example 85 with TemporalField

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

the class TCKMinguoChronology method test_resolve_yd_strict.

@Test(dataProvider = "resolve_yd")
public void test_resolve_yd_strict(int y, int d, MinguoDate 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);
    if (strict) {
        MinguoDate date = MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
        assertEquals(date, expected);
        assertEquals(fieldValues.size(), 0);
    } else {
        try {
            MinguoChronology.INSTANCE.resolveDate(fieldValues, ResolverStyle.STRICT);
            fail("Should have failed");
        } catch (DateTimeException ex) {
        // expected
        }
    }
}
Also used : MinguoDate(java.time.chrono.MinguoDate) TemporalField(java.time.temporal.TemporalField) DateTimeException(java.time.DateTimeException) HashMap(java.util.HashMap) 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