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?
kingsofjupiterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
First set the start Calendar

cal1.setTime(hi);

then add days until

hi1.after(cal1.getTime());
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Sorry:

Until hi1.after(cal1.getTime()) == false;
0
kingsofjupiterAuthor Commented:
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

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

kingsofjupiterAuthor Commented:
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.
0
CEHJCommented:
>>the inner while loop i mean

There isn't one - that's the problem
0
kingsofjupiterAuthor Commented:
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.
0
CEHJCommented:
Good ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.