I am stuck with a problem. I have two Date objects (util class objects). I want the difference between these two dates as the number of days between them? I am using Java 8. Can you please help me with this? Searched the web a lot. Some are saying to use Joda, while some are using TimeUnit. Some says Timeunit not handles leap year properly etc. I am confused now. Here is the link I read: http://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-in-java
In Java 8 all your problems are solved!
It includes a completely new package for handling dates, times, intervals etc based on everything that was wrong with the old classes, and stealing a lot from Joda time
You just need the ChronoUnit.between method with two Instant objects
Theres a really simple example here: http://docs.oracle.com/javase/tutorial/datetime/iso/period.html
.. but unbelievably there's a bug in the example (gap is declared as the wrong type, should be long), so here's a working example that illustrates just how neat the new classes are...
Your earlierccode snippet is OK, althoug you may as well just divide by the number of mSec in a day. There may be some really obscure traps in there because Date objects are in millisecs so you have to be vary careful about the time-of-day part of the object (especially when changing to/from DST). Personally I suggested what I did because
a) it goes with the current date/time support rather than the inferior earlier one
b} its going to work properly
c the code reads so clearly that even a non-Java newbie could guess what it means. I'm the world's biggest fan of easy-toread-and-understand code. In fact I'm obsessed with it.