Dates and Timeλ︎
Introduction to Java Interoperability
Use cases for dates and times
- timestamps on log events
- calendars
- tracking work
- calculating time between two date stamps
Java Time available by default
java.time
namespace is available on the class path by default, require
or import
expressions are not required.
Current data timeλ︎
Create a date and time stamp with the current data and time for the Operating System timezone.
A Java time object is returned
Current date
Instant
(java.time.Instant/now)
;; => #object[java.time.Instant 0x3684d2c0 "2023-07-13T13:02:27.889805413Z"]
Clojure Specλ︎
use Clojure Spec to define a data structure containing a java.time.LocalDate element
(spec/def :employee/first-name string?)
(spec/def :employee/last-name string?)
(spec/def :employee/birth-date #(instance? java.time.LocalDate %))
(spec/def :employee/person
(s/keys :req [:employee/first-name
:employee/last-name
:employee/birth-date]))
(def jenny #:ex{:first-name "Jenny"
:last-name "Barnes"
:birth-date (java.time.LocalDate/parse "1910-03-15")})
Check to see if Jenny is a valid employee