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