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"); }
显示的效果