Convert JodaTime duration to string

僤鯓⒐⒋嵵緔 提交于 2019-11-30 00:57:55

问题


I have movie with duration 127 seconds. I wanna display it as 02:07. What is the best way to implement this?


回答1:


Duration yourDuration = //...
Period period = yourDuration.toPeriod();
PeriodFormatter minutesAndSeconds = new PeriodFormatterBuilder()
     .printZeroAlways()
     .appendMinutes()
     .appendSeparator(":")
     .appendSeconds()
     .toFormatter();
String result = minutesAndSeconds.print(period);



回答2:


I wanted this for myself and I did not find llyas answer to be accurate. I want to have a counter and when I had 0 hours and 1 minute I got 0:1 with his answer- but this is fixed easily with one line of code!

Period p = time.toPeriod();
PeriodFormatter hm = new PeriodFormatterBuilder()
    .printZeroAlways()
    .minimumPrintedDigits(2) // gives the '01'
    .appendHours()
    .appendSeparator(":")
    .appendMinutes()
    .toFormatter();
String result = hm.print(p);

This will give you 02:07 !



来源:https://stackoverflow.com/questions/10829870/convert-jodatime-duration-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!