java计算时间差精确到秒

private static void getBetweenTime(String start, String type)
   throws ParseException {
  SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  java.util.Date begin = dfs.parse(start);
  java.util.Date end = dfs.parse(getCurrentTime());
  long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
  long day1 = between / (24 * 3600);
  long hour1 = between % (24 * 3600) / 3600;
  long minute1 = between % 3600 / 60;
  long second1 = between % 60 / 60;
  System.out.println(type + "相差" + day1 + "天" + hour1 + "小时" + minute1
    + "分" + second1 + "秒");
 }

by 雪洁 2011-07-01 17:50:31 3635 views
我来说几句

相关文章