简体   繁体   中英

How to calculate the difference (in days) between two DateTime objects?

I started playing with JodaTime and I failed to find a handy way to calculate the difference in days between 2 DateTime objects. The best I came up with is below:

    //given DateTime dt1 and dt2
    long distanceInMillis = dt2.getMillis()-dt1.getMillis();
    int distanceInDays = (int)(distanceInMillis / 24*60*60*1000L);

I would appreciate your suggestion of how this can be done better.

Nooooooo! So many Daylight Savings bugs from counting days using milliseconds :)

Joda provides

Days d = Days.daysBetween(dt1, dt2);
int days = d.getDays();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM