32

When retrieving a java.sql.Timestamp from a database via JDBC 4.1 or earlier, how does one obtain/convert to a java.time object?

Neither of the open-source JDBC drivers for Postgres is JDBC 4.2 compliant yet, so I'm looking for a way to use use java.time with JDBC 4.1.

3
  • May I ask why you need to use java.time, that appear to be new 1.8 classes? Commented Mar 18, 2014 at 4:21
  • 6
    Because java.util.Date & .Calendar are notoriously troublesome. I usually use Joda-Time instead. But I've been dabbling with java.time in Java 8. The java.time package is inspired by Joda-Time but is re-architected. Commented Mar 18, 2014 at 4:43
  • UPDATE: While I accepted the Answer by pickypg as correctly addressing the specifics of my Question, the best solution is to use only the java.time classes with JDBC 4.2 while avoiding entirely the troublesome legacy java.sql classes. Exchange java.time objects directly with the database; no need for conversion anymore. See the Answer by skrueger. Commented Jun 3, 2018 at 21:05

2 Answers 2

52

New Methods On Old Classes

By using the driver with Java 8 and later, you should automatically pick up some methods on your java.sql.Timestamp object for free. Both java.sql.Time and java.sql.Date have similar conversion methods.

Namely, to convert from java.sql to java.time you are looking for:

To go the other direction, from java.time to java.sql, use the new static methods:

Example:

preparedStatement.setTimestamp( 2, Timestamp.from(instant) ); 
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the answer and the insight. I was focused on the JDBC driver, and did not think to look at the java.sql.Timestamp class’ new features.
FYI, after getting an Instant as seen in this Answer, you can apply a ZoneOffset to get an OffsetDateTime, or better, apply a ZoneId to get a ZonedDateTime. For more info, see my Answer and my diagram on another Question.
Upcoming release should have support for java 8 time API: jdbc.postgresql.org/documentation/head/8-date-time.html
When using a column of type TIMESTAMP (without time-zone, which I see as equivalent to a LocalDateTime) what time-zone will JDBC use to convert that TIMESTAMP to an instant?
If you're storing timestamps into a database, you should be storing them as UTC. Otherwise, I think that you are right. See this setTimestamp answer for more details.
|
7

No need to convert

Like others have said in comments, PostgreSQL's JDBC driver now supports JDBC 4.2 including Java 8 Time API support. We can exchange java.time objects directly with the database.

https://jdbc.postgresql.org/documentation/head/8-date-time.html

So no need to convert, no need to ever use the java.sql types again. Use only their replacements in the java.time package as shown in this list.

PostgreSQL™ Java SE 8 (java.time) DATE LocalDate TIME [ WITHOUT TIMEZONE ] LocalTime TIMESTAMP [ WITHOUT TIMEZONE ] LocalDateTime TIMESTAMP WITH TIMEZONE OffsetDateTime or Instant 

This can be retrieved via ResultSet::getObject

ResultSet rs = ...; while (rs.next()) { LocalDate localDate = rs.getObject(1, LocalDate.class)); } 

Store a java.time object by calling PreparedStatement::setObject.

myPreparedStatement.setObject( … , myInstant ) ; 

1 Comment

This Answer is a very important update. Sticking with the java.time classes only is indeed the way to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.