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

Broken Calendar#getMinimalDaysInFirstWeek with java.locale.providers=HOST

XMLWordPrintable

    • b02
    • generic
    • windows
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10;
      Tested with Java 11 and Java 14

      A DESCRIPTION OF THE PROBLEM :
      When setting the property "java.locale.providers" to "HOST" the value of Calendar#getMinimalDaysInFirstWeek always returns 1, even for locales where this is not the case. For example de_De and en_GB the value should be 4.
      This leads to the error that week numbers at the end of a year maybe computed to a wrong value.
      E.g. in 2020 the week 2020-12-28 until 2021-01-03, should be week #53 in Germany (also in UK), but it is shown as week #1 when running int weekNumber = date.get(WeekFields.of(Locale.getDefault(Locale.Category.FORMAT)).weekOfWeekBasedYear());
      Where date is one of the days in the week mentioned above.

      I assume that the error is a missing/wrong implementation in HostLocaleProviderAdapterImpl, in the Method getCalendarDataProvider which implements the following snippet:
      @Override
      public int getMinimalDaysInFirstWeek(Locale locale) {
          return 0;
      }

      That returns 0 for minimalDaysInFirstWeek for all locales (later this is set to the default of 1). The macOS implementation of this class seems to have a valid implementation that calculates the value based on the actual locale.
       

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the attached code snippet, while your systems format locale is Germany or United Kingdom



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Days in first week: 4
      Week # 53
      ACTUAL -
      Days in first week: 1
      Week # 1

      ---------- BEGIN SOURCE ----------
      import java.time.LocalDate;
      import java.time.temporal.WeekFields;
      import java.util.Calendar;
      import java.util.Locale;

      public class WeekNumberTest {

          public static void main(String[] args) {

              System.setProperty("java.locale.providers", "HOST,COMPAT,SPI");

              Calendar instance = Calendar.getInstance(Locale.getDefault(Locale.Category.FORMAT));
              System.out.println("Days in first week: " + instance.getMinimalDaysInFirstWeek());

              LocalDate date = LocalDate.of(2020,12,31);
              int weekNumber = date.get(WeekFields.of(Locale.getDefault(Locale.Category.FORMAT)).weekOfWeekBasedYear());
              System.out.println("Week # " + weekNumber);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Start without HOST e.g. System.setProperty("java.locale.providers", COMPAT,SPI");

      But this prevents getting details from the format, that differ to the standard for that locale.

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: