我试图计算API返回给我的时间与当前设备时间之间的差异。
API以此格式返回时间:“game_start”:“2015-09-03 19:00:00”
为了计算差异,我这样做:
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开始 返回联赛开始的剩余时间