postgresql - why doesn't timestamptz work in postgres? - Stack Overflow

I am keeping date_created in TIMESTAMPTZdate_createdTIMESTAMPTZDEFAULT CURRENT_TIMESTAMP N

I am keeping date_created in TIMESTAMPTZ

date_created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,

in java

@CreatedDate
@Column(nullable = false, updatable = false)
private OffsetDateTime dateCreated;

Also in main application

public class Application {

    public static void main(final String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow"));
        log.info("Default time-zone: {}", TimeZone.getDefault());
        SpringApplication.run(Application.class, args);
    }

}

But when I get user.getDateCreated() I get it in UTC+0 format. How should I control my Spring application to automatically receive DateCreated in UTC+3?

I am keeping date_created in TIMESTAMPTZ

date_created TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,

in java

@CreatedDate
@Column(nullable = false, updatable = false)
private OffsetDateTime dateCreated;

Also in main application

public class Application {

    public static void main(final String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Moscow"));
        log.info("Default time-zone: {}", TimeZone.getDefault());
        SpringApplication.run(Application.class, args);
    }

}

But when I get user.getDateCreated() I get it in UTC+0 format. How should I control my Spring application to automatically receive DateCreated in UTC+3?

Share Improve this question edited Feb 11 at 13:39 Laurenz Albe 250k21 gold badges298 silver badges373 bronze badges asked Feb 11 at 11:12 ЛейлаЛейла 112 bronze badges 1
  • 2 The title of your question is a bit silly. It is working fine, so asking why it doesn't work doesn't make a lot of sense. Be open to the possibility that the problem is behind the keyboard. – Laurenz Albe Commented Feb 11 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 0

For that, you need to set the session time zone to UTC-03. Execute this SQL statement after connecting to the database:

SET timezone = 'UTC-03'

There seems to be no connection option to set the parameter during login.

Okay, the problem was in hibernate, not in postgres. In Hibernate 6 hibernate.timezone.default_storage now defaults to DEFAULT, meaning:

if the database/dialect supports it, time zones of date/time values are stored by using the timestamp with time zone SQL column type;

otherwise, time zones of date/time values are not stored, and date/time values are normalized to UTC.

In Hibernate ORM 5, time zones were not stored, but normalized to the time zone set in hibernate.jdbc.time_zone, the JVM time zone by default.

To revert to Hibernate ORM 5’s behavior, set the configuration property hibernate.timezone.default_storage to NORMALIZE.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745215804a4617013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信