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

DateTimeFormatter cannot correctly parse AM/PM

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      macOS 10.15.5

      $ java -version
      java version "11.0.8" 2020-07-14 LTS
      Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      This happens with 11.0.8 only on macOS. 16-ea is fine as well as 11.0.8 on Linux.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac DateFormatterBug.java
      java DateFormatterBug

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      2019-05-02T01:02:03
      ACTUAL -
      Exception in thread "main" java.time.format.DateTimeParseException: Text '05/02/2019 01:02:03 AM' could not be parsed at index 20
      at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
      at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
      at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
      at DateFormatterBug.main(DateFormatterBug.java:8)

      ---------- BEGIN SOURCE ----------
      import java.time.LocalDateTime;
      import java.time.format.DateTimeFormatter;

      public class DateFormatterBug {
          public static void main(String[] args) {
              String date = "05/02/2019 01:02:03 AM";
              DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a");
              LocalDateTime result = LocalDateTime.parse(date, formatter);
              System.out.println(result);
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Use JDK 16-ea:

      $ java -version
      openjdk version "16-ea" 2021-03-16
      OpenJDK Runtime Environment (build 16-ea+7-249)
      OpenJDK 64-Bit Server VM (build 16-ea+7-249, mixed mode, sharing)
      $ java DateFormatterBug
      2019-05-02T01:02:03

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              tongwan Andrew Wang
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: