Tag: postgresql 9.3

Postgres UTC到Java ZonedDateTime

关于我原始问题的后续问题 。 因为我知道所使用的任何日期时间都是针对已知的时区,而不是用户可能提交请求的时间,我会使用LocalDateTime,转换为UTC并保留。 然后,当检索到约会时,我将保存的时间转换为会议位置时区(存储在db中)。 但是,我保存的值似乎实际上保存在我当地的时区中。 我在Rest Controller中收到日期时间值,例如: startLocalDateTime: 2016-04-11T10:00 endLocalDateTime: 2016-04-11T10:30 约会有两个ZoneDateTime字段: @Column(name = “startDateTime”, columnDefinition= “TIMESTAMP WITH TIME ZONE”) private ZonedDateTime startDateTime; @Column(name = “endDateTime”, columnDefinition= “TIMESTAMP WITH TIME ZONE”) private ZonedDateTime endDateTime; 然后我将值更改为UTC并存储在我的实体上以存储到Postgres: appointment.setStartDateTime(startLocalDateTime.atZone(ZoneId.of( “UTC” ))) appointment.setEndDateTime(endLocalDateTime.atZone(ZoneId.of( “UTC” ))) 我将它存储在Postgres中(columnDefinition= “TIMESTAMP WITH TIME ZONE”)当我查看pgadminIII中的记录时,我看到: startDateTime “2016-04-11 04:00:00-06” endDateTime “2016-04-11 04:30:00-06” 所以这些似乎以UTC格式正确存储(如果到目前为止我做错了,请纠正我)。 然后我从数据库中检索它们并将它们返回为: Appointment […]

LocalDateTime到ZonedDateTime

我有Java 8 Spring网络应用程序,它将支持多个区域。 我需要为客户位置制作日历活动。 因此,假设我的网站和Postgres服务器托管在MST时区(但我想如果我们去云端,它可能就在任何地方)。 但客户是在美国东部时间。 所以,按照我读到的一些最佳实践,我想我会以UTC格式存储所有日期时间。 数据库中的所有日期时间字段都声明为TIMESTAMP。 所以这是我如何采用LocalDateTime并转换为UTC: ZonedDateTime startZonedDT = ZonedDateTime.ofLocal(dto.getStartDateTime(), ZoneOffset.UTC, null); //appointment startDateTime is a LocalDateTime appointment.setStartDateTime( startZonedDT.toLocalDateTime() ); 现在,例如,当搜索日期时,我必须从请求的日期时间转换为UTC,获取结果并转换为用户时区(存储在数据库中): ZoneId userTimeZone = ZoneId.of(timeZone.getID()); ZonedDateTime startZonedDT = ZonedDateTime.ofLocal(appointment.getStartDateTime(), userTimeZone, null); dto.setStartDateTime( startZonedDT.toLocalDateTime() ); 现在,我不确定这是正确的方法。 我也想知道是因为我从LocalDateTime到ZonedDateTime,反之亦然,我可能会失去任何时区信息。 这是我所看到的,这对我来说似乎不正确。 当我从UI收到LocalDateTime时,我得到了这个: 2016-04-04T08:00 ZonedDateTime = dateTime=2016-04-04T08:00 offset=”Z” zone=”Z” 然后,当我将转换后的值分配给我的约会LocalDateTime时,我存储: 2016-04-04T08:00 我觉得因为我存储在LocalDateTime中我失去了转换为ZonedDateTime的时区。 我应该让我的实体(约会)使用ZonedDateTime而不是LocalDateTime,以便Postgres不会丢失这些信息吗? —————- 编辑 —————- 在Basils出色的答案之后,我意识到我有一个不关心用户时区的奢侈 […]

如何使用JOOQ在PostgreSQL中插入带有JSON列的可更新记录?

我跟着答案是否有可能写一个数据类型转换器来处理postgres JSON列? 实现nodeObject转换器。 然后我尝试使用可更新的记录来插入记录,我得到了“org.jooq.exception.SQLDialectNotSupportedException:在方言POSTGRES中不支持类型类org.postgresql.util.PGobject”exception。 我怎么解决这个问题? 以下是我的代码: TableRecord r = create.newRecord(TABLE); ObjectNode node = JsonNodeFactory.instance.objectNode(); r.setValue(TABLE.JSON_FIELD, node, new JsonObjectConverter()); r.store();