I have a list of timed intervals given as strings in a CSV file.
For example, one has the value of "7:30"
(7 minutes and 30 seconds).
Spark offers the DateType
or the TimestampType
, but it's not these ones that are interesting me, there.
At least, I have to treat this "7:30"
value as a time (without date), and the best would be that it would be considered like a duration. But then, I don't know what is the best Java or Spark class to carry it.
But currently, my problem is that my code understands the string as a time in the day :
LocalTime.parse("7:30", DateTimeFormatter.ofPattern("m:ss"))
and fails with a DateTimeParseException
whose message "Unable to obtain LocalTime from TemporalAccessor" is hinting that the parser cannot tell if it is in front of an AM or PM hour.
java.time.format.DateTimeParseException: Text '7:30' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: {MilliOfSecond=0, SecondOfMinute=30, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=7},ISO of type java.time.format.Parsed
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.LocalTime.parse(LocalTime.java:463)
... 96 elided
It would be perfect if it didn't bother about it... But well, it does. How can I circumvent the problem and adapt my code so that, for example, I can tell the parser that all the given times are AM?
But maybe I'm wrong. I should parse with a DateTimeFormatter
, yes, but maybe into another java.time.*
object than one of class LocalTime
that is made for "hour of the day" and not for durations.
If it's the case, which one should I use instead?
And how can I create it from text parsing?