Im trying to calculate the difference between a time returned to me by an API and the current device time.
The API returns time in this format: "game_start": "2015-09-03 19:00:00"
To calculate the difference I do this:
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;
}
}
The problem I have is that the calculation always returns the time remaining as an hour more that it should be, until it gets to the time that it is actually meant to start then it returns that there is no time remaining e.g.
current time = 16:59 time it starts at = 17:00 time remaining that is returned 1hrs 1mins
then
current time = 17:00 time it starts at = 17:00 time remaining that is returned League Started