Question

"13-11-2012 09:14:39" is the sample timestamp string getting from server in my app. I need to calculate the defference between that time and current device time. My Server is sending the time in GMT format. Do i need to change the device timezone to GMT to calculate current time in GMT? And how can i get exact diff b/w those two times. Please help me.
Here is what i have tried so far:

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
Was it helpful?

Solution

TRy this,

 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top