Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting strings into dates in clojure

Tags:

clojure

I have strings in the following format "2013-02-20T17:24:33Z" and "Mon Feb 25 02:42:27 +0000 2013".

Is there a quick way to convert these into date time format so that they can be tested for equality and/or sorted.

clj-time does allow me to this format (date-time 1986 10 14 4 3 27 456). However to achieve this I will have to parse the two above strings. The above strings being standard formats, is there a way to directly convert them into date time objects?

Thanks, Murtaza

like image 620
murtaza52 Avatar asked Feb 28 '13 07:02

murtaza52


2 Answers

clj-time has standard formatters defined, see clj-time.format/show-formatters, but your second format is not a 'standard' format as far as clj-time is concerned (although it does look suspiciously close to rfc822). You can create a custom formatter tho...

(use 'clj-time.format)

(parse (formatters :date-time-no-ms) "2013-02-20T17:24:33Z")
#<DateTime 2013-02-20T17:24:33.000Z>

(parse (formatter "E MMM dd hh:mm:ss Z YYYY") "Mon Feb 25 02:42:27 +0000 2013" )
#<DateTime 2013-02-25T02:42:27.000Z>

(show-formatters)
:basic-date                             20130228
:basic-date-time                        20130228T114047.213Z
:basic-date-time-no-ms                  20130228T114047Z
:basic-ordinal-date                     2013059
:basic-ordinal-date-time                2013059T114047.213Z
:basic-ordinal-date-time-no-ms          2013059T114047Z
:basic-t-time                           T114047.213Z
:basic-t-time-no-ms                     T114047Z
:basic-time                             114047.213Z
:basic-time-no-ms                       114047Z
:basic-week-date                        2013W094
:basic-week-date-time                   2013W094T114047.213Z
:basic-week-date-time-no-ms             2013W094T114047Z
:date                                   2013-02-28
:date-hour                              2013-02-28T11
:date-hour-minute                       2013-02-28T11:40
:date-hour-minute-second                2013-02-28T11:40:47
:date-hour-minute-second-fraction       2013-02-28T11:40:47.213
:date-hour-minute-second-ms             2013-02-28T11:40:47.213
:date-time                              2013-02-28T11:40:47.213Z
:date-time-no-ms                        2013-02-28T11:40:47Z
:hour                                   11
:hour-minute                            11:40
:hour-minute-second                     11:40:47
:hour-minute-second-fraction            11:40:47.213
:hour-minute-second-ms                  11:40:47.213
:ordinal-date                           2013-059
:ordinal-date-time                      2013-059T11:40:47.213Z
:ordinal-date-time-no-ms                2013-059T11:40:47Z
:rfc822                                 Thu, 28 Feb 2013 11:40:47 +0000
:t-time                                 T11:40:47.213Z
:t-time-no-ms                           T11:40:47Z
:time                                   11:40:47.213Z
:time-no-ms                             11:40:47Z
:week-date                              2013-W09-4
:week-date-time                         2013-W09-4T11:40:47.213Z
:week-date-time-no-ms                   2013-W09-4T11:40:47Z
:weekyear                               2013
:weekyear-week                          2013-W09
:weekyear-week-day                      2013-W09-4
:year                                   2013
:year-month                             2013-02
:year-month-day                         2013-02-28
like image 74
sw1nn Avatar answered Oct 19 '22 15:10

sw1nn


Use SimpleDateFormat.

(let [input "Mon Feb 25 02:42:27 +0000 2013"
      fmt (java.text.SimpleDateFormat. "EEE MMM dd HH:mm:ss zzz yyyy")]
  (.parse fmt input))

Remember that parsing months' and weekdays' names requires appropriately set locale.

As a side note, the first date format in your question is ISO 8601.

like image 20
Jan Avatar answered Oct 19 '22 16:10

Jan