在android中将设备时区更改为GMT

时间:2012-11-15 09:36:42

标签: android gmt timestamp-with-timezone

“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

1 个答案:

答案 0 :(得分: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