How to get UTC timestamps from JDBC+postgreSql timestamp?

前端 未结 3 1422
别那么骄傲
别那么骄傲 2021-02-02 14:06

I created a table like like this in PostgreSQL:

create table myTable (
    dateAdded timestamp(0) without time zone null          


        
3条回答
  •  清酒与你
    2021-02-02 14:55

    There are a few tricks specific for the Postgres JDBC driver

    See https://jdbc.postgresql.org/documentation/head/java8-date-time.html

    So when reading you can do

        Instant utc =resultSet.getObject("dateAdded",LocalDateTime.class).toInstant(ZoneOffset.UTC);
    

    If you use a connection pool such as Hikari, you can also specify the time time-zone used by each connection by setting connectionInitSql=set time zone 'UTC'

提交回复
热议问题