如何将带时区的 PostgreSQL 时间戳转换为 Java Instant 或 OffSetDateTime?

How to convert PostgreSQL timestamp with time zone to Java Instant or OffSetDateTime?

如何将带时区的 PostgreSQL 时间戳转换为 Java Instant 或 OffSetDateTime?

带时区格式的 PostgreSQL 时间戳:2020-06-18 16:15:38+05:30

在 Java 11.7 - Ubuntu 中为 Instant.parse("2020-06-18 16:15:38+05:30".replace(" ", "T"))

获取以下异常
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-06-18T16:15:38+05:30' could not be parsed at index 19
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.Instant.parse(Instant.java:395)
at OffSetDateTimeExample.main(OffSetDateTimeExample.java:9)

但它适用于 Java 13.

任何帮助使其在 Java 11

中工作

拆分范围类型值

tstzrange 是 Postgres 中的 range type

通过调用 lower upper 函数在查询中拆分 PostgreSQL tstzrange

select 
    *, 
    lower(tstzrange) as lower_tstzrange, 
    upper(tstzrange) as upper_tstzrange 
from announcement
;

并在 Resultset 中用作 OffsetDateTime

TstzRange.builder()
    .startDateTime(rs.getObject("lower_tstzrange", OffsetDateTime.class))
    .endDateTime(rs.getObject("upper_tstzrange", OffsetDateTime.class))
            .build()

感谢 a_horse_with_no_name and Arvind Kumar Avinash 节省了我的时间并学习了拆分范围数据类型。