Clojure - parse string to date

To get from string to date, use parse.

To get from date to string, use format.

Both use a formatter to describe the transition.

=>(.format
    (java.text.SimpleDateFormat. "dd.MM.yyyy")
    (.parse
      (java.text.SimpleDateFormat. "ddMMyyyy")
      "08082013"))

"08.08.2013"

If you are playing around with date and time I recommend checking out this Clojure lib,

https://github.com/clj-time/clj-time

It is kind of the time lib most Clojure programmers use, and is based on the java lib joda time, that by many is agreed to be better than the Java build in one.


The .parse method of SimpleDateFormat will not generate a string, it will read a string and generate a java.util.Date object. If you want to generate a dotted string, you need SimpleDateFormat with the dots in place and call .format on it, given a java.util.Date.

See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html or take a look at clj-time