13

I'm trying to convert a string into a LocalDateTime object.

@Test
public void testDateFormat() {
   String date = "20171205014657111";
   DateTimeFormatter formatter = 
       DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
   LocalDateTime dt = LocalDateTime.parse(date, formatter);
}

I would expect this test to pass.

I get the following error:

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

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142
user4184113
  • 884
  • 2
  • 11
  • 26

1 Answers1

17

Looks like I may have run across this bug: https://bugs.openjdk.java.net/browse/JDK-8031085 as it corresponds to the JVM version I'm using. The workaround in the comments fixes the issue for me:

@Test
public void testDateFormat() {
    String date = "20171205014657111";
    DateTimeFormatter dtf = new DateTimeFormatterBuilder()
       .appendPattern("yyyyMMddHHmmss")
       .appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();
    LocalDateTime dt = LocalDateTime.parse(date, dtf);
}
OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
user4184113
  • 884
  • 2
  • 11
  • 26