How do I calculate difference in days between two dates?

In the following example we are going to calculate the differences between two dates. First, you’ll need to convert the date value into its corresponding value in milliseconds and then do the math calculation so we can get the difference in days, hours, minutes, etc.

For example to get the difference in day you’ll need to divide the difference in milliseconds with (24 * 60 * 60 * 1000).

Here is the result:

The better way to get number of days between two dates is to use the Joda Time API. In the following example you can see how to calculate date difference using Joda Time: How do I get number of days between two dates in Joda?.

  • Stephen M

    The day calculation is actually wrong if you cross a “Spring Forward” DST boundary. It will claim you’re only 123 days apart instead of 124 as you only went back 123 days and 23 hours.