Convert JodaTime duration to string

fedor.belov picture fedor.belov · May 31, 2012 · Viewed 11.5k times · Source

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

Answer

Ilya picture Ilya · Jun 1, 2012
Duration yourDuration = //...
Period period = yourDuration.toPeriod();
PeriodFormatter minutesAndSeconds = new PeriodFormatterBuilder()
     .printZeroAlways()
     .appendMinutes()
     .appendSeparator(":")
     .appendSeconds()
     .toFormatter();
String result = minutesAndSeconds.print(period);