How to determine the time between now and an arbitrary future date in Java

前端 未结 4 1562
失恋的感觉
失恋的感觉 2020-12-07 05:17

I\'m currently trying to determine the amount of time between two dates (one of which is the present date/time, the other an arbitrary future date). I\'m using native Java a

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 05:54

    Instead of doing this :

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

    build a Date object with the difference between the two dates (long value). Then you can do a setDate(Date) on a Calendar object and parse it.

    Alternatively, you can take a look at the Joda Time Api, which is relatively easy to use.

提交回复
热议问题