Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add days to current date in clojure

In clojure I want to add days to current date can anyone please guide me on that. Am getting current date as below and now let's say I want to add 7 days to it, how can I get a new date?

(.format (java.text.SimpleDateFormat. "MM/dd/yyyy") (java.util.Date.))
like image 817
Robin Avatar asked Nov 24 '14 22:11

Robin


2 Answers

This would work:

(java.util.Date. (+ (* 7 86400 1000) (.getTime (java.util.Date.)))

I prefer to use System/currentTimeMillis for the current time:

(java.util.Date. (+ (* 7 86400 1000) (System/currentTimeMillis)))

Or you can use clj-time which is a nicer api to deal with time (it's a wrapper around Joda Time). From the readme file:

(t/plus (t/date-time 1986 10 14) (t/months 1) (t/weeks 3))

=> #<DateTime 1986-12-05T00:00:00.000Z>

like image 110
Diego Basch Avatar answered Nov 18 '22 05:11

Diego Basch


user> (import '[java.util Calendar])
;=> java.util.Calendar
user> (defn days-later [n]
        (let [today (Calendar/getInstance)]
          (doto today
            (.add Calendar/DATE n)
            .toString)))
#'user/days-later
user> (println "Tomorrow: " (days-later 1))
;=> Tomorrow:  #inst "2014-11-26T15:36:31.901+09:00"
;=> nil
user> (println "7 Days from now: " (days-later 7))
;=> 7 Days from now:  #inst "2014-12-02T15:36:44.785+09:00"
;=> nil
like image 24
runexec Avatar answered Nov 18 '22 05:11

runexec