4

I am trying to get two dates from a SQL query, and compare them. So to compare them, I believe I will need to use the "Date" type. Here is what I am trying, I know I am getting the date from the resultSet incorrectly, but I am not sure how to do it.

Date validDate = new Date(0);
Date currentDate = new Date(0);

// query

    if (result.next()) {

    validDate  = (result.getObject("validDate")!=null)?result.getObject("validDate").toDate():"";
    currentDate = (result.getObject("currentDate")!=null)?result.getObject("currentDate").toDate():"";
}

if (currentDate > validDate) {
    //do something
}

So again, this was my attempt, but I cant seem to get it to run. Thanks in advance.

EDIT: the query has TO_CHAR(column, 'MM-DD-YYYY') on the two dates that I am getting.

Mr Man
  • 1,498
  • 10
  • 33
  • 54
  • 1
    Can you show us where you get "result"? Also, maybe an example of what a result.getObject("validDate") looks like. – Mike Lentini Nov 14 '11 at 15:30
  • The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API. Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. – Arvind Kumar Avinash Jul 09 '21 at 15:50

6 Answers6

4

EDIT: Now you've mentioned that your query converts the date to a string, stop doing that. You'll end up reparsing it on the calling side - so why perform two conversions pointlessly? Keep string conversions to the absolute minimum - stay in the most appropriate data type wherever possible.


Original answer

You haven't shown what result is, but you probably want something like ResultSet.getDate() to fetch the date values.

Note that your comparison code won't work either because there's no > for Date - you'd need something like:

if (currentDate.after(validDate))

Or fetch the underlying number of millis:

if (currentDate.getTime() > validDate.getTime())

Additionally:

  • You can't assign "" to a Date variable - a string isn't a Date.
  • You can just call ResultSet.getDate() and check whether the returned value is null, rather than calling getObject first and then getDate()
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Try currentDate.after(validDate)

Marc Van Daele
  • 2,856
  • 1
  • 26
  • 52
1

To compdare dates I always use the before and after methodes of Date.

Klaasvaak
  • 5,634
  • 15
  • 45
  • 68
1

Some nasty things can happen when accessing dates via the getObject method. You should try to use the rs.getTimestamp (with timeinfo) or the rs.getDate (without timeinfo) methods.

Also, because of the rather complex hierarchy of Date-objects you should compare Dates only using the date1.compareTo(date2) > 0 method.

Jonathan
  • 2,698
  • 24
  • 37
1

if your result object is ResultSet, then

Date validDate = result.getTimestamp("validDate"); 
Date currentDate= result.getTimestamp("currentDate"); 
// you can add null checks here too....
// you can also use if (currentDate.getTime() > validDate.getTime()){}
if (currentDate.before(validDate)) { 
 //some code inhere...
}
Daniel
  • 36,833
  • 10
  • 119
  • 200
1

There are at least three things wrong with your code:

  • "" is a String literal, so you cannot use it int your ternary expressions to be assigned to a variable of type Date - use null instead so you don't need a ternary
  • ResultSet.getObject() returns an Object, which does not have a toDate() method. Instead, simply use ResultSet.getDate()
  • You cannot compare Date instances using a > operator. You have to use the before() and after() methods of the Date class

Taking all this together, the following code might work:

Date validDate = new Date(0);
Date currentDate = new Date(0);

if (result.next()) {
    validDate  = result.getDate("validDate");
    currentDate = result.getDate("currentDate");
}
if (currentDate.after(validDate)) {
    //do something
}

The if clause may have to include some extra logic to deal with null values though. It's better to do that than to leave that to implicit conversions, too.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720