JDK-8138676 : Failed to format a datetime using a specific format
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.time
  • Affected Version: 8u51,9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: x86
  • Submitted: 2015-09-21
  • Updated: 2016-02-03
  • Resolved: 2015-10-09
Related Reports
Duplicate :  
Duplicate :  
Description
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


Comments
This is a duplicate of JDK-8031085
03-02-2016

This does not looks like a bug , as per the ISO-8601 calendar system its working with the format yyyyMMddHHmmss.SSS. However, it may be considered as an enhancement request to support the format with no '.' before the milliseconds. Moving to dev-team for evaluation.
01-10-2015