在我的PostgreSQL上,如果我运行该脚本:

show timezone;

它又回来了

Europe/Rome

所以根据这一点,我配置了

spring.jpa.properties.hibernate.jdbc.time_zone: Europe/Rome

我有一个表TestPostgre,它的列datetime_using_instant的数据类型为timestamp.

@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Entity
@Table(name = "test_postgre")
public class TestPostgre {

    @Id
    @Column(name = "pk", nullable = false)
    private String pk;
    
    @Column(name = "datetime_using_instant", nullable = false)
    private Instant dateTimeUsingInstant;
    
}

使用JpaRepository:

@Repository
public interface TestPostgreRepository extends JpaRepository<TestPostgre, String> {
    
}

当使用DateTimeUsing Instant值2023-10-11T12:30:00Z保存TestPostgre实例时,在PostgreSQL上,该值仍然是2023-10-11 12:30:00.000,但协调世界时和欧洲/罗马之间有2小时的偏移.

推荐答案

PostgreSQL Time Stamps

在一个已确定为timestamp without time zone,PostgreSQL will silently ignore any time zone indication的字面上.也就是说,resulting value是从输入值中的日期/时间字段导出的,而is not adjusted for time zone.

关于时间戳(没有时区的时间戳)和时间戳(有时区的时间戳)之间的区别的很好的教程可以在postgresqltutorial找到

Java相关问答推荐

我应该避免在Android中创建类并在运行时编译它们吗?

如果给定层次 struct 级别,如何从其预序穿越构造n元树

如何在Android上获取来电信息

Android Studio—java—在onBindViewHolder中,将断点和空白添加到BackclerView中

AlarmManager没有在正确的时间发送alert

Java List with all combinations of 8 booleans

springboot start loge change

无法在WebSocket onMessage中捕获错误

FALSE:它应该在什么时候使用?

Spring和可编辑";where";@Query

在Java 17中使用两个十进制数字分析时间时出错,但在Java 8中成功

搜索列表返回多个频道

如何将Pane的图像快照保存为BMP?

垃圾收集时间长,会丢弃网络连接,但不会在Kubernetes中反弹Pod

如何创建模块信息类文件并将其添加到JAR中?

如何在不作为类出现的表上执行原生查询?

协同 routine 似乎并不比JVM线程占用更少的资源

Maven创建带有特定类的Spring Boot jar和普通jar

java 11上出现DateTimeParseException,但java 8上没有

如何解释泛型类层次 struct 中子类的返回值类型和参数定义?