Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8272117

weekOfYear off by one week for Locale.GERMAN

XMLWordPrintable

      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


            naoto Naoto Sato
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: