I have yet to find a straightforward approach to this. I've seen tons of different examples, some actually clash with each other, others are using deprecated calls, I'm not sure why this is so difficult to find a solid answer for.
So I have a timestamp returned from a web service with this format:
"yyyy-MM-dd hh:mm:ss"
*The time is in Eastern Time even though the timezone is not part of their time stamp.
All I want to know is the best way to calculate the time difference between the web service's time stamp(EST) and the current time stamp of the phone being used, which could be in any time zone. Then display that difference as a countdown in days left, hours left, and minutes left.
Currently I'm doing something like this:
public String getTimeLeft(String date) {
StringBuilder dateString = new StringBuilder();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("EST"));
Date dateObj = formatter.parse(date);
Time time = new Time(Time.getCurrentTimezone());
time.set(dateObj.getTime());
time.timezone = Time.TIMEZONE_UTC;
long timeInMill = time.normalize(true);
endCal.setTimeInMillis(timeInMill);
currCal.setTime(new Date());
long timeDiff = endCal.getTimeInMillis() - currCal.getTimeInMillis();
int days = (int) (timeDiff / (1000*60));
int hours = (int) ((timeDiff / (1000*60*60))%24);
int minutes = (int) ((timeDiff % (1000*60*60)) / (1000*60));
dateString.append(days + (days == 1 ? " DAY " : " DAYS "));
dateString.append(hours + (hours == 1 ? " HOUR " : " HOURS "));
dateString.append(minutes + (minutes == 1 ? " MINUTE " : " MINUTES "));
return dateString.toString();
}
...and something there is wrong with the timezone conversions as the time differences aren't accurate. Any help is much appreciated. Thanks in advance.