java基础之,计算两个时间的差值

话不多说,咱们直接上代码把

 public static void test(String endDate,String newDate) throws ParseException {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 *60;
        long nm = 1000 * 60;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = df.parse(endDate);
        Date d2 = df.parse(newDate);
        long diff = d1.getTime() - d2.getTime();
        long day = diff/nd;
        long hour = diff/nh;
        long minute = diff/nm;
        log.info("相差的天数为:"+day);
        log.info("相差的小时为:"+hour);
        log.info("相差的分钟数为:"+minute);

    }

然后再主函数里面跑这个

public static void main(String[] args) throws ParseException {
        test("2019-02-15 10:00:00","2019-02-15 09:00:00");
    }

显示的效果

 

posted @ 2019-02-15 10:52  正能量教官  阅读(5230)  评论(0编辑  收藏  举报