Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Date and Time Difference in Days, Hours, Minutes and Seconds in Android

Tags:

android

I need to get difference between current date and a date in future, in days , hours , minutes and seconds in android. Like if current date and time is 17/05/2012 03:53:00 and a date in future is 18/05/2012 04:55:00.

I need to display difference as remaining time= day: 1, hours: 1, and minutes 2.

Any kind of help will be appreciated . Many thanks in advance.

Regards, Munazza K

like image 635
Munazza Avatar asked May 17 '12 10:05

Munazza


2 Answers

You can do this way

   c_date=18/05/2012 04:55:00.  and  saved_date=17/05/2012 03:53:00

   long diffInMillisec = c_date.getTime() -saved_date.getTime();
   long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMillisec);
   seconds = diffInSec % 60;
   diffInSec/= 60;
   minutes =diffInSec % 60;
   diffInSec /= 60;
   hours = diffInSec % 24;
   diffInSec /= 24;
   days = diffInSec;`
like image 71
Khan Avatar answered Oct 26 '22 12:10

Khan


You can subtract both dates, and the calculate the differences. Kinda like this:

long difference = calendar.getTimeInMillis()-currentTime;

    long x = difference / 1000;
    seconds = x % 60;
    x /= 60;
    minutes = x % 60;
    x /= 60;
    hours = x % 24;
    x /= 24;
    days = x;

You can subtract the time you've already calculated. You get the hours, get the rest, do the minutes, etc.

like image 29
RMartins Avatar answered Oct 26 '22 11:10

RMartins