Search in sources :

Example 1 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method test_weekOfWeekBasedYearField.

/**
     * Verify that the date can be reconstructed from the DOW, WeekOfWeekBasedYear,
     * and WeekBasedYear for every combination of start of week
     * and minimal days in week.
     * @param firstDayOfWeek the first day of the week
     * @param minDays the minimum number of days in the week
     */
@Test(dataProvider = "weekFields")
public void test_weekOfWeekBasedYearField(DayOfWeek firstDayOfWeek, int minDays) {
    // Known to be ISO Monday
    LocalDate day = LocalDate.of(2012, 12, 31);
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();
    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOWBY = day.get(wowbyField);
        int actualYOWBY = day.get(yowbyField);
        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = LocalDate.of(actualYOWBY, 1, 1);
        DayOfWeek isoDOW = day1.getDayOfWeek();
        int dow = (7 + isoDOW.getValue() - firstDayOfWeek.getValue()) % 7 + 1;
        int weekStart = Math.floorMod(1 - dow, 7);
        if (weekStart + 1 > weekDef.getMinimalDaysInFirstWeek()) {
            // The previous week has the minimum days in the current month to be a 'week'
            weekStart -= 7;
        }
        weekStart += actualDOW - 1;
        weekStart += (actualWOWBY - 1) * 7;
        LocalDate result = day1.plusDays(weekStart);
        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear " + String.format("%s, ISO Dow: %s, weekStart: %s, actualDOW: %s, actualWOWBY: %s, YearOfWBY: %d, expected day: %s, result: %s%n", weekDef, day.getDayOfWeek(), weekStart, actualDOW, actualWOWBY, actualYOWBY, day, result));
        day = day.plusDays(1);
    }
}
Also used : DayOfWeek(java.time.DayOfWeek) 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 2 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKWeekFields method data_weekFields.

//-----------------------------------------------------------------------
@DataProvider(name = "weekFields")
Object[][] data_weekFields() {
    Object[][] objects = new Object[49][];
    int i = 0;
    for (DayOfWeek firstDayOfWeek : DayOfWeek.values()) {
        for (int minDays = 1; minDays <= 7; minDays++) {
            objects[i++] = new Object[] { firstDayOfWeek, minDays };
        }
    }
    return objects;
}
Also used : DayOfWeek(java.time.DayOfWeek) DataProvider(org.testng.annotations.DataProvider)

Example 3 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKTemporalAdjusters method test_previousOrCurrent.

@Test
public void test_previousOrCurrent() {
    for (Month month : Month.values()) {
        for (int i = 1; i <= month.length(false); i++) {
            LocalDate date = date(2007, month, i);
            for (DayOfWeek dow : DayOfWeek.values()) {
                LocalDate test = (LocalDate) TemporalAdjusters.previousOrSame(dow).adjustInto(date);
                assertSame(test.getDayOfWeek(), dow);
                if (test.getYear() == 2007) {
                    int dayDiff = test.getDayOfYear() - date.getDayOfYear();
                    assertTrue(dayDiff <= 0 && dayDiff > -7);
                    assertEquals(date.equals(test), date.getDayOfWeek() == dow);
                } else {
                    assertFalse(date.getDayOfWeek() == dow);
                    assertSame(month, Month.JANUARY);
                    assertTrue(date.getDayOfMonth() < 7);
                    assertEquals(test.getYear(), 2006);
                    assertSame(test.getMonth(), Month.DECEMBER);
                    assertTrue(test.getDayOfMonth() > 25);
                }
            }
        }
    }
}
Also used : Month(java.time.Month) DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate) Test(org.testng.annotations.Test)

Example 4 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKLocalDateTime method test_getDayOfWeek.

//-----------------------------------------------------------------------
// getDayOfWeek()
//-----------------------------------------------------------------------
@Test
public void test_getDayOfWeek() {
    DayOfWeek dow = DayOfWeek.MONDAY;
    for (Month month : Month.values()) {
        int length = month.length(false);
        for (int i = 1; i <= length; i++) {
            LocalDateTime d = LocalDateTime.of(LocalDate.of(2007, month, i), TEST_2007_07_15_12_30_40_987654321.toLocalTime());
            assertSame(d.getDayOfWeek(), dow);
            dow = dow.plus(1);
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DayOfWeek(java.time.DayOfWeek) Month(java.time.Month) Test(org.testng.annotations.Test)

Example 5 with DayOfWeek

use of java.time.DayOfWeek in project jdk8u_jdk by JetBrains.

the class TCKIsoFields method test_loop.

//-----------------------------------------------------------------------
public void test_loop() {
    // loop round at least one 400 year cycle, including before 1970
    // Tuseday of week 1 1960
    LocalDate date = LocalDate.of(1960, 1, 5);
    int year = 1960;
    int wby = 1960;
    int weekLen = 52;
    int week = 1;
    while (date.getYear() < 2400) {
        DayOfWeek loopDow = date.getDayOfWeek();
        if (date.getYear() != year) {
            year = date.getYear();
        }
        if (loopDow == MONDAY) {
            week++;
            if ((week == 53 && weekLen == 52) || week == 54) {
                week = 1;
                LocalDate firstDayOfWeekBasedYear = date.plusDays(14).withDayOfYear(1);
                DayOfWeek firstDay = firstDayOfWeekBasedYear.getDayOfWeek();
                weekLen = (firstDay == THURSDAY || (firstDay == WEDNESDAY && firstDayOfWeekBasedYear.isLeapYear()) ? 53 : 52);
                wby++;
            }
        }
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek());
        assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek());
        date = date.plusDays(1);
    }
}
Also used : DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate)

Aggregations

DayOfWeek (java.time.DayOfWeek)18 LocalDate (java.time.LocalDate)10 Test (org.testng.annotations.Test)9 Month (java.time.Month)8 LocalDateTime (java.time.LocalDateTime)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2 WeekFields (java.time.temporal.WeekFields)2 DataProvider (org.testng.annotations.DataProvider)2 Instant (java.time.Instant)1 LocalTime (java.time.LocalTime)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 TemporalField (java.time.temporal.TemporalField)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 Locale (java.util.Locale)1 Callable (java.util.concurrent.Callable)1 AbstractTCKTest (tck.java.time.AbstractTCKTest)1