-1

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

4
  • 2
    try to use this format "01.03.2021 00:01"; Commented Aug 20, 2021 at 10:54
  • 3
    or this pattern: "dd.MM.yyyy H:mm". By the way, index 11 is the 0, use only one if you are parsing values with single-digit hours of day. Commented Aug 20, 2021 at 10:56
  • 2
    Indexes in Java are 0-based, so index 11 is 0, not the space character. Commented Aug 20, 2021 at 10:58
  • 2
    0:01 doesn't match HH:mm Commented Aug 20, 2021 at 10:58

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

use the pattern suggested by deHaar in the question "dd.MM.yyyy H:mm"
with HH you are explicitly requesting for the leading 0. use just one H if you don't find practical to have a leading 0.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.