DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 获取当前时间 Date date = new Date(); String nowtime = df.format(date); // System.out.println(nowtime); Date d2 = df.parse(2015-12-3 12:12:12); // Date d1 = df.parse(nowtime); long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别 long days = diff / (1000 * 60 * 60 * 24);//天 long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); //小时 long sc = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000); // 秒
如果需要我们可以通过 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 这种方法把sc转换成我们自己想要的样子