我在安卓应用程序上工作,我从服务器上获取日期,比如:Thu Jul 02 14:18:58 GMT+04:00 2015
,通过SimpleDateFormat解析后,我得到的是:2015年7月2日星期四下午02:18。通过Calender.getTime()
,我得到时间Thu Jul 02 14:57:25 GMT+04:00 2015
,解析后,得到Thursday , July 02 , 2015 02:57 PM
。现在我只需要计算两个日期和时间之间的时差。我正在做下面的事情,但它给了我NumberFormat异常。请帮帮我。
Calendar cal = Calendar.getInstance();
cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE , MMMM dd , yyyy hh:mm a");
String systemTime = sdf.format(cal.getTime());
int lastLocationSyncInt = Integer.parseInt(lastLocationSync);
int systemTimeInt = Integer.parseInt(systemTime);
int lastSeenTimeDifference = systemTimeInt -lastLocationSyncInt ;
mMap.addMarker(markerOptions).setSnippet(lastSeenTimeDifference+"");
不是转换为Integer,而是格式化日期并获得如下所示的差异
long days = 0;
long hours = 0;
SimpleDateFormat sourceFormat = new SimpleDateFormat(
"EEEE , MMMM dd , yyyy hh:mm a");
Date sDate = null;
Date sEdate = null;
try {
sDate = sourceFormat.parse(startdate);
sEdate = sourceFormat.parse(enddate);
long diff = sDate.getTime() - sEdate.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
hours = minutes / 60;
days = hours / 24;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果您有任何疑问,请随时评论