Java Code Examples for java.time.temporal.WeekFields#dayOfWeek()

The following examples show how to use java.time.temporal.WeekFields#dayOfWeek() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKWeekFields.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    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(dowField).toFormatter().withResolverStyle(LENIENT);
        int wom = date.get(womField);
        int dow = date.get(dowField);
        for (int j = wom - 10; j < wom + 10; j++) {
            String str = date.getYear() + ":" + date.getMonthValue() + ":" + j + ":" + dow;
            LocalDate parsed = LocalDate.parse(str, f);
            assertEquals(parsed, date.plusWeeks(j - wom), " ::" + str + ": :" + i + "::" + j);
        }

        date = date.plusDays(1);
    }
}
 
Example 3
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example 4
Source File: TCKWeekFields.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 5
Source File: TCKWeekFields.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withDayOfWeek(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 15);  // Safely in the middle of a month
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();
    TemporalField woyField = week.weekOfYear();

    int wom = day.get(womField);
    int woy = day.get(woyField);
    for (int dow = 1; dow <= 7; dow++) {
        LocalDate result = day.with(dowField, dow);
        assertEquals(result.get(dowField), dow, String.format("Incorrect new Day of week: %s", result));
        assertEquals(result.get(womField), wom, "Week of Month should not change");
        assertEquals(result.get(woyField), woy, "Week of Year should not change");
    }
}
 
Example 6
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWoyDow(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 60; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(MONTH_OF_YEAR).appendLiteral(':')
                .appendValue(woyField).appendLiteral(':')
                .appendValue(dowField).toFormatter();
        String str = date.getYear() + ":" + date.getMonthValue() + ":" +
                date.get(woyField) + ":" + date.get(dowField);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " :: " + str + " " + i);

        date = date.plusDays(1);
    }
}
 
Example 7
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWomDow(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField womField = week.weekOfMonth();

    for (int i = 1; i <= 15; i++) {
        DateTimeFormatter f = new DateTimeFormatterBuilder()
                .appendValue(YEAR).appendLiteral(':')
                .appendValue(MONTH_OF_YEAR).appendLiteral(':')
                .appendValue(womField).appendLiteral(':')
                .appendValue(dowField).toFormatter();
        String str = date.getYear() + ":" + date.getMonthValue() + ":" +
                date.get(womField) + ":" + date.get(dowField);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " :: " + str + " " + i);

        date = date.plusDays(1);
    }
}
 
Example 8
Source File: TCKWeekFields.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="WeekBasedYearData")
public void test_weekBasedYears(WeekFields weekDef, int weekBasedYear,
        int weekOfWeekBasedYear, int dayOfWeek, LocalDate date) {
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();
    assertEquals(date.get(dowField), dayOfWeek, "DayOfWeek mismatch");
    assertEquals(date.get(wowbyField), weekOfWeekBasedYear, "Week of WeekBasedYear mismatch");
    assertEquals(date.get(yowbyField), weekBasedYear, "Year of WeekBasedYear mismatch");
}
 
Example 9
Source File: TCKWeekFields.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOY = day.get(woyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = - (day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 10
Source File: TCKWeekFields.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="IsoWeekData")
public void test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby) {
    WeekFields weekDef = WeekFields.ISO;
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    assertEquals(date.get(dowField), dow.getValue());
    assertEquals(date.get(wowbyField), week);
    assertEquals(date.get(yowbyField), wby);
}
 
Example 11
Source File: TCKWeekFields.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="WeekBasedYearData")
public void test_weekBasedYears(WeekFields weekDef, int weekBasedYear,
        int weekOfWeekBasedYear, int dayOfWeek, LocalDate date) {
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();
    assertEquals(date.get(dowField), dayOfWeek, "DayOfWeek mismatch");
    assertEquals(date.get(wowbyField), weekOfWeekBasedYear, "Week of WeekBasedYear mismatch");
    assertEquals(date.get(yowbyField), weekBasedYear, "Year of WeekBasedYear mismatch");
}
 
Example 12
Source File: TCKWeekFields.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_withWeekOfWeekBasedYear(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField wowbyField = week.weekOfWeekBasedYear();
    TemporalField yowbyField = week.weekBasedYear();

    int dowExpected = (day.get(dowField) - 1) % 7 + 1;
    LocalDate dowDate = day.with(dowField, dowExpected);
    int dowResult = dowDate.get(dowField);
    assertEquals(dowResult, dowExpected, "Localized DayOfWeek not correct; " + day + " -->" + dowDate);

    int weekExpected = day.get(wowbyField) + 1;
    ValueRange range = day.range(wowbyField);
    weekExpected = ((weekExpected - 1) % (int)range.getMaximum()) + 1;
    LocalDate weekDate = day.with(wowbyField, weekExpected);
    int weekResult = weekDate.get(wowbyField);
    assertEquals(weekResult, weekExpected, "Localized WeekOfWeekBasedYear not correct; " + day + " -->" + weekDate);

    int yearExpected = day.get(yowbyField) + 1;

    LocalDate yearDate = day.with(yowbyField, yearExpected);
    int yearResult = yearDate.get(yowbyField);
    assertEquals(yearResult, yearExpected, "Localized WeekBasedYear not correct; " + day  + " --> " + yearDate);

    range = yearDate.range(wowbyField);
    weekExpected = Math.min(day.get(wowbyField), (int)range.getMaximum());

    int weekActual = yearDate.get(wowbyField);
    assertEquals(weekActual, weekExpected, "Localized WeekOfWeekBasedYear week should not change; " + day + " --> " + yearDate + ", actual: " + weekActual + ", weekExpected: " + weekExpected);
}
 
