Search in sources :

Example 56 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_parse_resolve_localizedWoy_lenient.

@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoy_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(woyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int woy = date.get(woyField);
        int dow = date.get(DAY_OF_WEEK);
        for (int j = woy - 60; j < woy + 60; j++) {
            String str = date.getYear() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(" ::" + str + ": :" + i + "::" + j, parsed, date.plusWeeks(j - woy));
        }
        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.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 57 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_weekOfMonthField.

@Test
@UseDataProvider("data_weekFields")
public void test_weekOfMonthField(DayOfWeek firstDayOfWeek, int minDays) {
    // Known to be ISO Monday
    LocalDate day = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOM = day.get(womField);
        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfMonth(1);
        int offset = -(day1.get(dowField) - 1);
        int week1 = day1.get(womField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOM - 1) * 7;
        LocalDate result = day1.plusDays(offset);
        assertEquals("Incorrect dayOfWeek or weekOfMonth: " + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n", week, day.getDayOfWeek(), offset, actualDOW, actualWOM, day, result), result, day);
        day = day.plusDays(1);
    }
}
Also used : TemporalField(java.time.temporal.TemporalField) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate) Test(org.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 58 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_parse_resolve_localizedWoWBYDow_lenient.

@Test
@UseDataProvider("data_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(" ::" + str + ": :" + i + "::" + j, parsed, date.plusWeeks(j - wowby));
        }
        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.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 59 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_parse_resolve_localizedWoy.

// -----------------------------------------------------------------------
@Test
@UseDataProvider("data_weekFields")
public void test_parse_resolve_localizedWoy(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField woyField = week.weekOfYear();
    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder().appendValue(YEAR).appendLiteral(':').appendValue(woyField).appendLiteral(':').appendValue(DAY_OF_WEEK).toFormatter();
        String str = date.getYear() + ":" + date.get(woyField) + ":" + date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(" :: " + str + " " + i, parsed, date);
        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.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 60 with WeekFields

use of java.time.temporal.WeekFields in project j2objc by google.

the class TCKWeekFields method test_basics.

// -----------------------------------------------------------------------
@Test
@UseDataProvider("data_weekFields")
public void test_basics(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    assertEquals(week.dayOfWeek().isDateBased(), true);
    assertEquals(week.dayOfWeek().isTimeBased(), false);
    assertEquals(week.weekOfMonth().isDateBased(), true);
    assertEquals(week.weekOfMonth().isTimeBased(), false);
    assertEquals(week.weekOfYear().isDateBased(), true);
    assertEquals(week.weekOfYear().isTimeBased(), false);
    assertEquals(week.weekOfWeekBasedYear().isDateBased(), true);
    assertEquals(week.weekOfWeekBasedYear().isTimeBased(), false);
    assertEquals(week.weekBasedYear().isDateBased(), true);
    assertEquals(week.weekBasedYear().isTimeBased(), false);
}
Also used : WeekFields(java.time.temporal.WeekFields) Test(org.junit.Test) AbstractTCKTest(tck.java.time.AbstractTCKTest) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

WeekFields (java.time.temporal.WeekFields)73 AbstractTCKTest (tck.java.time.AbstractTCKTest)58 LocalDate (java.time.LocalDate)57 TemporalField (java.time.temporal.TemporalField)51 DateTimeFormatter (java.time.format.DateTimeFormatter)36 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)34 Test (org.junit.Test)33 Test (org.testng.annotations.Test)33 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)31 DayOfWeek (java.time.DayOfWeek)7 DateTimeException (java.time.DateTimeException)6 ValueRange (java.time.temporal.ValueRange)6 ParsePosition (java.text.ParsePosition)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 GregorianCalendar (java.util.GregorianCalendar)2