Home arrow static arrow Java Programming [Archive] - sql date actions
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - sql date actions
This topic has 2 replies on 1 page.

Posts:247
Registered: 07.12.01
sql date actions  
Jul 14, 2004 1:07 AM



 
I have two java.sql.Dates. i need two substract one of the dates from the other and see if the difference is more then 8 years and 1 day. How can i do this?

I tried to use the getTime() methods and substract these two but the new calculated method is wrong.

 

Posts:51
Registered: 5/10/02
Re: sql date actions  
Jul 14, 2004 1:13 AM (reply 1 of 2)



 
I tried to use the getTime() methods and substract
these two but the new calculated method is wrong.

I think this is the correct way, if you of course after all transform the result of subtraction in a way you can compare with 8 years and 1 day...
 

Posts:31,095
Registered: 4/30/99
Re: sql date actions  
Jul 14, 2004 8:58 AM (reply 2 of 2)



 
You can't use getTime() and subtract the two because you don't know how many milliseconds there are in 8 years and a day. (The number varies depending on how many of those 8 years are leap years.) Use Calendar objects like this:
Calendar first = Calendar.getInstance();first.setTime(firstSqlDate);first.add(Calendar.YEAR, 8);first.add(Calendar.DATE, 1);Calendar second = Calendar.getInstance();second.setTime(secondSqlDate);if (first.before(second)) ...
Or
if (!first.after(second)) ...
depending on which comparison you need.

PC²
 
This topic has 2 replies on 1 page.