问题
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