在 AWS 服务器上部署的应用程序选择世界时间而不是本地时间

Deployed application picking Universal time Instead of Local time on AWS Server

目前,我的 AWS 实例显示时间低于

timedatectl status
            **Local time:** Thu 2019-12-26 19:41:51 IST
            Universal time: Thu 2019-12-26 14:11:51 UTC
            RTC time: Thu 2019-12-26 14:11:51
            Time zone: Asia/Kolkata (IST, +0530)
       System clock synchronized: yes
       systemd-timesyncd.service active: yes
       RTC in local TZ: no

波乔

@Entity
@Table(name = "hotel_booking")
public class Booking implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @Id
    private Integer version;

    @NotNull
    @Column(name = "ota_reference_no", nullable = false)
    private String referenceNo;

    @Column(name = "guest_name")
    private String guestName;

    @Column(name = "created_date", columnDefinition = "timestamp", nullable = false)
    private LocalDateTime createdDate;

    @Column(name = "modified_date", columnDefinition = "timestamp")
    private LocalDateTime modifiedDate;

======

一旦坚持 createdDate 提交选择世界时间但要求是,当地时间

在本地主机中得到相同的预期结果:

数据库使用:Postgres(即本地安装在AWS实例上)

如果这是(或将成为)真实的应用程序,请帮自己一个忙,并坚持时间轴上的任何实际点(或真实时刻)始终 UTC — 实际上,您可以使用 Instant 类型在您的应用程序中直接获得此信息。在线下,您可以使用 ZonedDateTime(或 LocalDateTime,但这个很棘手)在将值公开给用户代理之前相应地 "translate" 值 - 或用于任何其他操作( s) 你可能会这样做,可能对时区敏感。

By the way, the reason you are getting the Universal time instead of Local time it is because the Java Virtual Machine, the application, the framework you are using for persistence (or a combination) is/are set in such way, either by default, or because somewhere in the application there is a setting like this: -Duser.timezone=UTC. If it's the application, it might be ZoneOffset.UTC or TimeZone.setDefault(...), but these are unlikely as you would need to do this programmatically. Execute a System.out.println(ZoneId.systemDefault()), it should yield UTC (or something around those lines).