Using date type in java

I am trying to get two dates from an SQL query and compare them. Therefore, to compare them, I believe that I will need to use the "Date" type. Here is what I am trying, I know that I am getting the date from resultSet incorrectly, but I'm 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 that was my attempt, but I cannot get it to work. Thanks in advance.

EDIT: The request has TO_CHAR (column, "MM-DD-YYYY") for the two dates that I receive.

+4
source share
6 answers

EDIT: Now you mentioned that your request converts the date to a string, stop doing this. You end up recounting it on the caller - so why is there no point in making two conversions? Convert strings to an absolute minimum - stay in the most appropriate data type where possible.


Original answer

You have not shown what result , but you probably want something like ResultSet.getDate() to get the date values.

Please note that your comparison code will not work because there is no > for Date - you need something like:

 if (currentDate.after(validDate)) 

Or select the base number of millis:

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

Additionally:

  • You cannot assign "" to a Date variable - the string is not Date .
  • You can just call ResultSet.getDate() and check if the return value is null and not getObject call first and then getDate()
+4
source

Try currentDate.after (validDate)

+1
source

To compile dates, I always use the methods before and after Date.

+1
source

Some unpleasant things can occur when accessing dates using the getObject method. You should try using the rs.getTimestamp (withinfoinfo) or rs.getDate (no time) methods.

In addition, due to the rather complex hierarchy of Date objects, you should only compare dates using the date1.compareTo(date2) > 0 method.

+1
source

if your result object is a 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... } 
+1
source

There are at least three things in the code:

  • "" is a String literal, so you cannot use it so that your ternary expressions are assigned to a variable of type Date - use null , instead you will not need a triple
  • ResultSet.getObject() returns an Object that does not have a toDate() method. Instead, just use ResultSet.getDate()
  • You cannot compare Date instances with the > operator. You must use the before() and after() methods of the Date class

Taking all this together, the following code may 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 } 

In the if clause, you might need to include additional logic to handle null values. Better to do this than leave it for implicit conversions too.

+1
source

Source: https://habr.com/ru/post/1381142/


All Articles