jodatime 天周期类型问题/错误
我的 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
I have a problem with jodatime api. I can't understand why this test doesn't works. I need resolve how many days, hours, mins and seconds I have in X milliseconds. But the days value aren't resolved.... any idea?
@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);
}
the output is
days:0
hours:902
mins:4
seconds:0
00:902:04:00
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用以下方法标准化周期:
或
然后您可以使用 normalizedPeriod 并查看您正在寻找的结果。
作为快速测试,我修改了您的 junit 测试用例并添加了这一行:
在您根据持续时间创建周期之后。
You need to normalize the period using:
or
Then you can use the normalizedPeriod and see the results you are looking for.
As a quick test I modified your junit test case and added the line:
right after your create the period from the duration.
您使用的年表可能认为天数并不准确。尝试使用 IOSChronology.getInstanceUTC(),而不是 GregorianChronology。
The chronology that you are using may not consider days to be precise. Instead of GregorianChronology, try using
IOSChronology.getInstanceUTC()
.