Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate time difference between current and future time

Tags:

java

android

I want to calculate time difference in milliseconds from current time of a day(11 am , 1 october,2012) and time at midnight for the same day (11 pm 59 m 59s , 1 october , 2012.

I have tried this

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 59);
    cal.add(Calendar.HOUR, 23);
    cal.add(Calendar.MINUTE, 59);
        cal.getTime().getTime() - today.getTime();

here today is the current date.

But when i print long values of cal and today , the time difference if of 86400 approx one day.

like image 844
Prachur Avatar asked Dec 06 '22 11:12

Prachur


1 Answers

Use cal.set() instead of cal.add()

Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);

long diff = cal.getTime().getTime() - today.getTime();
like image 144
iTurki Avatar answered Jan 20 '23 06:01

iTurki