2

I am pulling a smalldatetime from SQL Server database into a java class (2014-03-11 11:49:00) and would like to do a comparison of this date-1 and today's date in java. The only problem is when pulling the date from the database it is assigned to a String variable, therefore I need some way of converting the String to a smalldatetime-1day in the java code. Also, I need a way to find (todays date) in the same format.

I have looked around for the last hour with little success.

if((today's date) >= (smalldatetime -1day)) { //do something }

Thanks!

scot0308
  • 75
  • 1
  • 9
  • 1
    You need to convert this string to **any** Date type that supports date arithmetic. If you do not plan to write the value back to the database you shouldn't even care about its SQL data type. – PM 77-1 Mar 11 '14 at 18:12
  • possible duplicate of [String to Date in Different Format in Java](http://stackoverflow.com/questions/882420/string-to-date-in-different-format-in-java) – Garis M Suero Mar 11 '14 at 18:14

1 Answers1

2

I can only assume you are using the ResultSet class to obtain the values from the database. If so the ResultSet class has a method called getTimestamp which returns the value as a java.sql.Timestamp object.

Assuming you have a ResultSet you can write you code like this:

java.sql.ResultSet rs = // assuming you already have this;
java.sql.Timestamp time = rs.getTimestamp("columnName");
if( System.currentTimeMillis() >= time.getTime() - ( 1000 * 60 * 60 * 24 ) ) {

}

Read the ResultSet javadoc for more details.

3urdoch
  • 7,192
  • 8
  • 42
  • 58