Ste*_*veD 91
您需要对周期进行标准化,因为如果使用总秒数构造它,那么这是它唯一的值.将其标准化将其分解为总天数,分钟,秒等.
由ripper234编辑 - 添加TL; DR版本:PeriodFormat.getDefault().print(period)
例如:
public static void main(String[] args) {
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(" and ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
Period period = new Period(72, 24, 12, 0);
System.out.println(daysHoursMinutes.print(period));
System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}
Run Code Online (Sandbox Code Playgroud)
将打印:
24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds
因此,您可以看到非标准化时段的输出只是忽略小时数(它没有将72小时转换为3天).
sim*_*mao 22
您也可以使用默认格式化程序,这对大多数情况都有好处:
Period period = new Period(startDate, endDate);
System.out.println(PeriodFormat.getDefault().print(period))
Run Code Online (Sandbox Code Playgroud)
Jhe*_*ico 12
Period period = new Period();
// prints 00:00:00
System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
period = period.plusSeconds(60 * 60 * 12);
// prints 00:00:43200
System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
period = period.normalizedStandard();
// prints 12:00:00
System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37048 次 |
| 最近记录: |