如何找到两个日期时间字符串之间的差异?

时间:2016-01-24 12:13:13

标签: android datetime

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

这是我的样本日期和代码来计算差异。

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.请告诉我在哪里出错。

1 个答案:

答案 0 :(得分:0)

变量中的拼写错误:

if(!$conn->set_charset("utf-8")){
   exit("Error loading character set utf8".$conn->error);}

不匹配:simpleDateFormat.parse(expiryTime);

这就是整个问题。