tl;dr
JDBC 4.2 and later:
Boolean isFutureTask = myResultSet.getObject( "myDateColumn" , Instant.class )
.isAfter( Instant.now() ) ;
Older JDBC:
Boolean isFutureTask = myResultSet.getTimestamp( … )
.toInstant()
.isAfter( Instant.now() ) ;
java.time
The other answers were correct but use outmoded classes. The java.util.Date
/.Calendar
classes have been supplanted by the java.time framework built into Java 8 and later.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
Date-time types
You should be storing your date-time values as a date-time type in your database. I believe that would be TIMESTAMP
type in MySQL (but I'm a Postgres man myself).
Then use the java.sql.Timestamp
type to move data in/out of the database.
java.sql.Timestamp ts = myResultSet.getTimestamp( 1 );
Immediately convert from java.sql to java.time. The java.sql classes are part of those old date-time classes that have proven to be poorly designed, confusing, and troublesome. Use them only until JDBC drivers are updated to directly deal with java.time types.
The old java.util.Timestamp
has new methods for converting to/from an Instant
. An Instant
is a moment on the timeline in UTC with a resolution of nanoseconds.
java.time.Instant instant = ts.toInstant();
Get the current moment.
Instant now = Instant.now();
Compare.
Boolean isStoredDateTimePast = now.isAfter( instant );
In JDBC 4.2 and later, your JDBC driver may be able to directly access the value from the database as an Instant
object via the ResultSet::getObject
method.
If you must parse that string, convert it to standard ISO 8601 format. Replace the space in the middle with a T
. Assuming the string represents an offset-from-UTC of zero, append Z
for Zulu
which means UTC. The java.time classes parse/generate strings by default in ISO 8601 formats.
String inputOriginal = "2011-12-30 17:10:00".
String input = inputOriginal.replace( " " , "T" ) + "Z" ;
Instant instant = Instant.parse( input );