开发者

JodaTime: Print Period with Days as Hours?

Is there any way to create a PeriodFormatter in JodaTime which is able to print a Period of lets s开发者_运维问答ay 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));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