两个字符串时间算时间差

朱雀 2022-06-09 06:29 289阅读 0赞
  1. SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  2. Date startDate = null;
  3. Date endDate = null;
  4. try {
  5. startDate=inputFormat.parse("2017-09-01 20:00:00");
  6. endDate=inputFormat.parse("2017-09-02 20:00:00");
  7. } catch (ParseException e) {
  8. e.printStackTrace();
  9. }
  10. //得到两个日期对象的总毫秒数
  11. long firstDateMilliSeconds = startDate.getTime();
  12. long secondDateMilliSeconds = endDate.getTime();
  13. //得到两者之差
  14. long firstMinusSecond = secondDateMilliSeconds - firstDateMilliSeconds;
  15. //得到秒
  16. long totalSeconds = (firstMinusSecond / 1000);

发表评论

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

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

相关阅读