Clojure Guides_ Date and Time
Clojure Guides_ Date and Time
This cookbook covers working with Java's java.time package in two styles, the first with interop and the
second using libraries.
This work is licensed under a Creative Commons Attribution 3.0 Unported License
(https://creativecommons.org/licenses/by/3.0/) (including images & stylesheets). The source is available
on Github (https://github.com/clojure-doc/clojure-doc.github.io).
Introduction
This cookbook does not assume a background in Java, if you have such a background the interop section
might be of interest.
This cookbook will be useful if you got your Clojure environment setup, know some of the basics and are
looking to work with time and dates in Clojure.
This guide aims to cover date and time in the JVM and JS domains. The guide currently covers only JVM.
The scope of this cookbook covers basic operations using the two styles.
Overview
The representation of date and time is dependent upon the host, for example in the JVM
Libraries
clojure.java-time (https://github.com/dm3/clojure.java-time)
cljc.java-time (https://github.com/henryw374/cljc.java-time)
A Clojure(Script) library which mirrors the java.time api through kebab-case-named vars.
tick (https://github.com/juxt/tick)
A Clojure(Script) & babashka library for dealing with time. Intended as a replacement for clj-
time.
Preliminaries
The examples below assume the following deps.edn
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.4"}
clojure.java-time/clojure.java-time {:mvn/version "1.3.0"}
com.widdindustries/cljc.java-time {:mvn/version "0.1.21"}}}
Recipes
clojure.java-time
Basics
For the people coming from a non-Java background, we are creating an instance of the
java.time.LocalDate , java.time.LocalTime and java.time.LocalDateTime classes respectively,
that's what the #object is for.
(require '[java-time.api :as jt])
;; What's the current day?
(jt/local-date)
;; => #object[java.time.LocalDate 0x28cb30b8 "2023-11-01"]
;; You may see a different result.
(ydm "20170108")
;; => #object[java.time.LocalDate 0x4851aa55 "2017-08-01"]
Using the import and the class name to create an instance of LocalDate :
(import (java.time LocalDate LocalTime LocalDateTime))
(ydm "20170108")
;; => #object[java.time.LocalDate 0x3135d642 "2017-08-01"]
cljc.java-time
According to the How it works (https://github.com/henryw374/cljc.java-time#how-it-works) section of
cljc.java-time each class in java.time has a corresponding Clojure namespace. This means that
we need to require each class as a namespace.
(require '[cljc.java-time local-date local-time local-date-time period temporal])
(require '[cljc.java-time.temporal.chrono-unit])
(require '[cljc.java-time.format.date-time-formatter])
(ydm "20170108")
;; #object[java.time.LocalDate 0x194dcb8 "2017-08-01"]
Examples
TBD
See also
Overview of Date and Time in Java
(https://docs.oracle.com/javase/tutorial/datetime/iso/overview.html)
« Mathematics with Clojure (/articles/cookbooks/math/) || Working with Files and Directories in Clojure »
(/articles/cookbooks/files_and_directories/)
Links
About (/articles/about/)
Table of Contents (/articles/content/)
Getting Started (/articles/tutorials/getting_started/)
Introduction to Clojure (/articles/tutorials/introduction/)
Clojure Editors (/articles/tutorials/editors/)
Clojure Community (/articles/ecosystem/community/)
Basic Web Development (/articles/tutorials/basic_web_development/)
Language: Functions (/articles/language/functions/)
Language: clojure.core (/articles/language/core_overview/)
Language: Collections and Sequences (/articles/language/collections_and_sequences/)
Language: Namespaces (/articles/language/namespaces/)
Language: Java Interop (/articles/language/interop/)
Language: Polymorphism (/articles/language/polymorphism/)
Language: Concurrency and Parallelism (/articles/language/concurrency_and_parallelism/)
Language: Macros (/articles/language/macros/)
Language: Laziness (/articles/language/laziness/)
Language: Glossary (/articles/language/glossary/)
Ecosystem: Library Development and Distribution (/articles/ecosystem/libraries_authoring/)
Ecosystem: Web Development (/articles/ecosystem/web_development/)
Ecosystem: Generating Documentation (/articles/ecosystem/generating_documentation/)
Building Projects: tools.build and the Clojure CLI (/articles/cookbooks/cli_build_projects/)
Data Structures (/articles/cookbooks/data_structures/)
Strings (/articles/cookbooks/strings/)
Mathematics with Clojure (/articles/cookbooks/math/)
Date and Time
Working with Files and Directories in Clojure (/articles/cookbooks/files_and_directories/)
Middleware in Clojure (/articles/cookbooks/middleware/)
Parsing XML in Clojure (/articles/cookbooks/parsing_xml_with_zippers/)
Growing a DSL with Clojure (/articles/cookbooks/growing_a_dsl_with_clojure/)