Use objects rather than strings when communicating with your database. For a date-time value, use a date-time object.
java.time
Avoid the troublesome old date-time classes including Calendar and GregorianCalendar. They are poorly-designed, flawed, and confusing. Among their troubles is toString methods that dynamically apply the JVM’s current default time zone when generating a string, meant to be helpful but turns out to be an anti-feature.
Instead, use the modern java.time classes.
If your JDBC driver is updated for JDBC 4.2 and later, use the java.time types directly rather than the java.sql types.
Alter your input string to comply with ISO 8601 format. Change that SPACE in middle to T.
String input = "2008-10-29 14:56:59".replace( " " , "T" ) ;
Parse as a LocalDateTime since your input lacks any indication of time zone or offset-from-UTC.
LocalDateTime ldt = LocalDateTime.parse( input ) ;
Or specify the date-time value as integers.
LocalDateTime ldt = LocalDateTime.of( 2013 , 12 , 1 , 0 , 0 , 0 , 0 ) ;
If your database column is of type TIMESTAMP WITHOUT TIME ZONE, you are ready to query. Be sure you understand that without the context of a time zone or offset-from-UTC, such values do not represent a specific moment, and are not a point on the timeline, rather they are vague ideas about possible moments.
String sql = "SELECT * FROM tbl_ WHERE when_ >= ? ; " ;
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , ldt ) ; // Call `setObject` to pass a java.time object directly without converting into `java.sql.*` type.
ResultSet resultSet = ps.executeQuery() ;
…
LocalDateTime ldt = resultSet.getObject( … , LocalDateTime.class ) ;
If you had intended to track actual moments, specific points on the timeline, then you would be using TIMESTAMP WITH TIME ZONE as your database column type, and using Instant and ZonedDateTime java.time classes.
SimpleDateFormat?"yyyy-MM-dd HH:mm:ss"do the trick?java.sql.Timestampwith JDBC directly.