Java – calculates the time difference between the current and future times

I want to calculate the time difference (in milliseconds) between the time of the day (11:00 a.m. on October 1, 2012) and the midnight time of the same day (11:59:59 p.m. on October 1, 2012)

I 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();

Today is today's date

But when I print out the difference between the long cal value and today's time, if it is 86400, it is about a day

resolvent:

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();

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>