?
Solved

Difference in days

Posted on 2008-11-15
7
Medium Priority
?
285 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:kingsofjupiter
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 22968382
First set the start Calendar

cal1.setTime(hi);

then add days until

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

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 22968392
Sorry:

Until hi1.after(cal1.getTime()) == false;
0
 

Author Comment

by:kingsofjupiter
ID: 22968529
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:kingsofjupiter
ID: 22968586
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 22968631
>>the inner while loop i mean

There isn't one - that's the problem
0
 

Author Comment

by:kingsofjupiter
ID: 22970051
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 22970065
Good ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question