Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,23 @@ protected int compare(final Calendar value, final Calendar compare, final int fi

int result;

// Week of Year is relative to its own week-year, which at the year boundary can differ
// from the calendar year (for example 31 December may fall in week 1 of the following
// week-year), so the week-year is compared rather than YEAR for this field.
if (field == Calendar.WEEK_OF_YEAR) {
result = Integer.compare(weekYear(value), weekYear(compare));
if (result != 0) {
return result;
}
return calculateCompareResult(value, compare, Calendar.WEEK_OF_YEAR);
}

// Compare Year
result = calculateCompareResult(value, compare, Calendar.YEAR);
if (result != 0 || field == Calendar.YEAR) {
return result;
}

// Compare Week of Year
if (field == Calendar.WEEK_OF_YEAR) {
return calculateCompareResult(value, compare, Calendar.WEEK_OF_YEAR);
}

// Compare Day of the Year
if (field == Calendar.DAY_OF_YEAR) {
return calculateCompareResult(value, compare, Calendar.DAY_OF_YEAR);
Expand Down Expand Up @@ -416,4 +422,14 @@ protected Object parse(String value, final String pattern, final Locale locale,
*/
@Override
protected abstract Object processParsedValue(Object value, Format formatter);

/**
* Returns the week-year of a calendar, which a {@code WEEK_OF_YEAR} value is relative to.
*
* @param calendar The calendar to read the week-year from.
* @return the week-year, or the calendar year if week dates are not supported.
*/
private int weekYear(final Calendar calendar) {
return calendar.isWeekDateSupported() ? calendar.getWeekYear() : calendar.get(Calendar.YEAR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ void testCompare() {
assertEquals("Invalid field: -1", e.getMessage(), "check message");
}

/**
* Test compareWeeks() at the week-year boundary, where WEEK_OF_YEAR is relative to a
* week-year that differs from the calendar year.
*/
@Test
@DefaultLocale(country = "US", language = "en")
void testCompareWeeksAcrossYearBoundary() {
final int noon = 120000;
// 31 Dec 2018 (Monday) is in week 1 of week-year 2019 (US: first day Sunday, minimal days 1)
final Calendar dec31y2018 = createCalendar(TimeZones.GMT, 20181231, noon);
final Calendar jan01y2018 = createCalendar(TimeZones.GMT, 20180101, noon);
final Calendar jan01y2019 = createCalendar(TimeZones.GMT, 20190101, noon);

// both are calendar year 2018 and week-of-year 1, but lie ~52 weeks apart
assertEquals(1, calValidator.compareWeeks(dec31y2018, jan01y2018), "Dec 31 2018 is weeks after Jan 1 2018");
assertEquals(-1, calValidator.compareWeeks(jan01y2018, dec31y2018), "Jan 1 2018 is weeks before Dec 31 2018");

// 31 Dec 2018 and 1 Jan 2019 share week 1 of week-year 2019, so they are the same week
assertEquals(0, calValidator.compareWeeks(dec31y2018, jan01y2019), "Dec 31 2018 is the same week as Jan 1 2019");
}

/**
* Test Date/Time style Validator (there isn't an implementation for this)
*/
Expand Down
Loading