Converting a date into a string

Clojure:
Converting a date into a string

How to:

In Clojure, we use the Java interop capabilities to format dates. Here’s a quick guide:

(import java.text.SimpleDateFormat)
(import java.util.Date)

;; Create a date object (let's use the current date and time)
(def now (Date.))

;; Set up the desired format
(def formatter (SimpleDateFormat. "yyyy-MM-dd HH:mm:ss"))

;; Format the date as a string
(def formatted-date (.format formatter now))

;; Print it out
(println formatted-date)
;; Output could be: "2023-03-15 09:26:45" (depends on the current date and time)

Deep Dive

Converting dates to strings isn’t exclusive to Clojure; it’s a common operation in many programming languages. Historically, the need arose as early as computers started handling dates because human-readable representation eases understanding and communication, while machines prefer more structured data formats.

In Clojure, because it runs on the Java Virtual Machine (JVM), we usually leverage Java’s date and time libraries, like java.util.Date and java.text.SimpleDateFormat. While these classes are long-standing, the newer java.time package (introduced in Java 8) represents an alternative with improved thread-safety and a more intuitive API.

Clojure doesn’t have a built-in date formatting library that is part of the core language, so it’s typical to use Java interop or third-party libraries, such as clj-time (a wrapper around Joda Time) for more idiomatic Clojure solutions.

Here’s how you might use java.time for formatting:

(import java.time.LocalDateTime)
(import java.time.format.DateTimeFormatter)

;; Create a date object (the current date and time)
(def now (LocalDateTime/now))

;; Set up the desired format
(def formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))

;; Format the date as a string
(def formatted-date (.format now formatter))

;; Print it out
(println formatted-date)
;; Similar output as before, with the current date and time

This method avoids the mutability issues present with SimpleDateFormat and should be preferred in new code where thread-safety is a concern.

See Also