如何从 "timestamp without time zone" 字段收集到没有时区的 Java 时间戳
How to collect from a "timestamp without time zone" Field to a Java Timestamp without Timezone
我的 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));
returns 一个附有时区的时间戳和一个双精度
但是我的 Profiler 说它应该 return 没有时区的时间戳。
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;
时区从何而来?我如何将时区设置为 + 0000
SQL 类型 TIMESTAMP WITHOUT TIME ZONE
(或只是 TIMESTAMP
)的默认 JDBC 类型是 java.sql.Timestamp
。由于我们都感到遗憾的历史原因,java.sql.Timestamp
扩展了 java.util.Date
,它通过关联您的 JVM 时区(即 TimeZone.getDefault()
) 带有 unix 时间戳。
SQL TIMESTAMP WITHOUT TIME ZONE
数据类型的更好表示是 java.time.LocalDateTime
,jOOQ 也支持它。较新版本的 jOOQ 代码生成器将设置 <javaTimeTypes>true</javaTimeTypes>
以使 JSR-310 类型成为默认类型。
尽管如此,尽管 java.sql.Timestamp
中调试器和相关的隐式时区造成混淆,但这两种数据类型是等价的,并且可以通过以下方式相互转换:
我的 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));
returns 一个附有时区的时间戳和一个双精度
但是我的 Profiler 说它应该 return 没有时区的时间戳。
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;
时区从何而来?我如何将时区设置为 + 0000
SQL 类型 TIMESTAMP WITHOUT TIME ZONE
(或只是 TIMESTAMP
)的默认 JDBC 类型是 java.sql.Timestamp
。由于我们都感到遗憾的历史原因,java.sql.Timestamp
扩展了 java.util.Date
,它通过关联您的 JVM 时区(即 TimeZone.getDefault()
) 带有 unix 时间戳。
SQL TIMESTAMP WITHOUT TIME ZONE
数据类型的更好表示是 java.time.LocalDateTime
,jOOQ 也支持它。较新版本的 jOOQ 代码生成器将设置 <javaTimeTypes>true</javaTimeTypes>
以使 JSR-310 类型成为默认类型。
尽管如此,尽管 java.sql.Timestamp
中调试器和相关的隐式时区造成混淆,但这两种数据类型是等价的,并且可以通过以下方式相互转换: