解析像“1h 30min”这样的时间字符串
任何人都知道有一个 Java 库可以将“30min”或“2h 15min”或“2d 15h 30min”等时间字符串解析为毫秒(或某种 Duration 对象)。 Joda-Time 可以做这样的事情吗?
(我有一个丑陋的长方法来维护它进行这样的解析,并且想摆脱它/用做得更好的东西替换它。)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能需要根据自己的格式稍微调整一下,但请尝试以下操作:
请注意,如果需要,有一个
appendSuffix
接受variants
参数使其更加灵活。更新:Joda Time 此后添加了
Period.toStandardDuration()
,然后您可以使用getStandardSeconds()
< /a> 获取long
形式的经过时间(以秒为单位)。如果您使用的是没有这些方法的旧版本,您仍然可以通过假设一天中的标准 24/小时、60 分钟/小时等自行计算时间戳。(在这种情况下,请利用
中的常量DateTimeConstants
类以避免需要幻数。)You'll probably have to tweak this a bit for your own format, but try something along these lines:
note that there is a
appendSuffix
that takes avariants
parameter if you need to make it more flexible.Update: Joda Time has since added
Period.toStandardDuration()
, and from there you can usegetStandardSeconds()
to get the elapsed time in seconds as along
.If you're using an older version without these methods you can still calculate a timestamp yourself by assuming the standard 24/hr in a day, 60min/hr, etc. (In this case, take advantage of the constants in the
DateTimeConstants
class to avoid the need for magic numbers.)持续时间解析现已包含在 Java 8 中。使用标准 ISO 8601 格式和
Duration.parse
。您可以转换此持续时间到总长度(以毫秒为单位)。请注意,
Duration
的分辨率为纳秒,因此您可能会丢失纳秒的数据 到 毫秒。该格式与您所描述的格式略有不同,但可以轻松地从一种格式转换为另一种格式。
Duration parsing is now included in Java 8. Use standard ISO 8601 format with
Duration.parse
.You can convert this duration to the total length in milliseconds. Beware that
Duration
has a resolution of nanoseconds, so you may have data loss going from nanoseconds to milliseconds.The format is slightly different than what you describe but could be easily translated from one to another.
我想让日期、小时和分钟可选,这似乎可以做到这一点。请注意,appendSuffix() 调用在字符后没有空格。
使用乔达2.3。
上面的代码通过了这些测试。
I wanted to make the day, hour and minute optional and this seems to work to do that. Note that the appendSuffix() calls do not have a space after the character.
Using Joda 2.3.
The above code passes these tests.
仅供参考,只是写了一个多小时,仅使用
java.time.*
,非常容易理解并根据任何需要进行自定义;此版本适用于以下字符串:
3d12h
、2y
、9m10d
等。FYI, Just wrote this for hour+ periods, only uses
java.time.*
, pretty simple to understand and customize for any need;This version works with strings like;
3d12h
,2y
,9m10d
, etc.java.time
下面引用的是 主页的通知乔达时间:
使用现代日期时间 API
java.time
的解决方案:您可以将输入字符串转换为 ISO_8601#Duration 格式,然后将其解析为
java.time.Duration
作为 JSR-310 实现。演示:
输出:
在线演示
正则表达式演示
了解有关现代日期时间 API*< /sup> 来自跟踪:日期时间。
* 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供的 Java 8+ API。请注意,Android 8.0 Oreo 已提供 对
java.time 的支持
。java.time
Quoted below is a notice from the home page of Joda-Time:
Solution using
java.time
, the modern Date-Time API:You can convert the input string into ISO_8601#Duration format and then parse the same into
java.time.Duration
which was introduced with Java-8 as part of JSR-310 implementation.Demo:
Output:
Online Demo
Regex Demo
Learn more about the modern Date-Time API* from Trail: Date Time.
* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for
java.time
.不,Joda 默认只获取持续时间、即时间隔和对象。对于后者,它接受日期或 SOAP ISO 格式等内容。您可以在此处为 Duration 类添加您自己的转换器,诚然,这会隐藏所有丑陋的代码。
No, Joda defaults to taking only Durations, Instant intervals, and objects. For the latter it accepts things like Dates or SOAP ISO format. You can add you own converter here for the Duration class, and admittedly that would hide all your ugly code.
不完全是 Java - 而是 Kotlin,并且不使用 Joda,而是使用 JDK 的 java.time:
摘自这个要点:
https://gist.github.com/OndraZizka/5fd56479ed2f6175703eb8a2e1bb1088
Not exactly Java - rather Kotlin, and not using Joda but JDK's
java.time
:Taken from this Gist:
https://gist.github.com/OndraZizka/5fd56479ed2f6175703eb8a2e1bb1088
我意识到这个帖子已经有几年了,但它不断出现在谷歌搜索的顶部,所以我想我们应该分享这个。
这是用 Kotlin 编写的完整示例
I realise this thread is a few years old but it keeps popping up at the top of a Google search so I thought we'd share this.
Here's a complete example written in Kotlin