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

DateTimeFormatter requires a PERIOD character to parse a fraction-of-second.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • tbd
    • 8
    • core-libs

      FULL PRODUCT VERSION :
      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b132)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X 10.8.5 (Mountain Lion).

      A DESCRIPTION OF THE PROBLEM :
      In the new java.time package, the documentation for the DateTimeFormatter class describes fraction-of-second as just another part of a date-time like any other part. Yet it treats fraction-of-second as special when parsing a string: A PERIOD character is required. Without a "." in the input string being parsed, and in the format pattern, an exception is thrown. The documentation says nothing about a period being required.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      This code works:

      String input = "20111203123456";
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss");
      LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );

      Rendering:

      2011-12-03T12:34:56

      But when adding the uppercase "S" for fraction of second, an exception is thrown. This code:

      String input = "2011120312345655";
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSS");
      LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );

      throws this exception:

      java.time.format.DateTimeParseException: Text '2011120312345655' could not be parsed at index 0

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected this string:

      2011-12-03T12:34:56.55
      ACTUAL -
      Throws this exception:

      java.time.format.DateTimeParseException: Text '2011120312345655' could not be parsed at index 0

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.time.format.DateTimeParseException: Text '2011120312345655' could not be parsed at index 0

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      String input = "2011120312345655";
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmssSS");
      LocalDateTime localDateTime = LocalDateTime.parse( input, formatter );
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None. A string without punctuation for the fraction-of-second cannot be parsed.

            rriggs Roger Riggs
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: