我正在尝试将时间在秒内转换为带有时区的时间戳。
我试过了
(defn- seconds-to-milliseconds[time]
(* time 1000))
(:require [clj-time.coerce :as clj-time])
(clj-time/from-long (seconds-to-milliseconds 1564132000))结果是:#clj-time/date-time"2019-07-26T09:06:40.000Z"
但是这个结果我不能存储在Postgres数据库中,因为我得到了
PSQLException: Can't infer the SQL type to use for an instance of org.joda.time.DateTime. Use setObject() with an explicit Types value to specify the type to use
所以我需要转换它
(clj-time/to-timestamp (clj-time/from-long (seconds-to-milliseconds 1564132000))))
结果是
#inst"2019-07-26T09:06:40.000000000-00:00"
我可以把这个储存在postgres但是它没有时区。
有人能帮我把时间用秒转换成时区的时间戳吗?
发布于 2019-08-20 16:38:12
找到了解决这个问题的方法。通过转换到UTC并存储在数据库中。
(:require [clj-time.coerce :as clj-time])
(:import (java.util TimeZone))
(TimeZone/setDefault (TimeZone/getTimeZone "UTC"))
(defn- seconds-to-milliseconds
[time]
(* time 1000))
(defn seconds-to-timestamp
[time]
(clj-time/to-sql-time (clj-time/from-long (seconds-to-milliseconds time))))示例:(seconds-to-timestamp 1564132000)
发布于 2019-08-19 05:18:17
我想你可以使用普通的旧java.util.Date
(-> time-in-millis
(java.util.Date.)
(save-in-db))因为您在输入上有时间(毫秒),所以您可以直接使用它,而不必将其转换为时区感知的日期类型。
顺便说一句。尽管PostgreSQL在内部具有“带时区”数据类型,但它不会影响值的存储方式(也就是说,“时区”信息无论如何都不会被存储!) ->参见Difference between timestamps with/without time zone in PostgreSQL获取更多详细信息。
https://stackoverflow.com/questions/57541451
复制相似问题