简体   繁体   中英

How does java.time.Duration calculate toDays()?

So the java doc on this is pretty sparse:

public 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.

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. Standard integer division.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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