I have movie with duration 127 seconds. I wanna display it as 02:07
. What is the best way to implement this?
Duration yourDuration = //...
Period period = yourDuration.toPeriod();
PeriodFormatter minutesAndSeconds = new PeriodFormatterBuilder()
.printZeroAlways()
.appendMinutes()
.appendSeparator(":")
.appendSeconds()
.toFormatter();
String result = minutesAndSeconds.print(period);
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
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With