/** * date1 开始时间 * date2 结束时间 * **/ public static String getDate(Date date1,Date date2){ SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); long between = 0; try { java.util.Date begin = dfs.parse(dfs.format(date1)); java.util.Date end = dfs.parse(dfs.format(date2)); between = (end.getTime() - begin.getTime());// 得到两者的毫秒数 } catch (Exception ex) { ex.printStackTrace(); } long day = between / (24 * 60 * 60 * 1000); long hour = (between / (60 * 60 * 1000) - day * 24); long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60); long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60); long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000 - s * 1000); return day + "天" + hour + "小时" + min + "分" + s + "秒" + ms + "毫秒"; }
public static void main(String[] args) { java.util.Date date1=new Date(); try{ Thread.sleep(2000); }catch (Exception e){ } java.util.Date date2=new Date(); System.out.println(getDate(date1,date2)); }