Android日期差返回时间(分钟)
我正在从db获得UTC日期。从当前utc日期时间中减去。我想在几分钟内得到差异。有点不对劲Android日期差返回时间(分钟),android,Android,我正在从db获得UTC日期。从当前utc日期时间中减去。我想在几分钟内得到差异。有点不对劲 Date currenttimedate = DateTime.UTCNow(); String lastsynctime = c.getString(c.getColumnIndex(columnname)).toString(); Date lastruntimedate = DateTime.StringDateToDate(l
Date currenttimedate = DateTime.UTCNow();
String lastsynctime = c.getString(c.getColumnIndex(columnname)).toString();
Date lastruntimedate = DateTime.StringDateToDate(lastsynctime, "MM/dd/yyyy HH:mm:ss");
long diffInMs = (currenttimedate. getTime() - lastruntimedate.getTime());
// convert it to Minutes
long diffInMins = TimeUnit.MILLISECONDS.toMinutes(diffInMs);
// return time in minutes
response = (int)(diffInMins);
它返回了大约720分钟。预期答案不能超过5个。因为数据每5分钟同步一次。添加一些调试行,以确保currenttimedate和lastruntimedate是您认为的那样。例如,时区是否有问题?您是否查看了返回的时间,以确保差异应为5分钟左右?是的。db的日期为2013年2月26日下午06:45:29。UTCNow是2013年2月26日星期二18:45:36 CST。所以差应该是0分钟。它显示720分钟,而getTime()返回1361926090000 1361882832000。您发布的两次时间差为720分钟(0645->1845)。确保两次都使用UTCNow,或者至少确保它们位于同一时区。