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.).
If your times are derived from UTC dates, or they are just the difference between two calls to System.getCurrentTimeMillis() measured on the same system, you will get a valid number of milliseconds as the difference, independent of any timezone issues. (which is why everything should be using UTC as a storage format — it’s much easier to go from UTC->local time; if you try to go the other way then you need to store the local timezone along with the local time — or attempt to infer it, gack!)
As for turning this into a number of days, you should just be able to divide by 86400000… with the caveat that there is an occasional leap second every other year or so.