Android 剩余时间的计算总是错了一个小时
我试图计算API返回给我的时间和当前设备时间之间的差值 API以这种格式返回时间:“游戏开始”:“2015-09-03 19:00:00” 要计算差异,请执行以下操作:Android 剩余时间的计算总是错了一个小时,android,calendar,Android,Calendar,我试图计算API返回给我的时间和当前设备时间之间的差值 API以这种格式返回时间:“游戏开始”:“2015-09-03 19:00:00” 要计算差异,请执行以下操作: protected String getTimeToStart(JSONObject jsonObject) { String time = ""; Calendar startTime; Calendar current; startTime = Calendar.getInstance()
protected String getTimeToStart(JSONObject jsonObject) {
String time = "";
Calendar startTime;
Calendar current;
startTime = Calendar.getInstance();
current = Calendar.getInstance();
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat format2 = new SimpleDateFormat("HH mm ");
startTime.setTime(format.parse(jsonObject.getJSONObject("data").getJSONObject("game").getString("game_start")));
String[] splited = (format2.format(startTime.getTimeInMillis() - current.getTimeInMillis()).split("\\s+"));
time = splited[0] + "hrs " + splited[1] + "mins";
} catch (ParseException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
if (checkStarted(startTime, current)) {
return time;
} else {
return "League Started";
}
protected boolean checkStarted(Calendar start, Calendar current) {
if (current.before(start)) {
return true;
} else {
return false;
}
}
我的问题是,计算总是返回剩余时间,比它应该的时间多一个小时,直到它到达实际开始的时间,然后返回没有剩余时间,例如
当前时间=16:59
开始时间=17:00
返回的剩余时间为1小时1分钟
然后
当前时间=17:00
开始时间=17:00
从文本解析日期时返回的剩余时间必须考虑区域设置和时区。请看我的回答: