从“7:30”开始(7 分 30 秒)字符串表示持续时间或时间量
我有一个在 CSV 文件中以字符串形式给出的定时间隔列表。
例如,值为 "7:30"
(7 分 30 秒)。
Spark 提供 DateType
或 TimestampType
,但我感兴趣的不是这些。
至少,我必须将这个“7:30”值视为时间(不带日期),最好是将其视为持续时间。但是,我不知道什么是最好的 Java 或 Spark 类来承载它。
但目前,我的问题是我的代码将字符串理解为一天中的时间:
LocalTime.parse("7:30", DateTimeFormatter.ofPattern("m:ss"))
并因 DateTimeParseException
失败,其消息“无法从 TemporalAccessor 获取 LocalTime” ”暗示解析器无法判断它是在上午还是下午之前。
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
如果它不介意的话就完美了……但是好吧,它确实如此。我怎样才能规避这个问题并调整我的代码,例如,我可以告诉解析器所有给定的时间都是上午?
但也许我错了。我应该使用 DateTimeFormatter
进行解析,是的,但可能会解析为另一个 java.time.*
对象,而不是为“创建的 LocalTime
类”之一一天中的某个小时”而不是持续时间。
如果是这样,我应该使用哪一个?
我如何从文本解析中创建它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论