Hi,

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

Thanks in advance.

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...

        Instant instant1, instant2; 
        long interval;

        instant1 = Instant.now();
        instant2 = Instant.now().plus(48, ChronoUnit.HOURS);

        interval = ChronoUnit.DAYS.between(instant1,instant2);

        System.out.println(instant1 +" - " + instant2 + " = " +  interval);

Don't worry about leap years etc, these classes use the most correct algorithms for dates known to man.

HI james,

Thanks for the response.

But I have two Date object. I have to use Date date1 , Date date2. Now, I want to find days between them. Can we incorporate Date datatype?

Also, It is correct?

long diff = date2.getTime() - date1.getTime();
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);

Basically, I want to get rid of leap year problem, day-light saving time thing etc. So, can you please suggest now?

Use Date's toInstant() method to convert your Dates to Instants, then proceed as above.
return ChronoUnit.DAYS.between(date1.toInstant(), date2.toInstant());
Couldn't be easier.

Thanks James for the quick response.

Instant instant1 = date1.toInstant();
Instant instant2 = date2.toInstant();

return ChronoUnit.DAYS.between(instant1,instant2);

Seems good?

Also, can you point out possible things in the code snippet I provided in my last comment? That will improve my understanding.

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
and
b} its going to work properly
and
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.

Hey James,

The code we discussed about date is giving zero days.

Instant startInstant = myclassObj.getStartTime().tonstant() ;
Instant endInstant = myclassObj.getExpiredTime().toInstant();

long days = ChronoUnit.DAYS.between(startInstant, endInstant);

:(

Without knowing the values of the two times I can't comment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.