clj-time.coerce java.sql.Date .toString 递减日期?

clj-time.coerce java.sql.Date .toString decrements date?

test_ns.clj

(ns test-ns
  (:require [clj-time.jdbc :as tj]
            [clj-time.coerce :as tc]))

(.toString (tc/to-sql-date (tc/from-string "2018-09-28")))

=> "2018-09-27"

为什么.toString 会递减日期?

project.clj:

(defproject my-project "0.1.0-SNAPSHOT"
  ...
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.10.0-beta1"]
                 [duct/core "0.7.0-alpha8"]
                 [duct/module.sql "0.5.0-alpha1"]
                 [duct/module.logging "0.4.0-alpha1"]
                 [duct/database.sql.hikaricp "0.3.3"]
                 [com.walmartlabs/lacinia "0.21.0"]
                 [com.rpl/specter "1.1.1"]
                 [net.mikera/core.matrix "0.62.0"]
                 [com.wsscode/pathom "2.2.4"]
                 [org.clojure/core.async "0.4.474"]
                 [org.postgresql/postgresql "42.2.5"]
                 [org.clojure/core.async "0.4.490"]
                 [com.layerware/hugsql "0.4.9"]
                 [cheshire "5.8.1"]
                 [camel-snake-kebab "0.4.0"]
                 [hickory "0.7.1"]
                 [incanter "1.9.3"]
                 [clj-http "3.9.1"]
                 [environ "1.1.0"]]
  :plugins [[lein-environ "1.1.0"]
            [duct/lein-duct "0.11.0-alpha6"]
            [lein-exec "0.3.7"]]
  :main ^:skip-aot portfolio-management.main
  :test-paths   ["test"]
  :source-paths ["src"]
  :target-path "target/%s"
  :resource-paths ["resources" "target/resources"]
  :prep-tasks ["javac" "compile" ["run" ":duct/compiler"]]
  :profiles
  {:dev          [:project/dev :profiles/dev]
   :repl         {:prep-tasks   ^:replace ["javac" "compile"]
                  :repl-options {:init-ns user}}
   :uberjar      {:aot :all}
   :profiles/dev {}
   :project/dev  {:source-paths   ["dev/src"]
                  :resource-paths ["dev/resources"]
                  :dependencies   [[integrant/repl "0.3.1"]
                                   [eftest "0.4.1"]
                                   [kerodon "0.9.0"]
                                   ]}})

编辑

~$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

我很确定这是一个时区问题,正如 Carcigenicate 已经指出的那样。 from-string 解析 UTC 时区中的字符串,java.sql.Date 使用默认的 JVM 时区。 如果您处于 "behind" UTC(向西)的时区,则返回的结果是正确的,因为在这些时区中,日期 2018-09-28 00:00:00 实际上是 2017-09-27。

(java.util.TimeZone/setDefault(java.util.TimeZone/getTimeZone "GMT"))
(.toString (tc/to-sql-date (tc/from-string "2018-09-28")))
;; => "2018-09-28"

(java.util.TimeZone/setDefault(java.util.TimeZone/getTimeZone "GMT-1"))
(.toString (tc/to-sql-date (tc/from-string "2018-09-28")))
;; => "2018-09-27"

您不应该使用 java.sql.Date 并期望它 returns UTC 时区的日期,只要那不是您的默认时区。