How to calculate the difference between two dates (java.util.Date)

_Esam
_Esam used Ask the Experts™
on
How do I get the difference (int value) of two java.util.Date dates?

Like difference between 2007-05-31 and 2007-05-29 is 2.

Thanks.
_Esam
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Date start=....;
Date end=....;
long diffInMilleseconds = end.getTime() - start.getTime();
long diffInSeconds = diffInMilleseconds/1000;
long diffInMinutes = diffInSeconds/60;

Author

Commented:
I need to find the difference between days ? How many days?
Yeah I figured you could continue the logic on your own but here it is for you
Date start=....;
Date end=....;
long diffInMilleseconds = end.getTime() - start.getTime();
long diffInSeconds = diffInMilleseconds/1000;
long diffInMinutes = diffInSeconds/60;
long diffInHours = diffInMinutes/60;
long diffInDays = diffInHours/24;
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Author

Commented:
Is this the only way ... or is there any other utility methods, etc. in the Calender class?

Commented:
Yep - you can. THe easiest way is getting the difference in milliseconds and making the days in the same way as above + some casting to int for example.
See this: http://www.exampledepot.com/egs/java.util/CompDates.html

Another way (IF both dates are in the same year - otherwise it becomes a bit complicated but still achievable) is to check which day is every date  in the year http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html#DAY_OF_YEAR

This is also a good article on dates and calculations with them in Java: http://www.javaworld.com/javaworld/jw-03-2001/jw-0330-time.html
The accepted solution has problems and will yield incorrect results in countries that have Day light savings (US, UK, Europe etc)

You  can check out the solution given here for the correct way of doing this:
http://tripoverit.blogspot.com/2007/07/java-calculate-difference-between-two.html
If you want to use the dates in a different format like 01-01-2009 00:00:00.000 you can use timestamp function.

Date today = new Date();

Timestamp ts = new Timestamp( today.getTime() + 604800000 );

604800000 = this number is equals to 7 days in seconds...
//The code is use for finding the difference between  days
//The code is use for finding the difference between  days
	
	
	public static int getDays(GregorianCalendar g1, GregorianCalendar g2) {
	      int elapsed = 0;
	      GregorianCalendar gc1, gc2;
 
	      if (g2.after(g1)) {
	         gc2 = (GregorianCalendar) g2.clone();
	         gc1 = (GregorianCalendar) g1.clone();
	      }
	      else   {
	         gc2 = (GregorianCalendar) g1.clone();
	         gc1 = (GregorianCalendar) g2.clone();
	      }
 
	      gc1.clear(Calendar.MILLISECOND);
	      gc1.clear(Calendar.SECOND);
	      gc1.clear(Calendar.MINUTE);
	      gc1.clear(Calendar.HOUR_OF_DAY);
 
	      gc2.clear(Calendar.MILLISECOND);
	      gc2.clear(Calendar.SECOND);
	      gc2.clear(Calendar.MINUTE);
	      gc2.clear(Calendar.HOUR_OF_DAY);
 
	      while ( gc1.before(gc2) ) {
	         gc1.add(Calendar.DATE, 1);
	         elapsed++;
	      }
	      return elapsed;
	   }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial