Reputation: 4839
How do I use the java.time
library in clojure? I can't even get it imported into my 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
It's in the java docs, and I have java 13 installed https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/time/Instant.html
There's https://github.com/dm3/clojure.java-time that claims to use it, that's cool if I were trying to use it in a project I might do that. But I just want to import it and play with a date in the repl. How is it done?
Upvotes: 3
Views: 506
Reputation: 1516
In Clojure, the syntax (some.class.Name.)
with a "." after the class name means to call a constructor for that class. If you look at the Java doc page for class java.time.Instant you will notice that it has no constructors: https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
There is a method called now
that returns an object of type Instant, which you can call with this syntax in Clojure:
user=> (java.time.Instant/now)
#object[java.time.Instant 0x599f571f "2019-10-09T05:18:06.192393Z"]
Upvotes: 4