java - 如何从 "timestamp without time zone"字段收集到没有时区的 Java 时间戳

标签 java psql jooq timezone-offset

我的 Jooq 查询:

dslContext.select(
                timeInterval,
                ifnull(avg(field(FieldName.AVERAGE, Double.class))
                        .cast(Double.class), 0.0))
                .from(channelTimebucketQuery)
                .groupBy(timeInterval)
                .orderBy(timeInterval)
                .collect(Collectors.toMap(Record2::component1, Record2::component2));

返回一个带有时区的时间戳和一个 double 值 enter image description here

但是我的分析器说它应该返回一个没有时区的时间戳。

select pg_typeof(time_bucket) from (

    select "alias_67258973"."time_bucket", coalesce(cast(avg(average) as double precision), 0.0) from (select "public"."time_bucket_gapfill"("bucket_width" := cast("public"."create_interval"("seconds" := 43200) as "pg_catalog"."interval"), "ts" := cast("public"."testTable"."time" as timestamp), "start" := cast(null as timestamp), "finish" := cast(null as timestamp)) as "time_bucket", avg("public"."testTable"."average") as "average" from "testTable" where ("public"."testTable"."device" in ('702088'  ) and "public"."testTable"."time" >= timestamp '2020-02-10 13:57:28.2212375' and "public"."testTable"."time" <= timestamp '2020-02-24 13:57:28.2222399') group by time_bucket) as "alias_67258973" group by "alias_67258973"."time_bucket" order by "alias_67258973"."time_bucket"
        ) as x;

enter image description here

时区从哪里来?如何将时区设置为 + 0000

最佳答案

SQL 类型的默认 JDBC 类型 TIMESTAMP WITHOUT TIME ZONE (或只是 TIMESTAMP )是 java.sql.Timestamp 。由于历史原因,我们都感到遗憾,java.sql.Timestamp延伸java.util.Date ,其模型为 TIMESTAMP WITHOUT TIME ZONE通过将您的 JVM 时区(即 TimeZone.getDefault() )与 unix 时间戳相关联。

更好地表示 SQL TIMESTAMP WITHOUT TIME ZONE数据类型为java.time.LocalDateTime ,jOOQ 也支持。 jOOQ 代码生成器的最新版本将设置 <javaTimeTypes>true</javaTimeTypes>使 JSR-310 类型成为默认类型。

尽管如此,尽管调试器和 java.sql.Timestamp 中相关的隐式时区造成了困惑。 ,这两种数据类型彼此等效,并且可以通过以下方式相互转换:

关于java - 如何从 "timestamp without time zone"字段收集到没有时区的 Java 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60378166/

相关文章:

java - JOOQ Mysql 整合问题

java - 使用 jOOQ 强制 boolean 类型抛出 NPE

java - 如何记录JOOQ执行的生成的sql

java - 扩展 Java 的链表供个人使用

c# - "this"在 Java 和/或 C# 中有何用途?

postgreSQL fatal error 窗口

MySQL 代码到 postgres

postgresql - PSQL:SSL 错误:未知协议(protocol)

java - 数据未插入数据库 JPA+Hibernate

java - Android 调试 InetAddress.isReachable