开发者

JodaTime: Print Period with Days as Hours?

开发者 https://www.devze.com 2023-03-07 22:49 出处:网络
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.no

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

精彩评论

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