I want to save a timestamp value into PostgreSQL database. Corresponding column is of the type TIMESTAMP WITHOUT TIME ZONE.
As input Java application gets epoch time (long value), that could be converted into Instant or OffsetDateTime with ZoneOffset.UTC.
What is the best approach? Are there any drawbacks with any of this method?
As per the documentation, Instant is not supported. But you shouldn't have issues with OffsetDateTime in UTC.
The PostgreSQL JDBC documentation mentions that a corresponding type for TIMESTAMP [ WITHOUT TIMEZONE ] is LocalDateTime, but OffsetDateTime in UTC is also supported. On the other hand, Instant is not supported.
See the quote below:
+--------------------------------+----------------+ | PostgreSQL™ | Java SE 8 | +--------------------------------+----------------+ | DATE | LocalDate | | TIME [ WITHOUT TIMEZONE ] | LocalTime | | TIMESTAMP [ WITHOUT TIMEZONE ] | LocalDateTime | | TIMESTAMP WITH TIMEZONE | OffsetDateTime | +--------------------------------+----------------+This is closely aligned with tables B-4 and B-5 of the JDBC 4.2 specification. Note that
ZonedDateTime,InstantandOffsetTime/TIME [ WITHOUT TIMEZONE ]are not supported. Also note that allOffsetDateTimewill instances will have be in UTC (have offset 0). This is because the backend stores them as UTC.
The JDBC 4.2 specification doesn't seem to support Instant.
Also see the following quote from the OffsetDateTime class documentation (highlight is mine):
OffsetDateTime,ZonedDateTimeandInstantall store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant.OffsetDateTimeadds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained.ZonedDateTimeadds full time-zone rules.It is intended that
ZonedDateTimeorInstantis used to model data in simpler applications. This class may be used when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With