我想在android中计算两次的差异: 例如Date now和1356033600000之间的差异 - >来自location.getTime()的时间戳
= 2分钟前
我只需要分钟! 我该怎么办?
我通过json获得clienttime。 clienttime = 1356033600000
String clienttime = e.getString("clienttime");
long mTime = (System.currentTimeMillis() - Long.valueOf(clienttime).longValue()) / (60 * 1000);
答案 0 :(得分:1)
在Android / Java中,时间通常以毫秒为单位计算,您可以使用现有常量来帮助您执行此简单检查:
// now minus two minutes
if(location.getTime() > System.currentTimeInMillis() - 2 * DateUtils.MINUTE_IN_MILLIS) {
// The location is less than two minutes old
}
else {
// The location is possibly stale
}
当您使用LocationManager#getLastKnownLocation()
时,这是常见的检查,只需确保location
在调用location.getTime()
之前不为空。
答案 1 :(得分:1)
(System.currentTimeMillis() - location.getTime())/(60 * 1000)
答案 2 :(得分:0)
这是我在自己的应用程序中使用的代码片段:
String oldDate = xmlResults[3]; //insert old dateTime in the following format: yyyy-MM-dd HH:mm:ss
int myValue = 15; //Check if difference is between 15 hours
// Date Time Format
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
// Convert Date to Calendar
Date xmlDate = formatter.parse(oldDate);
Calendar cal = Calendar.getInstance();
cal.setTime(xmlDate);
Calendar today = Calendar.getInstance();
// Check difference
long diff = today.getTimeInMillis() - cal.getTimeInMillis();
long hours = diff / 3600000; // 1000ms * 60sec * 60hours ->
// total hours
if (hours < myValue && hours >= 0) {
// Login is successful, return true
return true;
} else {
return false;
}
您可以轻松更改为代码来比较minuts。