如何将字符串HH:MM转换为Joda持续时间? [英] How to convert string HH:MM to Joda Duration?
问题描述
我在表单中有一个字段来说明事件的持续时间。
比如说,比赛将持续15分钟。因此该字段将具有以下值:00:15
如果是持续1小时:01:00等等。
I have a field in a form to state the time duration of an event. Say, the event is to last 15 mins. So the field will have the following value: 00:15 If it is to last 1 hour: 01:00, etc.
如何创建 Joda-Time 持续时间对象,字符串为HH:MM?
How can I create a Joda-Time Duration object with the string "HH:MM"?
看看Joda-Time主页,它提到可以分别使用ConverterManager和DurationConverter从指定对象创建一个Duration对象。
Looking at Joda-Time home page, it mentions that it is possible to create a Duration object from specified object using ConverterManager and DurationConverter respectively.
我的问题是,我怎么能实现上面的接口,以便我可以通过传递4:30参数来创建一个Duration对象?
My question is, how can I implement the above interfaces so that I can create a Duration object by passing the a "4:30" parameter?
提前谢谢,
Lucas
推荐答案
我认为更好的方法是使用 PeriodFormatterBuilder
,如下所示:
I think a better way would be to use PeriodFormatterBuilder
like this:
PeriodFormatter hoursMinutes= new PeriodFormatterBuilder()
.appendHours()
.appendSeparator(":")
.appendMinutes()
.toFormatter();
然后你可以将你的字符串解析为 Period
对象并在那些上调用 toStandardDuration()
- 或继续使用 Period
,因为这似乎与您的用例相匹配更好。
Then you can parse your strings into Period
objects and call toStandardDuration()
on those - or keep working with the Period
, since that seems to match your use case better.
这篇关于如何将字符串HH:MM转换为Joda持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!