Java之将时间戳转化为LocalDateTime

最近在编码过程中遇到将时间戳转化为 LocalDateTime ,并进行与当前时间进行对比的情况,用来记录一下自己的解决方案。

话不多说,直接上代码:

    private static boolean judgeTime(Long time) {
        LocalDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneOffset.systemDefault()).toLocalDateTime();
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 计算两个时间的时差
        Duration duration = Duration.between(dateTime, now);
        long diffInDays = duration.toDays();
        return diffInDays < 7;
    }

代码解释:这段逻辑是为了比较时间戳对应的时间与当前时间是否相差 7 天

代码debug截图:

猜你喜欢

转载自blog.csdn.net/JonTang/article/details/131109693
今日推荐