【问题标题】:how to find difference between two date Time string?如何找到两个日期时间字符串之间的差异?
【发布时间】:2016-01-24 12:13:12
【问题描述】:

我正在计算两个日期和时间之间的时间差,但它的重新调整无效差。

这是我计算差异的示例日期和代码。

loginTime=2016-01-24 12:04:30.16
expiryTime = 2016-01-24 13:04:30.16

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date now = new Date();


        try {
            Date startDate = simpleDateFormat.parse(loginTime);
            Date expireDate = simpleDateFormat.parse(expiryTime);

//            String temCurrentDate = new SimpleDateFormat("yyyy-MM-dd").format(now);
//            Date currentDate = new SimpleDateFormat("yyyy-MM-dd").parse(temCurrentDate);
//            String day_of_week = new SimpleDateFormat("EEEE").format(now);

            long difference =startDate.getTime()- expireDate.getTime();

            int hour = (int) difference / (60 * 60 * 1000);


        } catch (ParseException e) {
            e.printStackTrace();
        }

这个时间之间的差是 1 小时,但我得到 11 或 13。请告诉我在哪里出错了。

【问题讨论】:

标签: android datetime


【解决方案1】:

变量中的拼写错误:

simpleDateFormat.parse(expiryTime);

不匹配:ExpiryTime = 2016-01-24 13:04:30.16

这就是问题的全部。

【讨论】:

  • 我在输入问题时犯了这个拼写错误
  • 好吧,奇怪的是,该错误会导致您的代码发现许多小时的差异,因为其中一个时间是空白的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多