How do I add days to a date in Clojure

parse returns a java.util.Date, the error you are seeing is telling you that you can't cast a Date to a Number. You can use getTime to get the milliseconds of a Date:

(java.util.Date. (+ (* 7 86400 1000)
                 (.getTime (.parse (java.text.SimpleDateFormat. "yyyy-MM-dd") date))))

This potentially adds 7 days to the date. If you want to potentially add 90 days you need to replace the 7 with 90, like this: (* 90 86400 1000).

You can also use java.util.Calendar:

(let [cal (Calendar/getInstance)
      d (.parse (java.text.SimpleDateFormat. "yyyy-MM-dd") date)]
  (doto cal
    (.setTime d)
    (.add Calendar/DATE 90)
    (.getTime)))

Or better yet, clj-time:

(require '[clj-time.core :as t])
(require '[clj-time.format :as f])

(t/plus (f/parse (f/formatters :year-month-day) date)
        (t/days 90))

I wouldn’t recommend using the legacy date and time API in new code if you can avoid it.

Java 8 brought a new API for dates with which you can express this problem elegantly:

(let [date-format java.time.format.DateTimeFormatter/ISO_LOCAL_DATE]
  (.. (java.time.LocalDate/parse "2015-02-13" date-format)
      (plusDays 90)
      (format date-format)))

Or even, taking all shortcuts:

(-> (java.time.LocalDate/parse "2015-02-13") (.plusDays 90) str)

clj-time has from-now and ago:

(require '[clj-time.core :refer [days from-now]])

(-> 90 days from-now)

=> #object[org.joda.time.DateTime 0x4d8bcee3 "2017-01-11T16:03:40.067Z"]

(require '[clj-time.core :refer [hours ago]])

(-> 7 hours ago)

=> #object[org.joda.time.DateTime 0x3eef2142 "2016-10-13T09:19:01.246Z"]

Available PeriodType definitions: years, months, weeks, days, hours, minutes, seconds.


The resulting Joda DateTime objects can easily be manipulated, for instance to a unix epoch in milliseconds:

(require '[clj-time.core :refer [days from-now]])
(require '[clj-time.coerce :as coerce])

(coerce/to-long (-> 90 days from-now))

=> 1484150620067