Dinesh Anuruddha Dinesh Anuruddha - 2 months ago 13
Android Question

Get time difference in android?

In my android app, i am getting date and time in following format via HTTP request.


Tue Apr 17 16:23:33 IST 2012


Now i want to calculate the time difference between that time and current time.I have got lot of ways to calculate time difference in the internet but all those solutions are with different formats.How can i calculate time difference with above time format?

Edit:
This is the code i used its working fine.

DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date dateOne = df.parse("Tue apr 22 13:07:32 IST 2012");
Date dateTwo = df.parse("Tue Apr 22 13:07:33 IST 2012");
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
System.out.println("difference:" + timeDiff); //differencs in ms


but i have to take current time in format "Tue apr 22 13:07:32 IST 2012" to use as dateTwo.

Answer

you can also this code:::

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
Date dateOne = df.parse("2011-02-08 10:00:00 +0300");
Date dateTwo = df.parse("2011-02-08 08:00:00 +0100");   
long timeDiff = Math.abs(dateOne.getTime() - dateTwo.getTime());
System.out.println("difference:" + timeDiff);   // difference: 0