JAVA--java计算时间差,日期差小结

求两个时间值之间相差几分钟

    public static void main(String[] args) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String currentTime="2020-1-2 15:25:00";
        Date parseCurrentTime = df.parse(currentTime);
        long newCurrentTime = parseCurrentTime.getTime();
        //从数据库中拿到时间
        String reportTime="2020-1-2 15:10:00";
        Date parseReportTime = df.parse(reportTime);
        long oldReportTime = parseReportTime.getTime();

        long diff=(newCurrentTime-oldReportTime)/1000/60;
        System.out.println("当前系统时间为:"+newCurrentTime+"数据中的上报时间:"+oldReportTime+"两个时间差为:"+diff);
    }

执行结果

在这里插入图片描述

获取两个日期相差的天数

  /**
     * 获取两个日期相差的天数
     *
     * @param startDateStr
     * @param endDateStr
     */
    public static int getDayBetweenTwoDate(String startDateStr, String endDateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            Date startDate = simpleDateFormat.parse(startDateStr);
            Date endDate = simpleDateFormat.parse(endDateStr);
            Calendar startCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            //计算两个日期相差的天数
            //startCalendar.getTime().getTime()返回long毫秒数形式,毫秒转为秒所以除以1000
            //1天=24小时,1小时=60分,1分=60秒,所以两个时间的差再除以60 * 60 * 24换算成天的形式
            days = ((int) (startCalendar.getTime().getTime() / 1000) - (int) (endCalendar.getTime().getTime() / 1000)) / (60 * 60 * 24);
 
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

获取两个日期相差的天数2种

     public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.parse("2020-03-10 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("计算两个时间的差:");
        LocalDateTime end = LocalDateTime.now();
        //方法1
        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);
        System.out.println("发送短信耗时【 " + days + "天:" + hours + " 小时:" + minutes + " 分钟:" + millis + " 毫秒:" + nanos + " 纳秒】");
        方法2//
        LocalDate localDate1 = LocalDate.of(now.getYear(),now.getMonthValue(),now.getDayOfMonth());
        LocalDate localDate2 = LocalDate.of(end.getYear(),end.getMonthValue(),end.getDayOfMonth());

        long day=localDate2.toEpochDay()-localDate1.toEpochDay();
        System.out.println("相差了的天数:"+day);
    }

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值