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
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;`
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With