为什么在 clojure 中解析日期会将结果向后移动 2 小时

Why parsing date in clojure shifts the result 2 hours back

Clojure 代码

(def fmt (java.text.SimpleDateFormat. "yyyy-MM-dd"))
#'user/fmt
user=> (.parse fmt "2015-07-10")
#inst "2015-07-09T22:00:00.000-00:00"

类似的Java代码:

public class DateFmt {
    public static void main(String[] args) throws ParseException {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        final Date date = sdf.parse("2015-07-10");

        System.out.println(date);
    }
}

虽然 Java 代码打印:Fri Jul 10 00:00:00 CEST 2015(这是我所期望的),但 Clojure 向后移动了 2 小时?

Clojure 的 instant 和 Java 的 Date 之间存在差异,主要是时间偏移量是可选的。

Unlike RFC3339:

  • we only parse the timestamp format
  • timestamp can elide trailing components
  • time-offset is optional (defaults to +00:00)

因此,Clojure 的时间始终与 UTC 相关联,而 Java 与您当地的时区相关联。

时间实际上是相等的,因为你是 two hours ahead of UTC

如果您希望 Clojure 输出与 Java 匹配,则必须指示 SimpleDateFormat 实例以 UTC 格式读取时间。

(def fmt
  (let [format (java.text.SimpleDateFormat. "yyyy-MM-dd")]
    (.setTimeZone format (java.util.TimeZone/getTimeZone "UTC"))
    format))