简体   繁体   English

java.time.Duration如何计算toDays()?

[英]How does java.time.Duration calculate toDays()?

So the java doc on this is pretty sparse: 因此,关于此的Java文档非常稀疏:

public long toDays() 公共long toDays()

Gets the number of days in this duration. 获取此持续时间中的天数。 This returns the total number of days in the duration by dividing the number of seconds by 86400. This is based on the standard definition of a day as 24 hours. 这将通过将秒数除以86400来返回持续时间的总天数。这是基于一天的标准定义为24小时。

This instance is immutable and unaffected by this method call. 此实例是不可变的,不受此方法调用的影响。

Returns: the number of days in the duration, may be negative 返回:持续时间中的天数,可能为负

straight forward enough, but what does this do? 直截了当, 但这怎么办?

// set duration to 36 hours
Duration duration = Duration.parse("PT36H");

// get days in duration
Long daysInDuration = duration.toDays();

System.out.println(daysInDuration);

does it round, or just take the floor? 它是圆的还是只是地板? My inclination is that it take the floor... 我的意思是,它占据了地板...

public long toDays() {
  return seconds / SECONDS_PER_DAY;
}

It divides a long by an int and assigns is to a long , so no smart rounding here. 它用longint除以int并把int分配给long ,所以这里没有智能舍入。 Standard integer division. 标准整数除法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM