JDK-8272117 : weekOfYear off by one week for Locale.GERMAN
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 11,16,18
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2021-08-05
  • Updated: 2021-08-07
  • Resolved: 2021-08-07
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Java 9.0.4 -- working
Java 10.0.2 -- fails
Java 16.0.2 -- fails
Eclipse build 20210612-2011
Windows 10, Version 10.0.19042 Build 19042

A DESCRIPTION OF THE PROBLEM :
With Java9, when setting weekOfYear=1 on '2010-01-01', it stays on January 1.
Starting from Java10, it is set one week earlier, to '2009-12-25'.

I could not find any related information on the Java10 release notes, so I assume its a bug.
This affects both Calendar and LocalDate calculation.

REGRESSION : Last worked in version 8

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile+run with Java9 -- works
compile+run with Java10+ -- fails


---------- BEGIN SOURCE ----------
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DateTimeUtilsTest {

	@Test
	public void testDate() {

		System.out.println("-- Date --");
		final Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.GERMAN);

		cal.setTime(new Date(110, 0, 1));
		System.out.println(cal.getTime());

		cal.set(Calendar.WEEK_OF_YEAR, 2);
		System.out.println(cal.getTime());

		Assertions.assertEquals(15, cal.get(Calendar.DAY_OF_MONTH));

	}

	@Test
	public void testLocalDate() {
		System.out.println("-- LocalDate --");

		LocalDate date = LocalDate.of(2010, Month.JANUARY, 1);
		final TemporalField weekOfYearField = WeekFields.of(Locale.GERMAN).weekOfYear();

		System.out.println(date);
		System.out.println(date.get(weekOfYearField));

		date = date.with(weekOfYearField, 2);

		System.out.println(date);
		System.out.println(date.get(weekOfYearField));

		Assertions.assertEquals(15, date.getDayOfMonth());
	}
}

---------- END SOURCE ----------

FREQUENCY : always



Comments
The result is due to the fix to JDK-8190918 and expected, where week-related information is retrieved from the country, not from the language. Since the specified locale (Locale.GERMAN) only designates the language, it will default to the country invariant week information (week starts on Sundays). To avoid this, use the locale that designates the country, such as Locale.GERMANY.
07-08-2021

The observations on Windows 10: JDK 8: Passed. JDK 11: Failed, test cases failed. JDK 16: Failed. JDK 18ea+1: Failed.
07-08-2021