I came up with this for my scheduling needs (I use it for getting the
start time for a Timer):

(import '(java.util GregorianCalendar))

(defn get-next-friday-noon []
  (let [cal (new GregorianCalendar)
        day-of-week (. cal (get (. GregorianCalendar DAY_OF_WEEK)))
        friday            (. GregorianCalendar FRIDAY)
        day-of-week-field (. GregorianCalendar DAY_OF_WEEK)
        hour-of-day-field (. GregorianCalendar HOUR_OF_DAY)
        minute-field      (. GregorianCalendar MINUTE)
        second-field      (. GregorianCalendar SECOND)
        diff (- friday day-of-week)]
    (cond (> diff 0) ;; Friday is later this week
          (. cal (add day-of-week-field diff))
          (< diff 0) ;; Friday was earlier this week
          (. cal (add day-of-week-field (+ 7 diff)))
          (= diff 0) ;; Today is Friday
          (if (> (. cal (get hour-of-day-field)) 12)
            (. cal (add day-of-week-field 7))))
    ;; Always set time to 12:00:00 before it is returned
    (. (doto cal
         (set hour-of-day-field 12)
         (set minute-field 00)
         (set second-field 00))
       (getTime))))

It returns the time of next Friday at noon. If today is Friday, it
might return the time of noon today if it is before noon. If it is
after noon then it will return next Friday noon.

However, it seems very complicated, does anyone have some clever idea
on how to optimize it?

/Mathias
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to