Preserve timezone in PostgreSQL timestamptz type

后端 未结 3 2046
眼角桃花
眼角桃花 2020-12-28 18:22

For an ISO8601 compliant datetime

2004-10-19 10:23:54+02

Is it possible to have that value, with +02 offset, reflected in the

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-28 19:19

    Java developers can use Joda Time combined with Jadira UserType's PersistentDateTimeAndZone. Example:

    @Basic(optional = false)
    @Columns(columns = { @Column(name = "modificationtime"),
            @Column(name = "modificationtime_zone") })
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTimeAndZone")
    @Index(name = "payment_modificationtime_idx")
    private DateTime modificationTime = null;
    

    In this example, the DateTime information is in 2 columns:

    1. modificationtime timestamp without time zone to store the timestamp in UTC time zone
    2. modificationtime_zone varchar(255) to store the time zone ID as string (e.g. America/Caracas)

    While Joda Time and Jadira (and Hibernate) is specific to Java (and is the de facto approach), the above approach of structuring the RDBMS columns to store both timestamp and time zone can be applied to any programming language.

提交回复
热议问题