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减少日期?
- 期望格式的日期字符串将匹配其输入
- tc/to-sql-date转换为java.sql.date class
- 。
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)
我很确定这是一个时区问题,正如已经指出的那样。 from-string
在UTC时区和java.sql.date中解析一个字符串。DATE使用默认的JVM TimeZone。返回的结果是正确的,如果您处于"落后" UTC(西部)的时区,因为在那些时区,2018-09-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,并期望它在UTC时区中返回日期,只要这不是您的默认时区。