1. Home
  2. Documentation
  3. Data types
  4. Using Java 8 Date and Time classes
Edit this page on GitHub

Using Java 8 Date and Time classes

The PostgreSQL® JDBC driver implements native support for the Java 8 Date and Time API (JSR-310) using JDBC 4.2.

PostgreSQL®Java SE 8
DATELocalDate
TIME [ WITHOUT TIME ZONE ]LocalTime
TIMESTAMP [ WITHOUT TIME ZONE ]LocalDateTime
TIMESTAMP WITH TIME ZONEOffsetDateTime

This is closely aligned with tables B-4 and B-5 of the JDBC 4.2 specification.

Note

ZonedDateTime , Instant and OffsetTime / TIME WITH TIME ZONE are not supported. Also note that all OffsetDateTime instances will have be in UTC (have offset 0). This is because the backend stores them as UTC.

Example 5.2. Reading Java 8 Date and Time values using JDBC

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
while (rs.next()) {
    System.out.print("Column 1 returned ");
    LocalDate localDate = rs.getObject(1, LocalDate.class);
    System.out.println(localDate);
}
rs.close();
st.close();

For other data types simply pass other classes to #getObject .

Note

The Java data types needs to match the SQL data types in table 7.1.

LocalDate localDate = LocalDate.now();
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, localDate);
st.executeUpdate();
st.close();