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

Failed to format a datetime using a specific format

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8u51, 9
    • core-libs
    • x86
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_51"
      Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
      Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Can parse a LocalDateTime using the following pattern: "yyyyMMddHHmmssSSS"
      However this pattern works "yyyyMMddHHmmss.SSS"

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Try parsing a string in the "yyyyMMddHHmmssSSS" format using:
      LocalDateTime.parse("20150910121314987", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"))

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no exception thrown and date successfully parsed
      ACTUAL -
      DateTimeParseException is thrown
      Exception in thread "main" java.time.format.DateTimeParseException: Text '20150921165941914' could not be parsed at index 0
      at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
      at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
      at java.time.LocalDateTime.parse(LocalDateTime.java:492)
      at TestLocalDataTimeFormat.main(TestLocalDataTimeFormat.java:11)

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.time.format.DateTimeParseException: Text '20150921165941914' could not be parsed at index 0
      at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1947)
      at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1849)
      at java.time.LocalDateTime.parse(LocalDateTime.java:492)

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class TestLocalDataTimeFormat {

        public static void main(String[] args) {
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
            String date = formatter.format(now);
            System.out.println("date to parse: "+now);
            LocalDateTime.parse(date, formatter); // throws an exception
        }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use the following format (mind the '.'): "yyyyMMddHHmmss.SSS"

      LocalDateTime.parse("20150910121314987", DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS"))

      or alternatively use jodatime library

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: