Compare date from recordset
Hi.
Im trying to compare a date from oracle to see if it's more than 24 hours old or not. If I get the date with
rs.getDate(meta.getColumnName(4));
My result is : 2007-05-23
If i use
rs.getString(meta.getColumnName(4));
My result is: 2007-05-23 10:21:12.0
How can i try
if(rs.getDate(meta.getColumnName(4)) is lesser than 24 hours)
You know what i meen :)
Sincerly
Rothsten
[447 byte] By [
Rothstena] at [2007-11-27 5:13:19]

# 1
ResultSet#getDate returns java.sql.Date which contains year, month and day only.
ResultSet#getTime returns java.sql.Time which contains hours, minutes and seconds.
ResultSet#getTimestamp returns java.sql.Timestamp which contains year, month, day, hours, minutes and seconds.
Likely you need the getTimestamp().
After all, you can also just retrieve the row by getTimestamp(4) instead of getTimestamp(meta.getColumnName(4)). This saves one call.
# 2
Well here is a simple code snippet which might help you....
long timedifference = rs.getDate(column_name2).getTime() - rs.getDate(column_name1).getTime();
// day = 24 hrs , 1hr = 3600 sec,1 sec = 1000 milli sec
long millisec_per_day = 24*3600*1000;
if( timedifference > millisec_per_day)
System.out.println("More than a day");
else if( timedifference < millisec_per_day)
System.out.println("Less than a day");
else if( timedifference == millisec_per_day)
System.out.println("Exactly a day");
NOTE: The following snippet may not be a perfect solution when it comes to implementation part (as many ppl do it with Calendar and others).
Hope this might help :)
REGARDS,
RaHuL