Daniel Wu
Daniel Wu

Reputation: 6003

how to get the current date as YYYYMMDD in clojure?

I am using the following code:

(require '[clj-time.core :as time]
         '[clj-time.coerce :as tc]
         '[clj-time.format :as f])
 (f/unparse (f/formatter "yyyyMMdd") time/now)

But it throws the following error.

caused by: java.lang.ClassCastException: clj_time.core$now cannot be cast to org.joda.time.ReadableInstant

Upvotes: 3

Views: 1164

Answers (1)

Viktor K.
Viktor K.

Reputation: 2740

unparse function takes 2 arguments. First is the format,which should be an instance of org.joda.time.format.DateTimeFormatter, which you create correctly by calling

 (f/formatter "yyyyMMdd")

the second argument is date time, which should be an instance of org.joda.time.DateTime and here you are doing small mistake. Instead of passing DateTime you are passing clojure function time/now, what you should do is to call the function like this

(f/unparse (f/formatter "yyyyMMdd") (time/now))

Upvotes: 2

Related Questions