Q:

clojure - best way to increase date by x days

I need to read in a date string in yyyyMMdd format and increase it by x amount of days - at the minute I am doing it by converting to millis and adding 1 day in mills then converting back to yyyyMMdd.
(.print 
 (.withZone 
  (DateTimeFormat/forPattern "yyyymmdd") (DateTimeZone/forID "EST"))
 (+ 86400000 (.parseMillis 
  (.withZone (DateTimeFormat/forPattern "yyyymmdd")
  (DateTimeZone/forID "EST")) "20150401")))

Is there a cleaner way to do this? clj-time library is not available to me, and I am using clojure 1.2

A:

Since you cant't use clj-time, which is the best option in this case, I can't think of anything better than using org.joda.time as you did.
However, I would suggest rewriting your code a little bit:

there is no need for time zones here;
you could create DateTimeFormat object once and reuse it.

Here is how your function could look:

(defn add [date pattern days]
  (let [fmt (DateTimeFormat/forPattern pattern)
        add (* 86400000 days)]
    (->>  date
          (.parseMillis fmt)
          (+ add)
          (.print fmt))))

(add "20150401" "yyyymmdd" 1) ; => "20150402"

If you don't want to work with milliseconds, you could use .parseDateTime instead of .parseMillis and .plusDays method do add days to the parsed date:

(defn add [date pattern days]
  (let [fmt (DateTimeFormat/forPattern pattern)
        dt  (.parseDateTime fmt date)]
    (.print fmt (.plusDays dt days))))

