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.
  • 81,772
  • 15
  • 137
  • 161
user4184113
  • 976
  • 2
  • 11
  • 29

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
  • 179,855
  • 19
  • 132
  • 245
user4184113
  • 976
  • 2
  • 11
  • 29