LocalDateTime 比较大小,计算两个LocalDateTime的时间差时分秒

LocalDateTime 比较大小

最近开发快递物流轨迹对接 获取 快递轨迹的时候 需要 判断哪些轨迹信息是 已经保存过得 必须进行 时间比较
实体用的都是 年月时间 格式 以下是LocalDateTime 比较方法

public static void main(String[] args) {
        //获取当前时间
        LocalDateTime nowTime= LocalDateTime.now();
        //自定义时间
        LocalDateTime endTime = LocalDateTime.of(2021, 10, 23, 14, 10, 10);
        //比较  如今的时间 在 设定的时间 之后  返回的类型是Boolean类型
        System.out.println(nowTime.isAfter(endTime));
        //比较   如今的时间 在 设定的时间 之前  返回的类型是Boolean类型
        System.out.println(nowTime.isBefore(endTime));
        //比较   如今的时间 和 设定的时候  相等  返回类型是Boolean类型
        System.out.println(nowTime.equals(endTime));
    }

例子 :
如果是之后就进行存储
在这里插入图片描述
相关方法

   //第一种
        String time1 = "2019-06-26 19:00:00";
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(time1, dtf2);
        System.out.println(localDateTime.isBefore(LocalDateTime.now()));//你的时间在当前时间之前是true
        System.out.println(localDateTime.isAfter(LocalDateTime.now()));//在当前时间之后是false
     //第二种
     LocalDateTime now = LocalDateTime.now();
     Duration duration = Duration.between(now,now);
        System.out.println(duration);
        long days = duration.toDays(); //相差的天数
        long hours = duration.toHours();//相差的小时数
        long minutes = duration.toMinutes();//相差的分钟数
        long millis = duration.toMillis();//相差毫秒数
        long nanos = duration.toNanos();//相差的纳秒数
        System.out.println(millis);
     //第三种
        long mills = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        System.out.println(mills >= System.currentTimeMillis());

计算时间差时分秒

方法1

        LocalDateTime fromDate = LocalDateTime.now();
        LocalDateTime toDate = LocalDateTime.now().plusHours(40);

        long minutes = ChronoUnit.MINUTES.between(fromDate, toDate);
        long hours = ChronoUnit.HOURS.between(fromDate, toDate);

        System.out.println(minutes);
        System.out.println(hours);

方法2

         LocalDateTime fromDate = LocalDateTime.now();
         LocalDateTime toDate = LocalDateTime.now().plusHours(40);

         Duration duration = Duration.between(fromDate, toDate);

        long minuts = duration.toMinutes();
        long hours = duration.toHours();

例子:

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗小稳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值