Link to home
Start Free TrialLog in
Avatar of kingsofjupiter
kingsofjupiter

asked on

Difference in days

I have two Date parameters from DB2 database, as Startdate and Enddate, In query string i am calling them as,

java.sql.Date hi = rs.getDate("STARTDATE");
java.sql.Date hi1 = rs.getDate("ENDDATE");

How do i find the difference in number of days in integer between these two dates?

I have tried out many ways, but none seems to work, the milliseconds thing isnt working as well. Only the gregorian calendar thing worked under given date, but then how do i add Startdate and Enddate to a Gregoriancalendar?

Calendar then = new GregorianCalendar(2008, Calendar.OCTOBER, 27);
Calendar now = new GregorianCalendar();

Like say for how do i add the date there instead of the predefined date?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kingsofjupiter
kingsofjupiter

ASKER

Just like many other method this also does not shows any error but it is not showing the output, I am putting up the code here, Any idea where the thing is going wrong?



query = "SELECT STARTDATE, ENDDATE, WARDAMOUNT, TOTALWARDAMNT FROM PBS.WARD_MASTER";  
	    psmt=cn.prepareStatement(query);
	    rs=psmt.executeQuery();
	    while (rs.next())
	    {
	     java.sql.Date hi = rs.getDate("STARTDATE");
	     java.sql.Date hi1 = rs.getDate("ENDDATE");
 
	     
	     int diff = 0;
	     //Calendar then = Calendar.getInstance();    
	     Calendar then = new GregorianCalendar();
	     then.setTime(hi);
	     boolean b = hi1.after(then.getTime());
	     if(b)
	     {
	       then.add(Calendar.DATE,1);
	       diff++;
	     }
 
	     out.println(diff);
 
 
	    
	    
	    
	    }
	     
	   rs.close();
	    psmt.close();
	    
		} catch(Exception e) { System.out.println(e); }

Open in new window

It seems the whole while loop is executing only once, the inner while loop i mean, I am trying out some other options using the same code, also it seems if the database has date value as NULL which it will have incase of unoccupied Wards, the query loop is exiting straightaway after finding a single null.
>>the inner while loop i mean

There isn't one - that's the problem
Thanks a lot, I fixed the problem, output now is coming perfectly, and when it is null, it is not even showing the output for the record corresponding the null value, so thats good as well.

Thanks a lot.
Good ;-)