期间没有显示天数 [英] PeriodFormatter not showing days
问题描述
我正在使用PeriodFormatter返回一个字符串,显示事件的时间。以下代码不断创建1146小时39分钟,而不是x天y小时z分钟。任何想法?
I am using a PeriodFormatter to return a string showing the time until an event. The code below keeps creating strings like 1146 hours 39 minutes instead of x days y hours z minutes. Any ideas?
谢谢,
Nathan
Thanks, Nathan
PeriodFormatter formatter = new PeriodFormatterBuilder()
.printZeroNever()
.appendDays()
.appendSuffix( "d " )
.appendHours()
.appendSuffix( "h " )
.appendMinutes()
.appendSuffix( "m " )
.toFormatter();
return formatter.print( duration.toPeriod() );
推荐答案
这是因为您转换 持续时间 到 期间 ,方法 Duratoin :: toPeriod
这是在Joda-time文档中描述的:
This is because you convert Duration to Period with method Duratoin::toPeriod
This is described in Joda-time documentation:
public Period toPeriod()将此持续时间转换为Period实例
使用标准期类型和ISO年表。 将使用期间类型中仅精确的
字段。因此,这段时间内只能使用小时,分钟,
秒和毫秒字段。年,
月,周和日字段将不会被填充。
public Period toPeriod() Converts this duration to a Period instance using the standard period type and the ISO chronology. Only precise fields in the period type will be used. Thus, only the hour, minute, second and millisecond fields on the period will be used. The year, month, week and day fields will not be populated.
在没有开始日期(或结束日期)的情况下,时间安排无法正确计算,因为有些日子可能是24小时或 23h (由于DST)
Period insance cann't be calculated properly without start date (or end date), because some days can be 24h or 23h (due to DST)
您使用方法 持续时间:: toPeriodFrom 方法而是
例如
You shoud use method Duration::toPeriodFrom method instead
E.g.
Duration duration = new Duration(date1, date2);
// ...
formatter.print( duration.toPeriodFrom(date1));
这篇关于期间没有显示天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!