3. Dates and Times

Introduction

;;------------------------------------
;; Use a calendar to compute year, month, day, hour, minute and second values.

(import '(java.util Calendar))
(import '(java.util GregorianCalendar))

(defn print-day-of-year []
  (let [cal (GregorianCalendar.)]
    (printf "Today is day %d of the current year.\n"
            (.get cal Calendar/DAY_OF_YEAR))))

Finding Today's Date

;;------------------------------------
(import '(java.util Calendar))
(import '(java.util GregorianCalendar))

(defn todays-date []
  (let [cal (GregorianCalendar.)
        day (.get cal Calendar/DATE)
        month (.get cal Calendar/MONTH)
        year (.get cal Calendar/YEAR)]
    [day month year]))

(defn print-todays-date []
  (let [[day month year] (todays-date)]
    (printf "The current date is %d %02d %02d\n" year (inc month) day)))

Converting DMYHMS to Epoch Seconds

;;------------------------------------
(import '(java.util Calendar))
(import '(java.util TimeZone))
(import '(java.util GregorianCalendar))

(defn epoch-seconds-of-dmyhms [tz day month year hour minute second]
  (let [cal (GregorianCalendar.)
        zone (TimeZone/getTimeZone tz)]
    (do
      (.setTimeZone cal zone)
      (.set cal Calendar/DAY_OF_MONTH day)
      (.set cal Calendar/MONTH month)
      (.set cal Calendar/YEAR year)
      (.set cal Calendar/HOUR_OF_DAY hour)
      (.set cal Calendar/MINUTE minute)
      (.set cal Calendar/SECOND second)
      (int (/ (.getTime (.getTime cal)) 1000)))))

(epoch-seconds-of-dmyhms "UTC" 4 10 2011 12 30 55)

Converting Epoch Seconds to DMYHMS

;;------------------------------------
(import '(java.util Calendar))
(import '(java.util Date))
(import '(java.util GregorianCalendar))

(defn dmyhms-of-epoch-seconds [seconds]
  (let [cal (GregorianCalendar.)
        date (Date. (* seconds 1000))]
    (do
      (.setTime cal date)
      [(.get cal Calendar/DAY_OF_MONTH)
       (.get cal Calendar/MONTH)
       (.get cal Calendar/YEAR)
       (.get cal Calendar/HOUR_OF_DAY)
       (.get cal Calendar/MINUTE)
       (.get cal Calendar/SECOND)])))

(defn print-dmyhms-of-epoch-seconds [seconds]
  (let [[day month year hour minute seconds] (dmyhms-of-epoch-seconds seconds)]
    (printf "%02d-%02d-%d %02d:%02d:%02d\n"
            day (inc month) year hour minute seconds)))

;; 3.4 Adding or Subtracting from a Date

Adding to or Subtracting from a Date

Difference of Two Dates

Day in a Week/Month/Year or Week Number

Parsing Dates and Times from Strings

Printing a Date

High-Resolution Timers

Short Sleeps

Program: hopdelta