1

I am trying to create a LocalDateTime object from a string that is coming from another system (i.e. I don't have the ability to control it). The LocalDateTime.parse(String, DateTimeFormatter) method of Java 8 is not correctly able to parse my string. An example of the date string would be 20190813074127111 and the pattern I am using is yyyyMMddHHmmssSSS. The error is that Text '20190813074127111' could not be parsed at index 0

I have validated my letters in my DateTimeFormatter pattern are correct using the documentation for the letters. I have seen many questions and answers on Stack Overflow very similar to this but none have exactly solved my issue.

I thought that this Stack Overflow was similar to what I am experiencing, which says that the entire String could be being evaluated as a year as I am using yyyy in my pattern string. I tried modifying the pattern to both yyMMddHHmmssSSS and uuMMddHHmmssSSS; in both cases the exception changes to Text '20190813074127111' could not be parsed at index 15

I have tried trimming the SSS at the end of my pattern and removing the milliseconds from the input string and that correctly creates a LocalDateTime with the correct date and time, but I don't have the precision provided with the milliseconds, and this use case should be as precise as possible.

I had to remove some details since I am doing this for a company, but this is identical to the use case I am trying to accomplish

public class MyClass {
    private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

    public LocalDateTime parseStringToDate(String input) {
        return LocalDateTime.parse(input, DATETIME_FORMATTER);
    }
}
cjwillis48
  • 545
  • 2
  • 6
  • 15

1 Answers1

2

You need to use the DateTimeFormatBuilder:

DateTimeFormatter formatter =
    new DateTimeFormatterBuilder()
        .appendPattern("yyyyMMddHHmmss")
        .appendValue(ChronoField.MILLI_OF_SECOND, 3)
        .toFormatter();

Related post: Is java.time failing to parse fraction-of-second?

George
  • 2,820
  • 4
  • 29
  • 56
  • This is the correct way to do it. It is to note that `LocalDateTime` does nano of seconds instead of milliseconds like the old `Date` did – locus2k Oct 31 '19 at 18:14
  • 2
    Correct, but this is what we have the duplicate mechanism for. Please flag as a duplicate instead of writing a new duplicate answer in this and similar cases. Readers are helped better when led to the place where we have all the information about this question gathered. – Ole V.V. Oct 31 '19 at 18:15
  • Perfect answer. Thanks for linking the related post that's one that I did not see in the several stack overflow that I saw – cjwillis48 Oct 31 '19 at 18:19
  • Tips: (a) Such output could be made to comply with the [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) standard, specifically the "basic" variation that minimizes delimiters, by putting a `T` in between the date portion and the time-of-day portion. (b) If this value represents a moment in UTC, it should have a `Z` appended, meaning UTC, pronounced “Zulu”. – Basil Bourque Oct 31 '19 at 22:16