2
我刚刚在Joda时间框架中测试了PeriodFormatterBuilder。当我将周数输出添加到构建器时,计算的时间是正确的。但是,如果周不追加,究竟是我想要的,建设者只是下降7天:Joda时间PeriodFormatterBuilder
public class JodaTest {
public static void main(String[] args) {
// builder 1 (weeks inc.)
PeriodFormatterBuilder b1 = new PeriodFormatterBuilder();
b1.appendYears().appendSuffix(" year", " years");
b1.appendSeparator(" ");
b1.appendMonths().appendSuffix(" month", " months");
b1.appendSeparator(" ");
// appends weeks ...
b1.appendWeeks().appendSuffix(" week", " weeks");
b1.appendSeparator(" ");
b1.appendDays().appendSuffix(" day", " days");
b1.appendSeparator(" ");
b1.printZeroIfSupported().minimumPrintedDigits(2);
b1.appendHours().appendSuffix(" hour", " hours");
b1.appendSeparator(" ");
b1.appendMinutes().appendSuffix(" minutes");
b1.appendSeparator(" ");
b1.appendSeconds().appendSuffix(" seconds");
PeriodFormatter f1 = b1.toFormatter();
// builder 2 (weeks not inc.)
PeriodFormatterBuilder b2 = new PeriodFormatterBuilder();
b2.appendYears().appendSuffix(" year", " years");
b2.appendSeparator(" ");
b2.appendMonths().appendSuffix(" month", " months");
b2.appendSeparator(" ");
// does not append weeks ...
b2.appendDays().appendSuffix(" day", " days");
b2.appendSeparator(" ");
b2.printZeroIfSupported().minimumPrintedDigits(2);
b2.appendHours().appendSuffix(" hour", " hours");
b2.appendSeparator(" ");
b2.appendMinutes().appendSuffix(" minutes");
b2.appendSeparator(" ");
b2.appendSeconds().appendSuffix(" seconds");
PeriodFormatter f2 = b2.toFormatter();
Period period = new Period(new Date().getTime(), new DateTime(2014, 12, 25, 0, 0).getMillis());
System.out.println(f1.print(period));
System.out.println(f2.print(period)); // 7 days missing?
}
}
打印出:
1 month 1 week 2 days 09 hours 56 minutes 21 seconds
1 month 2 days 09 hours 56 minutes 21 seconds
在第二行的日子值应为“9天”。如何让建造者总结正确的日期值?
虽然很有希望,但会向我抛出异常:IllegalArgumentException:期间不支持“周”字段。 – Suma 2017-06-08 11:37:06
@Suma权利。建议更正。 – RealSkeptic 2017-06-08 19:57:16