Calculating difference in dates in Java

Anirudh picture Anirudh · Jan 17, 2009 · Viewed 10.4k times · Source

I find it funny that Java (or the java.util library) does not have a built-in function to calculate difference in dates. I want to subtract one date from another to get the elapsed time between them. What is the best way to do this?

I know the simple way is to take the difference of the time in milliseconds and then convert that into days. However, I wanted to know if this works in all cases (with daylight saving, etc.).

Answer

duffymo picture duffymo · Jan 17, 2009

Java's not missing much, if you look at open source: try Joda-Time.