How to convert PostgreSQL timestamp with time zone to Java Instant or OffSetDateTime?
PostgreSQL timestamp with time zone format: 2020-06-18 16:15:38+05:30
Getting the following exception in Java 11.7 - Ubuntu for 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)
but it works in Java 13.
Any help to make it work in Java 11
ResultSet.getObject(..., OffsetDateTime.class)OffsetDateTimebyOffsetDateTime odt = OffsetDateTime.parse("2020-06-18 16:15:38+05:30", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssxxx"));.timestamp with time zone. Probably the easiest way to retrieve that is to useselect lower(..), upper(...)to get the two edges of the range as two separate columns. Then usegetObject(..., OffsetDateTime.class)on each of them.