0

“13-11-2012 09:14:39”是从我的应用程序中的服务器获取的示例时间戳字符串。我需要计算该时间与当前设备时间之间的差异。我的服务器以 GMT 格式发送时间。我需要将设备时区更改为 GMT 以计算 GMT 的当前时间吗?我怎样才能得到这两次准确的黑白差异。请帮我。
这是我到目前为止所尝试的:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

Date requestDate = formatter.parse(serverDateString);

Calendar requestDateCal = formatter.getCalendar();
requestDateCal.setTime(requestDate);

Calendar currentDateCal = Calendar.getInstance();
calculateTimeDiff(currentDateCal.getTimeInMillis(), requestDateCal.getTimeInMillis()); //finds diff between two timestamps
4

1 回答 1

1

试试这个,

 SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

 formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
 Date requestDate = formatter.parse(serverDateString);

 Calendar requestDateCal = formatter.getCalendar();
 requestDateCal.setTime(requestDate);

 Calendar currentDateCal = Calendar.getInstance();
 currentDateCal.setTimeZone(TimeZone.getTimeZone("GMT"));
 calculateTimeDiff(currentDateCal.getTimeInMillis(), requestDateCal.getTimeInMillis()); //finds diff between two timestamps
于 2012-11-15T09:57:45.130 回答