# Java.time

**URL:** <https://forum.datomic.com/t/java-time/1406>\
**Category:** General\
**Created:** [April 3, 2020, 5:06am UTC](https://forum.datomic.com/t/java-time/1406 "2020-04-03T05:06:05Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![onetom](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.datomic.com/onetom/32/430_2.png) [@onetom](https://forum.datomic.com/u/onetom)\
**Post date:** [May 27, 2021, 5:57am UTC](https://forum.datomic.com/t/java-time/1406/4 "2021-05-27T05:57:14Z")

</div>

I just made a transparent solution - using Richelieu advices - for using `java.time.Instant` in transactions and receiving them in pull results.

See my original post about it here:

> **[Teach \`java.time.Instant\` to Datomic (Cloud) \`transact\` and \`pull\`](https://clojureverse.org/t/teach-java-time-instant-to-datomic-cloud-transact-and-pull/7698)**
>
> Since the java.time package is well-designed and built-in to Java 8+ and it’s quite convenient to work with, via juxt/tick, I really wanted to use it with Datomic transparently. Here is my 1st, seemingly successful attempt, which advises the d/pull...

For convenience, I will repeat the code here too:

```clojure
(ns xxx.datomic.client
  "Conveniences for working with the `datomic.client.api`."
  (:require
    [datomic.client.api :as d]
    [richelieu.core :refer :all]
    [tick.alpha.api :as t]
    [clojure.walk :as walk]))

(defn- maybe-instant->inst [i] (if (t/instant? i) (t/inst i) i))

(defadvice ^:private transact-instants [transact conn arg-map]
  (transact conn
            (update arg-map :tx-data
                    (partial walk/postwalk maybe-instant->inst))))

(defonce _transact-instants (advise-var #'d/transact #'transact-instants))

(defn- maybe-inst->instant [i] (if (inst? i) (t/instant i) i))

(defadvice ^:private pull-instants
  ([pull db arg-map]
   (->> (pull db arg-map)
        (walk/postwalk maybe-inst->instant)))
  ([pull db selector eid]
   (->> (pull db selector eid)
        (walk/postwalk maybe-inst->instant))))

(defonce _pull-instants (advise-var #'d/pull #'pull-instants))

```

Here is an example of a `java.time.Instant` roundtrip to a Datomic DB:

```clojure
(comment
  (let [client (d/client {:server-type :dev-local
                          :storage-dir :mem
                          :system "tick"})
        db-ref {:db-name "demo"}
        _ (d/create-database client db-ref)
        conn (d/connect client db-ref)]
    
    (->> [[{:db/ident :timestamp
            :db/valueType :db.type/instant
            :db/cardinality :db.cardinality/one}]
          
          [{:db/ident :t1 
            :timestamp (t/now)}]]
         (reduce (fn [c tx] (d/transact c {:tx-data tx}) c) conn))
    
    (d/pull (d/db conn) [:timestamp] :t1))
  )

```

It returns `{:timestamp #time/instant"2021-05-27T05:54:00.260Z"}`.

---

_[View the full topic](https://forum.datomic.com/t/java-time/1406)._
