Avatar of _Esam
_Esam
 asked on

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

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
Java

Avatar of undefined
Last Comment
tzxie2000

8/22/2022 - Mon
NHBFighter

Date start=....;
Date end=....;
long diffInMilleseconds = end.getTime() - start.getTime();
long diffInSeconds = diffInMilleseconds/1000;
long diffInMinutes = diffInSeconds/60;
_Esam

ASKER
I need to find the difference between days ? How many days?
ASKER CERTIFIED SOLUTION
NHBFighter

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
_Esam

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Venabili

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
ryanfernandes

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
PLANET_LTD

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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
tzxie2000

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