aviad
aviad

Reputation: 8278

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.

Upvotes: 2

Views: 1311

Answers (1)

Affe
Affe

Reputation: 47974

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

Joda provides

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

Upvotes: 8

Related Questions