6

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.

5
  • use the value of "04:38"
    – DimaSan
    Aug 19, 2016 at 8:39
  • That means I got to edit the entire csv file. Isn't there another way?
    – k88
    Aug 19, 2016 at 8:42
  • I would try to correct the csv builder if possible. What is that format of time ... H:MM. Where is it comming from ?
    – AxelH
    Aug 19, 2016 at 8:47
  • "H:mm" or "H:m" should work correctly.
    – Prabhu
    Aug 19, 2016 at 8:59

2 Answers 2

10

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"

1
  • That is what I also had thought. Turns out, I forgot that I am parsings two identical values, and the exception was being thrown by the duplicate value. Thanks!
    – k88
    Aug 19, 2016 at 8:51
0

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.