Example 13
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOY = day.get(woyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = - (day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 14
Source File: DateTimeFormatterBuilder.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the printerParser to use based on the field and the locale.
 *
 * @param locale  the locale to use, not null
 * @return the formatter, not null
 * @throws IllegalArgumentException if the formatter cannot be found
 */
private DateTimePrinterParser printerParser(Locale locale) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch (chr) {
        case 'Y':
            field = weekDef.weekBasedYear();
            if (count == 2) {
                return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0);
            } else {
                return new NumberPrinterParser(field, count, 19,
                        (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1);
            }
        case 'e':
        case 'c':
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        default:
            throw new IllegalStateException("unreachable");
    }
    return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE);
}
 
Example 15
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOY = day.get(woyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = - (day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 16
Source File: TCKWeekFields.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfYearField(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate day = LocalDate.of(2012, 12, 31);  // Known to be ISO Monday
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField dowField = week.dayOfWeek();
    TemporalField woyField = week.weekOfYear();

    for (int i = 1; i <= 15; i++) {
        int actualDOW = day.get(dowField);
        int actualWOY = day.get(woyField);

        // Verify that the combination of day of week and week of month can be used
        // to reconstruct the same date.
        LocalDate day1 = day.withDayOfYear(1);
        int offset = - (day1.get(dowField) - 1);
        int week1 = day1.get(woyField);
        if (week1 == 0) {
            // week of the 1st is partial; start with first full week
            offset += 7;
        }
        offset += actualDOW - 1;
        offset += (actualWOY - 1) * 7;
        LocalDate result = day1.plusDays(offset);

        assertEquals(result, day, "Incorrect dayOfWeek or weekOfYear "
                + String.format("%s, ISO Dow: %s, offset: %s, actualDOW: %s, actualWOM: %s, expected: %s, result: %s%n",
                week, day.getDayOfWeek(), offset, actualDOW, actualWOY, day, result));
        day = day.plusDays(1);
    }
}
 
Example 17
Source File: TCKWeekFields.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="IsoWeekData")
public void test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby) {
    WeekFields weekDef = WeekFields.ISO;
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    assertEquals(date.get(dowField), dow.getValue());
    assertEquals(date.get(wowbyField), week);
    assertEquals(date.get(yowbyField), wby);
}
 
Example 18
Source File: TCKWeekFields.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="IsoWeekData")
public void test_WOWBY(LocalDate date, DayOfWeek dow, int week, int wby) {
    WeekFields weekDef = WeekFields.ISO;
    TemporalField dowField = weekDef.dayOfWeek();
    TemporalField wowbyField = weekDef.weekOfWeekBasedYear();
    TemporalField yowbyField = weekDef.weekBasedYear();

    assertEquals(date.get(dowField), dow.getValue());
    assertEquals(date.get(wowbyField), week);
    assertEquals(date.get(yowbyField), wby);
}
 
Example 19
Source File: DateTimeFormatterBuilder.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the printerParser to use based on the field and the locale.
 *
 * @param locale  the locale to use, not null
 * @return the formatter, not null
 * @throws IllegalArgumentException if the formatter cannot be found
 */
private DateTimePrinterParser printerParser(Locale locale) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch (chr) {
        case 'Y':
            field = weekDef.weekBasedYear();
            if (count == 2) {
                return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0);
            } else {
                return new NumberPrinterParser(field, count, 19,
                        (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1);
            }
        case 'e':
        case 'c':
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        default:
            throw new IllegalStateException("unreachable");
    }
    return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE);
}
 
Example 20
Source File: DateTimeFormatterBuilder.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the printerParser to use based on the field and the locale.
 *
 * @param locale  the locale to use, not null
 * @return the formatter, not null
 * @throws IllegalArgumentException if the formatter cannot be found
 */
private DateTimePrinterParser printerParser(Locale locale) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch (chr) {
        case 'Y':
            field = weekDef.weekBasedYear();
            if (count == 2) {
                return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0);
            } else {
                return new NumberPrinterParser(field, count, 19,
                        (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1);
            }
        case 'e':
        case 'c':
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        default:
            throw new IllegalStateException("unreachable");
    }
    return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE);
}