When you read in an #inst
literal, by default, a java.util.Date
instance is created. These have a method defined for print-method
(a multimethod responsible for printed representations of objects) in the clojure.instant
namespace (the relevant fragment of the 1.5.1 source is here).
clj-time uses the DateTime
type from Joda-Time, which has no custom print-method
method set up. This makes sense, since having a print-method
which produces a representation which doesn't round-trip is rarely desirable. In order for it to round-trip, you'd have to use a non-standard #inst
reader. To achieve that, if you're reading edn data, you can use clojure.edn/read
(or read-string
) passing the appropriate reader in as documented in its docstring:
(require '[clojure.edn :as edn])
(edn/read {:readers {'inst your-inst-reader}} rdr)
When using clj-time, your-inst-reader
will be something like #(clj-time.format/parse some-formatter %)
. You can then provide a matching print-method
definition following the example of clojure.instant
. (Or you could just provide the print-method
definition and remember that you won't get perfect roundtripping; clj-time provides very convenient coercion functions -- see below -- so this might be ok.)
Alternatively you could print and read java.util.Date
instances and convert them to and from DateTime
s; there are functions for doing that in clj-time.coerce
.
(java.util.Date.)
prints#inst "2013-07-09T18:05:53.231-00:00"
. – juan.facorro Jul 9 '13 at 18:06clj-time.core/now
is a DateTime object, which is Joda. – Jeremy Jul 9 '13 at 18:07DateTime
tojava.util.Date
and then printing it? – juan.facorro Jul 9 '13 at 18:08