Unable to parse string into Java 8 LocalDateTime

Viewed 904

Running this gives me the following error, what am I missing ?

public static void main(String[] args) {

    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137",_timestampFomatGMT);
    System.out.println(localDateTime);     
}

Gives me the following exception. What am I missing ?

Exception in thread "main" java.time.format.DateTimeParseException: Text '20200331094118137' could not be parsed at index 0
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalDateTime.parse(LocalDateTime.java:492)
        at cotown.lib.common.util.JavaTimeUtil.main(JavaTimeUtil.java:90)
2 Answers

Java does not accept a plain Date value as DateTime.

Try using LocalDate,

public static void main(String[] args) {
DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate localDateTime = LocalDate.parse("20200331",_timestampFomatGMT);
System.out.println(localDateTime);
}

or if you really have to use LocalDateTime, then try

LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay();

EDIT

there was a bug for this already raised https://bugs.openjdk.java.net/browse/JDK-8031085. It is fixed in JDK 9.

You cannot parse a date-only String into a LocalDateTime without passing a time value in addition.

What you can do is use a date-only class like LocalDate similar to your code:

public static void main(String[] args) {
    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
    System.out.println(localDate);
}

That would simply output

2020-03-31

If you really need to have a LocalDateTime and the String to be parsed cannot be adjusted to include time, then pass an additional time of 0 hours and minutes with an intermediate operation like this (but keep in mind that the output will include the time information as well):

public static void main(String[] args) {
    DateTimeFormatter _timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMdd");
    LocalDate localDate = LocalDate.parse("20200331",_timestampFomatGMT);
    LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0));
    System.out.println(localDateTime);
}

Or use LocalDateTime time = LocalDate.parse("20200331", _timestampFomatGMT).atStartOfDay(); as suggested by @Shubham.

Output would be:

2020-03-31T00:00

For outputting the date only, change the last line of the last example to

System.out.println(localDateTime.format(DateTimeFormatter.ISO_DATE));

which will only output the date part of the LocalDateTime in an ISO representation:

2020-03-31

EDIT

Targeting your latest question update, this might help:

public static void main(String[] args) {
    DateTimeFormatter timestampFomatGMT = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
    LocalDateTime localDateTime = LocalDateTime.parse("20200331094118137", timestampFomatGMT);
    System.out.println(localDateTime);
}

Output:

2020-03-31T09:41:18.137
Related