[英]how to find difference between two date Time string?
I am calculating time difference between two dates and time but its restuning invalid difference. 我正在计算两个日期和时间之间的时差,但恢复了无效的时差。
here's my sample date and code to calculate the difference. 这是我的示例日期和计算差异的代码。
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();
}
Difference between this time is 1 hour but i am getting 11 or 13. Please tell where i am going wrong in this. 这段时间之间的时差是1小时,但我要11或13。请告诉我我在哪里出错了。
Spelling error in your variable: 变量中的拼写错误:
simpleDateFormat.parse(expiryTime);
Does not match: ExpiryTime = 2016-01-24 13:04:30.16
不匹配:
ExpiryTime = 2016-01-24 13:04:30.16
That's the whole problem. 这就是整个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.