Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get date and time in Clojure?

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?

like image 245
Ali Avatar asked Jan 08 '11 19:01

Ali


3 Answers

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.

like image 83
Goran Jovic Avatar answered Nov 20 '22 09:11

Goran Jovic


Java 1.8 added the java.time package to the core JDK to clean up many of the frustrations 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:

(java.time.LocalDateTime/now)
like image 22
Brad Koch Avatar answered Nov 20 '22 09:11

Brad Koch


There is a Clojure-wrapper library for Joda-Time. Or you'll have to use java interop with the standard Java API.

like image 18
robert_x44 Avatar answered Nov 20 '22 10:11

robert_x44