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

The following examples show how to use java.time.temporal.WeekFields#weekOfMonth() . 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 openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWom(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    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(DAY_OF_WEEK).toFormatter().withResolverStyle(SMART);
        String str = date.getYear() + ":" + date.getMonthValue() + ":" +
                date.get(womField) + ":" + date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " ::" + str + "::" + i);

        date = date.plusDays(1);
    }
}
 
Example 2
Source File: TCKWeekFields.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_parse_resolve_localizedWom_lenient(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    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(DAY_OF_WEEK).toFormatter().withResolverStyle(LENIENT);
        int wom = date.get(womField);
        int dow = date.get(DAY_OF_WEEK);
        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-jdk9 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 4
Source File: TCKWeekFields.java    From openjdk-8-source 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 5
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_parse_resolve_localizedWom(DayOfWeek firstDayOfWeek, int minDays) {
    LocalDate date = LocalDate.of(2012, 12, 15);
    WeekFields week = WeekFields.of(firstDayOfWeek, minDays);
    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(DAY_OF_WEEK).toFormatter().withResolverStyle(SMART);
        String str = date.getYear() + ":" + date.getMonthValue() + ":" +
                date.get(womField) + ":" + date.get(DAY_OF_WEEK);
        LocalDate parsed = LocalDate.parse(str, f);
        assertEquals(parsed, date, " ::" + str + "::" + i);

        date = date.plusDays(1);
    }
}
 
Example 6
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_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(" ::" + str + ": :" + i + "::" + j, parsed, date.plusWeeks(j - wom));
        }

        date = date.plusDays(1);
    }
}
 
Example 7
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_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 8
Source File: TCKWeekFields.java    From openjdk-jdk8u-backup 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 9
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_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = weekDef.weekOfMonth();
    TemporalField woyField = weekDef.weekOfYear();

    LocalDate day = LocalDate.of(2012, 11, 30);
    LocalDate endDay = LocalDate.of(2013, 1, 2);
    while (day.isBefore(endDay)) {
        LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
        int lastWOM = last.get(womField);
        LocalDate first = day.with(DAY_OF_MONTH, 1);
        int firstWOM = first.get(womField);
        ValueRange rangeWOM = day.range(womField);
        assertEquals(rangeWOM.getMinimum(), firstWOM,
                "Range min should be same as WeekOfMonth for first day of month: "
                + first + ", " + weekDef);
        assertEquals(rangeWOM.getMaximum(), lastWOM,
                "Range max should be same as WeekOfMonth for last day of month: "
                + last + ", " + weekDef);

        last = day.with(DAY_OF_YEAR, day.lengthOfYear());
        int lastWOY = last.get(woyField);
        first = day.with(DAY_OF_YEAR, 1);
        int firstWOY = first.get(woyField);
        ValueRange rangeWOY = day.range(woyField);
        assertEquals(rangeWOY.getMinimum(), firstWOY,
                "Range min should be same as WeekOfYear for first day of Year: "
                + day + ", " + weekDef);
        assertEquals(rangeWOY.getMaximum(), lastWOY,
                "Range max should be same as WeekOfYear for last day of Year: "
                + day + ", " + weekDef);

        day = day.plusDays(1);
    }
}
 
Example 10
Source File: TCKLocalizedFieldParser.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("provider_fieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'e' :
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b
            = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals("Incorrect ending parse position", ppos.getIndex(), expectedPos);
        long value = parsed.getLong(field);
        assertEquals("Value incorrect for " + field, value, expectedValue);
    }
}
 
Example 11
Source File: TCKWeekFields.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfMonthField(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 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(result, day, "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));
        day = day.plusDays(1);
    }
}
 
Example 12
Source File: DateTimeFormatterBuilder.java    From openjdk-8-source 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 13
Source File: DateTimeFormatterBuilder.java    From TencentKona-8 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 14
Source File: TCKWeekFields.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfMonthField(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 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(result, day, "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));
        day = day.plusDays(1);
    }
}
 
