tl;dr
myPreparedStatement.setObject( // Insert object into database for `TIMESTAMP WITHOUT TIME ZONE` type of column.
… ,
LocalDate.parse( "2015-02-05" ) // Parse input string to a date-only value lacking time-of-day and lacking time zone.
.atStartOfDay() // Determine start of a generic 24-hour day for a `LocalDateTime` object lacking offset-from-UTC and lacking time zone.
) ;
java.time
The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.
Parse your input string
LocalDate ld = LocalDate.parse( "2015-02-05" ) ;
Unzoned
If your business problem lacks any offset-from-UTC or time zone, is not a specific moment on the timeline, then get the first moment of a generic 24-hour day by converting to a LocalDateTime
.
LocalDateTime ldt = ld.atStartOfDay() ;
ldt.toString(): 2015-02-05T00:00
To store this in database in a column of data type similar to the SQL standard type of TIMESTAMP WITHOUT TIME ZONE
, use the java.time objects with a JDBC driver compliant with JDBC 4.2 and later. Better to exchange objects with your database rather than mere strings.
myPreparedStatement.setObject( … , ldt ) ;
…and…
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
Zoned
If your business problem does intend an offset or zone, you must specify to get an actual point on the timeline.
The first moment of the day is not always 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day may begin at another time such as 01:00:00. Let java.time determine the first moment of the day with a call to LocalDate::atStartOfDay
.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;
zdt.toString(): 2015-02-05T00:00+01:00[Africa/Tunis]
Exchange java.time objects with your database as seen above. If you are trying to record actual moments in history, your database column should be of a type equivalent to the SQL standard TIMESTAMP WITH TIME ZONE
.
myPreparedStatement.setObject( … , zdt ) ;
…and…
LocalDateTime ldt = myResultSet.getObject( … , ZonedDateTime.class ) ;
Your driver and/or database likely converts such zoned values to a UTC value. Or you may do so explicitly yourself. Extract an Instant
object which is always in UTC.
myPreparedStatement.setObject( … , zdt.toInstant() ) ;
…and…
Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
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.