k88
k88

Reputation: 1934

Java LocalTime Parse Exception

I had a piece of code that worked just fine and now somehow doesn't work.

I am reading in a csv file and get an error when reading in a time field of the format 4:38.

My code that throws the exception is:

LocalTime.parse("4:38", DateTimeFormatter.ofPattern("HH:mm"))

I also tried "H:mm" or "H:m" for the pattern but it throws the same exception: Text '4:38' could not be parsed at index 0. Any idea to why it throws an exception at the hour number?

I am using Java 8.

Upvotes: 6

Views: 2230

Answers (2)

Prabhu
Prabhu

Reputation: 94

"H:mm" is working fine for me.enter image description hereenter image description here

Upvotes: 0

debus
debus

Reputation: 640

The pattern needs one single "H" and one single "m".

LocalTime.parse("4:38", DateTimeFormatter.ofPattern("H:m"));

It works fine for 4:38 and 14:38.

Official Doc: See "Patterns for Formatting and Parsing"

Upvotes: 10

Related Questions