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

US DateTimeFormatter uses Narrow No-Break Space

XMLWordPrintable

    • b21
    • 20
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Occurs in JDK 20, 21 and 22, but not in older ones like JDK 18 and 19.

      A DESCRIPTION OF THE PROBLEM :
      The US DateTimeFormatter uses a "Narrow No-Break Space" since JDK 20. This special character is used instead of a normal space before "AM" and "PM". Especially for parsing a users input that is an serious problem, because users can not type this character on a normal keyboard. They also could not see the difference, so an error message with an example of an correct formatted DateTime could not help.

      REGRESSION : Last worked in version 19

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Format a LocalDateTime to a String, with an older JDK Version (< 20) and parse it back in a newer one (>= 19).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Parsing works fine and I get the same value.
      ACTUAL -
      Parsing fails with an exception.

      ---------- BEGIN SOURCE ----------
      import java.time.LocalDateTime;
      import java.time.format.DateTimeFormatter;
      import java.time.format.FormatStyle;
      import java.util.Locale;

      public class Main {
          public static void main(String[] args) {
              Locale.setDefault(Locale.US);
              LocalDateTime exampleValue = LocalDateTime.of(2016, 8, 23, 10, 25, 30);
              DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
              String formatted = exampleValue.format(formatter);
              String expected = "Aug 23, 2016, 10:25:30 AM";

              // Print both strings directly among each other. You will never see a different.
              System.out.println(formatted + " <-- formatted with DateTimeFormatter");
              System.out.println(expected + " <-- Is this expected the same?");
              /*
               * Answer the question with an equals() call. The result is not as expected,
               * since OpenJDK 20 because the space before "AM" is a "Narrow No-Break Space",
               * which is a different character then a normal space.
               */
              System.out.println(expected.equals(formatted));
              // Now try parsing string back to LocalDateTime.
              System.out.println(formatted + " (formatted) back to LocalDateTime: ");
              System.out.println(LocalDateTime.parse(formatted, formatter));
              System.out.println(expected + " (expected) back to LocalDateTime: ");
              System.out.println(LocalDateTime.parse(expected, formatter));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Because such formatting is often part of an GUI library (e.g. Jakarta Faces), there is no workaround for developers of an end user application. They would have to place an workaround in the GUI libraries code.

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: