Blue42
Blue42

Reputation: 139

Clojure Unix time stamp for previous day

(defn todaydate [& props]
(let [
tester(str "")
tester (let [today (Date.)]
   (.getTime today))

The piece of code above returns a unix timestamp for the current day. I want to be able to use the tester to get a unix timestamp for the day previous to this.

The clj-time library is not available to me for using to solve this problem

Upvotes: 1

Views: 270

Answers (1)

Jared314
Jared314

Reputation: 5231

You will want to use a java.util.Calendar, add negative one days, and then use getTimeInMillis divided by 1000 to get the timestamp.

The following would give you the unix timestamp for midnight yesterday.

(let [yesterday (doto (GregorianCalendar. (TimeZone/getTimeZone "UTC"))
                  (.set Calendar/HOUR_OF_DAY, 0)
                  (.set Calendar/MINUTE 0)
                  (.set Calendar/SECOND 0)
                  (.set Calendar/MILLISECOND 0)
                  (.add Calendar/DAY_OF_YEAR -1))]
  (/ (.getTimeInMillis yesterday) 1000))

Note: Be careful about the timezone you use.

Also, if you just need something quick, you could just subtract the number of seconds in a day from the value you have.

Upvotes: 2

Related Questions