Joda time PeriodFormatterBuilder,我可以省略几周而只使用几天吗?
Joda time PeriodFormatterBuilder, can I omit weeks and use only days?
我看过this,但是否可以只使用月和日,而不是周?
例如,我想要“1 个月 16 天”,而不是“1 个月 2 周 2 天”。
您可以使用类似于 answer you linked 的解决方案,但您还需要使用 org.joda.time.PeriodType
来标准化周期:
// 1 month, 2 weeks and 2 days
Period p = new Period(0, 1, 2, 2, 0, 0, 0, 0);
PeriodFormatter fmt = new PeriodFormatterBuilder()
// months
.appendMonths().appendSuffix(" month", " months").appendSeparator(" and ")
// days
.appendDays().appendSuffix(" day", " days")
.toFormatter();
System.out.println(fmt.print(p.normalizedStandard(PeriodType.yearMonthDayTime())));
这将打印:
1 month and 16 days
我看过this,但是否可以只使用月和日,而不是周?
例如,我想要“1 个月 16 天”,而不是“1 个月 2 周 2 天”。
您可以使用类似于 answer you linked 的解决方案,但您还需要使用 org.joda.time.PeriodType
来标准化周期:
// 1 month, 2 weeks and 2 days
Period p = new Period(0, 1, 2, 2, 0, 0, 0, 0);
PeriodFormatter fmt = new PeriodFormatterBuilder()
// months
.appendMonths().appendSuffix(" month", " months").appendSeparator(" and ")
// days
.appendDays().appendSuffix(" day", " days")
.toFormatter();
System.out.println(fmt.print(p.normalizedStandard(PeriodType.yearMonthDayTime())));
这将打印:
1 month and 16 days