Nat*_*th5 6 java datetime jodatime periodformatter
我使用PeriodFormatter返回一个字符串,显示事件发生的时间.下面的代码不断创建字符串,如1146小时39分钟,而不是x天y小时z分钟.有任何想法吗?
谢谢,内森
PeriodFormatter formatter = new PeriodFormatterBuilder()
.printZeroNever()
.appendDays()
.appendSuffix( "d " )
.appendHours()
.appendSuffix( "h " )
.appendMinutes()
.appendSuffix( "m " )
.toFormatter();
return formatter.print( duration.toPeriod() );
Run Code Online (Sandbox Code Playgroud)
这是因为您使用Duratoin :: toPeriod方法将Duration转换为Period.
这在Joda-time文档中有所描述:
public Period toPeriod()使用标准句点类型和ISO年表将此持续时间转换为Period实例.仅使用句点类型中的精确字段.因此,仅使用该时段上的小时,分钟,秒和毫秒字段.不会填充年,月,周和日字段.
如果没有开始日期(或结束日期),则无法正确计算期间金额,因为有些日子可能是24小时或23 小时(由于夏令时)
你应该使用方法Duration :: toPeriodFrom方法而不是
Eg
Duration duration = new Duration(date1, date2);
// ...
formatter.print( duration.toPeriodFrom(date1));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |