如何在 clojure repl 中使用 java.time? Java 互操作找不到 类

How to use java.time in clojure repl? Java interop can't find classes

如何在 clojure 中使用 java.time 库?我什至无法将它导入我的 repl。

user=> *clojure-version*
{:major 1, :minor 10, :incremental 0, :qualifier nil}
user=> (java.util.Date.)
#object[java.util.Date 0x5c22a205 "Tue Oct 08 22:10:21 PDT 2019"]

user=> (java.time.Instant.)
Syntax error (IllegalArgumentException) compiling new at (REPL:1:1).
No matching ctor found for class java.time.Instant

它在 java 文档中,我安装了 java 13 https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/time/Instant.html

https://github.com/dm3/clojure.java-time 声称使用它,如果我想在我可能会这样做的项目中使用它,那就太好了。但我只想导入它并在 repl 中玩一个日期。它是怎么做到的?

在 Clojure 中,语法 (some.class.Name.) 带有“.”在 class 名称之后意味着调用 class 的构造函数。如果您查看 class java.time.Instant 的 Java 文档页面,您会注意到它没有构造函数:https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html

有一个名为 now 的方法,它 returns 一个 Instant 类型的对象,您可以在 Clojure 中使用以下语法调用它:

user=> (java.time.Instant/now)
#object[java.time.Instant 0x599f571f "2019-10-09T05:18:06.192393Z"]