How to get a java.time object from a java.sql.Timestamp without a JDBC 4.2 driver?

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:

  • Timestamp::toInstant()
  • Timestamp::toLocalDateTime()
  • Date::toLocalDate()
  • Time::toLocalTime()

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

  • Timestamp.from(instant)
  • Timestamp.valueOf(localDateTime)
  • Date.valueOf(localDate)
  • Time.valueOf(localTime)

Example:

preparedStatement.setTimestamp( 2, Timestamp.from(instant) );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)