tl;dr
get the date in between single quotes?
Don’t. You should be using smart objects, not dumb strings, to exchange date values with your database.
Use java.time classes that replaced troublesome legacy classes.
For date-only values, use LocalDate
class that replaced java.sql.Date
.
myPreparedStatement.setObject(
… ,
LocalDate.of( 2018 , Month.JANUARY , 23 )
)
…and retrieval…
myResultSet.getObject(
… ,
LocalDate.class
)
java.time
The Answer by Tim Biegeleisen is correct, but uses troublesome old classes. The java.util.Date
, java.sql.Date
, SimpleDateFormat
, and related old date-time classes were supplanted years ago by the java.time classes.
As of JDBC 4.2 and later, we can directly exchange java.time objects with the database. Use PreparedStatement::setObject
and ResultSet::getObject
methods.
Date-only
If your database column is of a date-only type without time-of-day and without time zone, akin to the SQL-standard DATE
type, use LocalDate
class. Example code:
LocalDate localDate = LocalDate.of( 2018 , Month.JANUARY , 23 ) ; // 2018-01-23.
String sql = "SELECT * FROM t02 WHERE t02_create_date = ? ;" ; // Tip: Make a habit of closing your SQL properly with a semicolon. Won't hurt, and might help.
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , localDate );
ResultSet rs = ps.executeQuery();
while (rs.next()) {
LocalDate ld = rs.getObject( 1 , LocalDate.class ) ; // Retrieve an `LocalDate` class.
}
Date-time
If your database column is of a date-with-time-of-day type, akin to the SQL-standard TIMESTAMP WITH TIME ZONE
type, use Instant
class. Example code:
LocalDate instant = LocalDate.of(
String sql = "SELECT * FROM t02 WHERE t02_create_date = ? ;" ; // Tip: Make a habit of closing your SQL properly with a semicolon. Won't hurt, and might help.
PreparedStatement ps = conn.prepareStatement( sql ) ;
ps.setObject( 1 , instant );
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Instant instantRetrieved = rs.getObject( 1 , Instant.class ) ; // Retrieve an `Instant` class.
// You may want to adjust from UTC to some other time zone.
ZoneID z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instantRetrieved.atZone( z ) ; // Same moment, same point on the timeline, different wall-clock time as seen by the people of some particular region (a time zone).
}
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.