Comparing 2 Dates!!!

hi guys,

i have to compare 2 dates, out of which one is retrieved from the database and the other one is the current system time.

i have to compare the 2 dates..if the date retrieved from the DB is equal to the system date, it should return

"already logged in"

else

it should accept the values like ID,date and time, and enter in the database..

both the dates compared are sql dates..

heres a part of my code..

pst2 = con.prepareStatement("SELECT logintime,day FROM login where empid=? order by day asc");

pst2.setString(1,eid);

rs2 = pst2.executeQuery();

while(rs2.next())

{

logtime=rs2.getTime("logintime");

day=rs2.getDate("day");

}

System.out.println(day);

if(day.compareTo(datt)>0)

{

pst1=con.prepareStatement("insert into login(empid,logintime,day) values(?,?,?)");

pst1.setString(1,eid);

pst1.setString(2,time);

pst1.setDate(3,datt);

int rowsInserted=pst1.executeUpdate();

}

else

{

System.out.println("u have already logged in");

}

please kindly reply to my query..

thanks in advance..

[1182 byte] By [sagardeva] at [2007-9-24]
# 1

Tell us the error!

It's my code. It could be useful?

<%GregorianCalendar calendar = new GregorianCalendar();

Calendar dateDB = Calendar.getInstance(); //current date

dateDB.setTime(new Date(element1.getDateinsert().getTime())); //element1 is the row of DB which has a column DateInsert

// diff in milliseconds

long diffMillis = calendar.getTimeInMillis()

- dateDB.getTimeInMillis();

// diff in days

long diffDays = diffMillis / (24 * 60 * 60 * 1000);

// after 3 days it's late

if (diffDays > 3) {

%>

...

Ema

billinga at 2007-7-15 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
Maybe the error is in the update?!I used executeQuery(), not executeUpdate().Ema
billinga at 2007-7-15 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

thanks!!

theres no particular error as such..but..the only thing is that..

what ever condition i give..

it either always accepts the inputs and stores in the database..or either reply already logged in..

if it accepts the inputs..those inputs are irrelavent..i.e., it accepts the date which is already there.

sagardeva at 2007-7-15 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4

//from your code post

logtime=rs2.getTime("logintime");

day=rs2.getDate("day");

if(day.compareTo(datt)>0)

one is a Date object and the other is a Time object and you expect them to be the same value ?

cheers,

ram.

Madathil_Prasada at 2007-7-15 > top of java,Enterprise & Remote Computing,Web Tier APIs...