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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With