The Java 8 Date & Time API provides the following new classes which in contrast to Date
and Calendar
are immutable and thread-safe:
The ones in bold are pretty safe and easy to use with Hibernate and PostgreSQL. You should primary use them if you don't know where to start. Otherwise you might need to create your own convertors or rely on Hibernate’s conversion features which are proprietary since PostgreSQL doesn’t store Instant
, ZonedDateTime
and OffsetTime
.
In regard to timezone it’s safer if you use the just use the Offset*
versions of the bold classes.
Here's the conversion table for Java/SQL:
Java | SQL |
---|---|
LocalDate | DATE |
LocalTime | TIME |
LocalDateTime | TIMESTAMP |
OffsetTime | TIME_WITH_TIMEZONE |
OffsetDateTime | TIMESTAMP_WITH_TIMEZONE |
Duration | BIGINT |
Instant | TIMESTAMP |
ZonedDateTime | TIMESTAMP |