scot0308
scot0308

Reputation: 75

Converting returned java string into sql smalldatetime

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!

Upvotes: 2

Views: 2263

Answers (1)

3urdoch
3urdoch

Reputation: 7332

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.

Upvotes: 2

Related Questions