Example 15
Source File: TCKWeekFields.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = weekDef.weekOfMonth();
    TemporalField woyField = weekDef.weekOfYear();

    LocalDate day = LocalDate.of(2012, 11, 30);
    LocalDate endDay = LocalDate.of(2013, 1, 2);
    while (day.isBefore(endDay)) {
        LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
        int lastWOM = last.get(womField);
        LocalDate first = day.with(DAY_OF_MONTH, 1);
        int firstWOM = first.get(womField);
        ValueRange rangeWOM = day.range(womField);
        assertEquals(rangeWOM.getMinimum(), firstWOM,
                "Range min should be same as WeekOfMonth for first day of month: "
                + first + ", " + weekDef);
        assertEquals(rangeWOM.getMaximum(), lastWOM,
                "Range max should be same as WeekOfMonth for last day of month: "
                + last + ", " + weekDef);

        last = day.with(DAY_OF_YEAR, day.lengthOfYear());
        int lastWOY = last.get(woyField);
        first = day.with(DAY_OF_YEAR, 1);
        int firstWOY = first.get(woyField);
        ValueRange rangeWOY = day.range(woyField);
        assertEquals(rangeWOY.getMinimum(), firstWOY,
                "Range min should be same as WeekOfYear for first day of Year: "
                + day + ", " + weekDef);
        assertEquals(rangeWOY.getMaximum(), lastWOY,
                "Range max should be same as WeekOfYear for last day of Year: "
                + day + ", " + weekDef);

        day = day.plusDays(1);
    }
}
 
Example 16
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_fieldRanges(DayOfWeek firstDayOfWeek, int minDays) {
    WeekFields weekDef = WeekFields.of(firstDayOfWeek, minDays);
    TemporalField womField = weekDef.weekOfMonth();
    TemporalField woyField = weekDef.weekOfYear();

    LocalDate day = LocalDate.of(2012, 11, 30);
    LocalDate endDay = LocalDate.of(2013, 1, 2);
    while (day.isBefore(endDay)) {
        LocalDate last = day.with(DAY_OF_MONTH, day.lengthOfMonth());
        int lastWOM = last.get(womField);
        LocalDate first = day.with(DAY_OF_MONTH, 1);
        int firstWOM = first.get(womField);
        ValueRange rangeWOM = day.range(womField);
        assertEquals(rangeWOM.getMinimum(), firstWOM,
                "Range min should be same as WeekOfMonth for first day of month: "
                + first + ", " + weekDef);
        assertEquals(rangeWOM.getMaximum(), lastWOM,
                "Range max should be same as WeekOfMonth for last day of month: "
                + last + ", " + weekDef);

        last = day.with(DAY_OF_YEAR, day.lengthOfYear());
        int lastWOY = last.get(woyField);
        first = day.with(DAY_OF_YEAR, 1);
        int firstWOY = first.get(woyField);
        ValueRange rangeWOY = day.range(woyField);
        assertEquals(rangeWOY.getMinimum(), firstWOY,
                "Range min should be same as WeekOfYear for first day of Year: "
                + day + ", " + weekDef);
        assertEquals(rangeWOY.getMaximum(), lastWOY,
                "Range max should be same as WeekOfYear for last day of Year: "
                + day + ", " + weekDef);

        day = day.plusDays(1);
    }
}
 
Example 17
Source File: TCKLocalizedFieldParser.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="FieldPatterns")
public void test_parse_textField(String pattern, String text, int pos, int expectedPos, long expectedValue) {
    WeekFields weekDef = WeekFields.of(locale);
    TemporalField field = null;
    switch(pattern.charAt(0)) {
        case 'e' :
            field = weekDef.dayOfWeek();
            break;
        case 'w':
            field = weekDef.weekOfWeekBasedYear();
            break;
        case 'W':
            field = weekDef.weekOfMonth();
            break;
        case 'Y':
            field = weekDef.weekBasedYear();
            break;
        default:
            throw new IllegalStateException("bad format letter from pattern");
    }
    ParsePosition ppos = new ParsePosition(pos);
    DateTimeFormatterBuilder b
            = new DateTimeFormatterBuilder().appendPattern(pattern);
    DateTimeFormatter dtf = b.toFormatter(locale);
    TemporalAccessor parsed = dtf.parseUnresolved(text, ppos);
    if (ppos.getErrorIndex() != -1) {
        assertEquals(ppos.getErrorIndex(), expectedPos);
    } else {
        assertEquals(ppos.getIndex(), expectedPos, "Incorrect ending parse position");
        long value = parsed.getLong(field);
        assertEquals(value, expectedValue, "Value incorrect for " + field);
    }
}
 
Example 18
Source File: TCKWeekFields.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="weekFields")
public void test_weekOfMonthField(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 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(result, day, "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));
        day = day.plusDays(1);
    }
}
 
Example 19
Source File: TCKWeekFields.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("data_weekFields")
public void test_weekOfMonthField(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 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);
    }
}
 
Example 20
Source File: DateTimeFormatterBuilder.java    From jdk8u-dev-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);
}