java比较两个时间的差值

矫情吗;* 2022-08-23 04:48 308阅读 0赞
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  2. Date before = sdf.parse("2015-07-01 08:58:17");
  3. Date now=sdf.parse("2015-07-02 09:03:23");
  4. Calendar c1 = Calendar.getInstance();
  5. Calendar c2 = Calendar.getInstance();
  6. c1.setTime(before);
  7. c2.setTime(now);
  8. long l=now.getTime()-before.getTime();
  9. long day=l/(24*60*60*1000);
  10. long hour=(l/(60*60*1000)-day*24);
  11. long min=((l/(60*1000))-day*24*60-hour*60);
  12. long s=(l/1000-day*24*60*60-hour*60*60-min*60);
  13. System.out.println("耗时:"+day+"天"+hour+"小时"+min+"分"+s+"秒");
  14. //另 compareTo 方法比较时间是否相等,返回值为:-1、0、1
  15. //int i= now.compareTo(before));
  16. //now比before早,返回-1,
  17. //now与before相同,返回0
  18. //now比before晚,返回1

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读