How to convert string HH:MM to Joda Duration?

前端 未结 1 446
时光说笑
时光说笑 2021-01-11 14:57

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

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-11 15:27

    I think a better way would be to use PeriodFormatterBuilder like this:

     PeriodFormatter hoursMinutes= new PeriodFormatterBuilder()
         .appendHours()
         .appendSeparator(":")
         .appendMinutes()
         .toFormatter();
    

    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.

    0 讨论(0)
提交回复
热议问题