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