In this guide, we will discuss Clojure Date and Time. Since the Clojure framework is derived from Java classes, one can use the date-time classes available in Java in Clojure. The class date represents a specific instant in time, with millisecond precision.
Following are the methods available for the date-time class.
java.util.Date
This is used to create the date object in Clojure.
Syntax
Following is the syntax.
java.util.Date.
Parameters β None.
Return Value β Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
Example
An example on how this is used is shown in the following program.
(ns example) (defn Example [] (def date (.toString (java.util.Date.))) (println date)) (Example)
Output
The above program produces the following output. This will depend on the current date and time on the system, on which the program is being run.
Tue Mar 01 06:11:17 UTC 2016
java.text.SimpleDateFormat
This is used to format the date output.
Syntax
Following is the syntax.
(java.text.SimpleDateFormat. format dt)
Parameters β βformatβ is the format to be used when formatting the date. βdtβ is the date which needs to be formatted.
Return Value β A formatted date output.
Example
An example on how this is used is shown in the following program.
(ns example) (defn Example [] (def date (.format (java.text.SimpleDateFormat. "MM/dd/yyyy") (new java.util.Date))) (println date)) (Example)
Output
The above program produces the following output. This will depend on the current date and time on the system, on which the program is being run.
03/01/2016
getTime
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
Syntax
Following is the syntax.
(.getTime)
Parameters β None.
Return Value β The number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.
Example
An example on how this is used is shown in the following program.
(ns example) (import java.util.Date) (defn Example [] (def date (.getTime (java.util.Date.))) (println date)) (Example)
Output
The above program produces the following output. This will depend on the current date and time on the system, on which the program is being run.
1456812778160
Next Topic : Click Here
Pingback: Clojure - Destructuring | Adglob Infosystem Pvt Ltd