java LocalDateTime 加时间,计算两个时间的差

 LocalDateTime localDateTime = LocalDateTime.now();
 localDateTime= localDateTime.plusDays(1);//加一天

计算两个时间的差

System.out.println("计算两个时间的差:");
        String time2 = "2020-06-26 19:00:00";
        String time1 = "2020-06-26 19:00:30";

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime end = LocalDateTime.parse(time1, dtf2);
       // LocalDateTime end = LocalDateTime.now();
        //LocalDateTime now = LocalDateTime.now();
        LocalDateTime now = LocalDateTime.parse(time2, dtf2);
        Duration duration = Duration.between(now,end);
        long days = duration.toDays(); //相差的天数
        long hours = duration.toHours();//相差的小时数
        long minutes = duration.toMinutes();//相差的分钟数
        long millis = duration.toMillis();//相差毫秒数
        long nanos = duration.toNanos();//相差的纳秒数
        System.out.println(now);
        System.out.println(end);

        System.out.println("发送短信耗时【 "+days+"天:"+hours+" 小时:"+minutes+" 分钟:"+millis+" 毫秒:"+nanos+" 纳秒】");
计算两个时间的差:
发送短信耗时【 0天:0 小时:0 分钟:30000 毫秒:30000000000 纳秒】

3做比较

        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

你可能感兴趣的:(java LocalDateTime 加时间,计算两个时间的差)