Is there any way to create a PeriodFormatter in JodaTime which is able to print a Period of lets say 2 days, 4 hours and 4 minutes as 52 hours, 4 minutes?
I believe you can use Period.normalizedStandard
specifying a PeriodType
containing just hours and minutes. Sample:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) {
// 2 days, 4 hours, 4 minutes
Period period = new Period(0, 0, 0, 2, 4, 4, 0, 0);
// Actually we're fine with seconds etc as well
Period hoursAndMinutes = period.normalizedStandard(PeriodType.time());
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendHours()
.appendSuffix(" hour", " hours")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.toFormatter();
System.out.println(formatter.print(hoursAndMinutes));
}
}
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