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

SimpleDateFormat.format returns wrong form of month (standalone instead of context-sensitive) for "MMMM" template

XMLWordPrintable

      The spec for j.t.SimpeDateFormat says regarding month format the following:
      "Letter M produces context-sensitive month names"

      So, accepting of the following template to SimpleDateFormat instance should produces contex-sensitive month names:
      "MMMM"

      We can compare the result with the result of SimpleDateFormat.getDateFormatSymbols().getMonths() result, because the spec of the DateFormatSymbols.getMonths says:
      "If the language requires different forms for formatting and stand-alone usages, this method returns month names in the formatting form."

      However, the following minimized test shows that for number of locales this comparison will fail:
      -----------------------------------------------------------------------------------------------------------------------------

      public class Test {

          public static void main(String[] args) {
              minimizedTest();
          }

          public static void minimizedTest() {
              for (Locale l : Locale.getAvailableLocales()) {
                  checkSimpleDateFormatForLocale(l);
              }
          }

          private static final Date date = new Date(1234567890);
          // Letter M produces context-sensitive month names.
          private static final String monthTemplate = "MMMM";

          private static void checkSimpleDateFormatForLocale(Locale locale) {
              SimpleDateFormat sdf = new SimpleDateFormat(monthTemplate, locale);
              // If the language requires different forms for formatting and stand-alone usages, this method returns month names in the formatting form.
              String dateFormatSymbol = sdf.getDateFormatSymbols().getMonths()[0];
              String result = sdf.format(date, new StringBuffer(), new FieldPosition(0)).toString();
              if (!dateFormatSymbol.equals(result)) {
                  System.out.println("---------------------------------");
                  System.out.println("Locale: " + locale);
                  System.out.println("SimpleDateFormat.getDateFormatSymbols month: " + dateFormatSymbol);
                  System.out.println("SimpleDateFormat.format month: " + result);
              }
          }
      }
      -----------------------------------------------------------------------------------------------------------------------------

      If the following locales will be available they will be in output:
      hr_HR, it, uk, sk, fi_FI, cs, el, uk_UA, cs_CZ, pl_PL, ca_ES, hr, lt, ca, ru_RU, fi, ru, el_GR, it_CH, sk_SK, lt_LT, it_IT, pl

      Tested with jdk8 and jdk8u20.

            okutsu Masayoshi Okutsu
            evotchen Elena Votchennikova (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: