tl;dr
Use smart objects, not dumb strings.
java.time.LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class )
java.time
FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.
With JDBC 4.2 and later, you can directly exchange java.time objects with your database via getObject
& setObject
methods. Use objects rather than mere strings to communicate with a database.
No zone
If drawing data from a column defined in a type similar to the standard TIMESTAMP WITHOUT TIME ZONE
, use LocalDateTime
class.
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
UTC
If drawing data from a column defined in a type similar to the standard TIMESTAMP WITH TIME ZONE
, use Instant
class or ZonedDateTime
class.
Instant instant = myResultSet.getObject( … , Instant.class ) ;
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.
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.