java.time.format.DateTimeParseException: Text could not be parsed at index "dd.MM.yyyy HH:mm"

7.8k views Asked by At

My code is:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");
String filterValue = "01.03.2021 0:01";
LocalDateTime parsedDate = LocalDateTime.parse(filterValue, formatter);

I get the exception java.time.format.DateTimeParseException: Text '01.03.2021 0:01' could not be parsed at index 11

1

There are 1 answers

2
Jens Kisters On

The format says "HH" but the value provided is "0". "01.03.2021 00:01" works.

I think its pretty unpractical to force a leading zero for the hour but well, i got a solution.