clojure - best way to increase date by x days -
i need read in date string in yyyymmdd format , increase x amount of days - @ minute doing converting millis , adding 1 day in mills converting yyyymmdd.
(.print (.withzone (datetimeformat/forpattern "yyyymmdd") (datetimezone/forid "est")) (+ 86400000 (.parsemillis (.withzone (datetimeformat/forpattern "yyyymmdd") (datetimezone/forid "est")) "20150401")))
is there cleaner way this? clj-time library not available me, , using clojure 1.2
since cant't use clj-time
, best option in case, can't think of better using org.joda.time
did.
however, suggest rewriting code little bit:
- there no need time zones here;
- you create
datetimeformat
object once , reuse it.
here how function 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 don't want work milliseconds, use .parsedatetime
instead of .parsemillis
, .plusdays
method add days parsed date:
(defn add [date pattern days] (let [fmt (datetimeformat/forpattern pattern) dt (.parsedatetime fmt date)] (.print fmt (.plusdays dt days))))
Comments
Post a Comment