# Duration

# 介绍

可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、Instants等时间。

# 常用方法

方法 说明
public static Duration between(开始时间对象1,截止时间对象2) 传入2个时间对象,得到Duration对象
public long toDays() 计算隔多少天,并返回
public long toHours() 计算隔多少小时,并返回
public long toMinutes() 计算隔多少分,并返回
public long toSeconds() 计算隔多少秒,并返回
public long toMillis() 计算隔多少毫秒,并返回
public long toNanos() 计算隔多少纳秒,并返回

# 方法实例

public class PeriodTest {

    public static void main(String[] args) {
        LocalDateTime of = LocalDateTime.of(2020, 2, 1, 12, 1);
        LocalDateTime of1 = LocalDateTime.of(2023, 5, 30,15,2);

        Duration between = Duration.between(of, of1);

        System.out.println(between.getSeconds());
        System.out.println(between.getNano());

        long days = between.toDays();
        long hours = between.toHours();
        System.out.println(days);
        System.out.println(hours);

    }
}