Date-time types
For date-time values, use date-time data types to define your column, and use date-time classes in Java. The job of your JDBC driver is to mediate between these types.
You are trying to pass strings rather than date-time objects.
Half-Open logic
In date-time work, use Half-Open approach where the beginning is inclusive while the ending is exclusive. So lunch starts at noon and runs up to, but does not include, the first moment of 1 PM. A week starts at Monday and runs up to, but does include, the following Monday.
SELECT * FROM tbl_
WHERE when_ >= ? -- Pass start moment. Inclusive.
AND when_ < ? -- Pass stop moment. Exclusive.
;
The SQL command BETWEEN is “closed” meaning both the beginning and ending are inclusive; not good for date-time work.
Parse strings to date-time
You need to transform your user-input into date-time objects. You may want to parse a string types by user. Or you may want to use a date-time widget. In your case, parsing strings is apparently needed. Search Stack Overflow for DateTimeFormatter
to find hundreds of existing Questions and Answers.
SQL & JDBC
The Instant
class in Java represents a moment on the timeline in UTC. Equivalent to the legacy java.util.Date
class but with a finer resolution of nanoseconds rather than milliseconds.
Apply a time zone ZoneId
to get a ZonedDateTime
object. Equivalent to the legacy class GregorianCalendar
.
ZonedDateTime zdt = ZonedDateTime.parse( input , … ) ;
myPreparedStatement.setObject( … , zdt.toInstant() ) ;
And…
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime zdt = instant.atZone( ZoneId.of( "America/Montreal" ) ) ;
Tips
Observe naming conventions. In Java, variables start with a lowercase letter.
Avoid naming columns in database with reserved words. Easiest way to entirely avoid all reserved words is to append a trailing underscore to all the names of all your database objects. The SQL standard explicitly promises to never use a trailing underscore.