9

I'm trying to parse a unformatted string which contains a date (e.g. today = "08082013") to the format "08.08.2013".

This works: (.parse (java.text.SimpleDateFormat. "ddMMyyyy") today) => <Date Sun Jan 01 00:00:00 CET 1950>

But when I do (.parse (java.text.SimpleDateFormat. "dd.MM.yyyy") today) I get the error "Unparseable date: "08082013"

Why? How can I get my desired date format?

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
m-arv
  • 463
  • 2
  • 6
  • 14

5 Answers5

17

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"
NielsK
  • 6,886
  • 1
  • 24
  • 46
4

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.

pjc
  • 157
  • 3
  • clj-time is deprecated since it is based on Joda time. See: https://stackoverflow.com/questions/29750221/is-joda-time-deprecated-with-java-8-date-and-time-api-java-time – Mariana B. Dec 11 '17 at 13:39
  • @MarianaB. clj-time is not 'deprecated'. People are advised to consider java.time instead, but at this point the project is alive and well maintained. – glts May 20 '18 at 21:39
  • 2
    true, I meant clj-time at this moment is based on Joda time and that is deprecated in newer releases of java. – Mariana B. May 22 '18 at 09:32
  • So to be oh so clear... clj-time is a wrapper for Joda time. Java Time is the replacement for Joda time and there is a Clojure wrapper for Java Time. Users have been asked to migrate away from Joda time in favor of Java Time and therefore... that implies away from clj-time. I think the current recommended time libraries are native Java Time or the Clojure wrapper for it. https://github.com/dm3/clojure.java-time – Jason Sep 13 '22 at 12:44
3

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

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53
0

Using clj-time, you can use this:

(defn format-date
  [date-str]
  (f/unparse (f/formatter "dd.MM.yyyy") (f/parse custom-formatter date-str)))

(format-date "08082013") ;=> "08.08.2013"
Édipo Féderle
  • 4,169
  • 5
  • 31
  • 35
0

Clojure is based on Java, so many of your options will end up calling Java in the end. The way I understand it, Java has two time apis. The old one is at java.text.SimpleDateFormat and is not recommended anymore. The new one is under java.time and is "much better".

With that in mind, you have several options for date manipulation in Clojure:

  • You can call either the new or old Java functions directly. NeilsK's answer provides examples on how to do this with the old api. You should be able to adapt it fairly easily to the new java.time api with some help from the Clojure documentation on java interop.
  • Everyone is saying use clj-time but I would not recommend it as it's based on a Java library called JodaTime which was deprecated when Java's new time API came out.
  • I personally have had a lot of success with a library called clojure.java-time which is a simple Clojure wrapper for Java's new time api.
  • juxt/tick looks very promising as it seeks to provide that new api in a clojure-friendly format across not only java platforms, but also in the browser and on dotnet. It was in alpha at the time of this writing but I will be keeping an eye on it.
BonsaiOak
  • 27,741
  • 7
  • 30
  • 54