Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 594
Basil Bourque Avatar asked Mar 18 '14 04:03

Basil Bourque


People also ask

How do I cast Java sql Timestamp to Java Lang string?

The toString() method of the java. sql. Timestamp class returns the JDBC escape format of the time stamp of the current Timestamp object as String variable. i.e. using this method you can convert a Timestamp object to a String.

How do I do a Timestamp in sql?

The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.

How do I get the current time in Java 8?

You can get the time from the LocaldateTime object using the toLocalTime() method. Therefore, another way to get the current time is to retrieve the current LocaldateTime object using the of() method of the same class. From this object get the time using the toLocalTime() method.


2 Answers

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) ); 
like image 143
pickypg Avatar answered Sep 20 '22 14:09

pickypg


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 ) ;  
like image 29
skrueger Avatar answered Sep 22 '22 14:09

skrueger