I need to log some events on a Clojure Client-Server scenario, but it seems to me that Clojure does not provide a date/time function. Can any one confirm this or I am missing something here?! If I am correct then I need to use java interop, right?
preguntado el 08 de enero de 11 a las 19:01
If all you need is to get the current time and date for your logger, then this function is OK:
(defn now  (new java.util.Date))
Now that you mentioned this, it would be useful to have support for immutable Date objects.
Java 1.8 agregó el java.time package to the core JDK to clean up many of the frustraciones with the state of date & time in Java. Since java.time is now a widely available part of core Java with a much improved API, I would encourage you to give it the first look when writing new date & time code.
Here's how you can retrieve the current date and time:
If you dont need nothing more advanced, just use Java classes.
(.format (java.text.SimpleDateFormat. "MM/dd/yyyy") (new java.util.Date))
Con la clj-time, the Clojure library that wraps the Java Joda Time library, you could use code like the following:
(require '[clj-time.core :as time]) (require '[clj-time.format :as time-format]) (time/now) => #<DateTime 2013-03-31T03:23:47.328Z> (def time-formatter (time-format/formatters :basic-date-time)) ;; ISO 8601 UTC format (time-format/unparse custom-formatter (date-time 2010 10 3)) => "20101003T000000.000Z"
One benefit of Joda Time (and hence clj-time) is that new releases support new changes to time zones.