Link to home
Start Free TrialLog in
Avatar of jkavx
jkavx

asked on

Java compare current date to db value

I need a routine to determine if the current date is more than 1 day later than a createDate value in a database.  So given the two dates, how to determine this:

   Date createDate = // get createDate from database
   Date curDate = new Date();
  // is curDate more than 1 day after createDate
Avatar of for_yan
for_yan
Flag of United States of America image

java.sql.Date dd = //from datbase

java.util.Dtae  d = new java.util.Date();

if(d.getTime() - dd.getTime() > 3600L*24L*1000L) {
//do somthing
}else
{
//do somthing else
}
in general it is probably betetr to use it in the slect statemnet

select * from table where sysdate - date_field > 1

that would be in Oracle

In other databses ssysdate will be similar but different sysntax
One more way with Calendar
object

java.sql.Date dd = //from datbase

java.util.Date  d = new java.util.Date();


Calendar c = Calendar.getInstance();
c.setTime(dd);

Calendar c1 = Calendar.getInstance();
c1.setTime(d);

c.add(Calendar.DATE, 1);

java.util.Date d2 = c.getTime();
if(dd.after(d2)){
// do somthing
}
else
{
//do something
}





Avatar of jkavx
jkavx

ASKER

These are great, thx.  It seems now like the business rule may be changing, so the test might be whether the currentDate is the next calendar day from the createdDate or more.  So if the createdDate is 9/7 and the currentDate is 9/8 then the condition is met.  How would you do that?
I think something like taht should work.

Let me check it

GregorianCalendar gc1 = new GregorianCalendar(dd.getYear(), dd.getMonth(); dd.getDate());

GregorianCalendar gc2 = new GregorianCalendar(d.getYear(), d.getMonth(); d.getDate());

gc1.add(Calendar.DATE,1);

if(g1.equals(gc2)) {


} else

{

}






ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America 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