我有jodatime api的问题.我不明白为什么这个测试不起作用.我需要在X毫秒内解决我有多少天,几小时,几分钟和几秒钟.但天价值没有解决….任何想法?
@Test
public void testTime() {
long secs = 3866 * 24 * 35;
long msecs = secs * 1000;
//Period period = duration.toPeriod();
PeriodType periodType = PeriodType.forFields(
new DurationFieldType[]{
DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds(),
});
Duration duration = Duration.standardSeconds(secs);
Period period = duration.toPeriod(periodType, GregorianChronology.getInstance());
System.out.println("days:" + period.getDays());
System.out.println("hours:" + period.getHours());
System.out.println("mins:" + period.getMinutes());
System.out.println("seconds:" + period.getSeconds());
PeriodFormatter periodFormatter = new PeriodFormatterBuilder()
.printZeroAlways()
.minimumPrintedDigits(2)
.appendDays()
.appendSeparator(":")
.appendHours()
.appendSeparator(":")
.appendMinutes()
.appendSeparator(":")
.appendSecondsWithOptionalMillis()
.toFormatter();
StringBuffer stringBuffer = new StringBuffer();
periodFormatter.printTo(stringBuffer, period);
System.out.println(">>>" + stringBuffer);
}
输出是
天:0
小时:902
分钟:4
秒:0
00:902:04:00
最佳答案 您需要使用以下方法规范化期间:
Period normalizedPeriod = period.normalizeStandard();
要么
Period normalizedPeriod = period.normalizeStandardPeriodType();
然后,您可以使用normalizedPeriod并查看您要查找的结果.
作为快速测试,我修改了你的junit测试用例并添加了一行:
period = period.normalizedStandard();
在您从持续时间创建期间之后.