Search code examples
clojureclj-time

Parse string to get date and time in a specific format in Clojure


I just started to play with wit/duckling. It is written in Clojure and I have no previous experience in Clojure. I need to parse a string like 2016-08-14T19:45:48.000+05:30 to a format like 1945hrs, Sunday, August 14th 2016. I searched on Internet and came across to lib clj-time. After struggling with it for a long time I came across this thread and thought that rfc822 is my cup of tea. So I used formatter rfc822 but it is giving me exception:

java.lang.IllegalArgumentException: Invalid format: "2016-08-16T00:00:00.000+05:30"

Here is my code:

(ns firstproj.core
  (:gen-class)
  (:require [duckling.core :as p])
  (:require [clj-time.format :as f]))

(defn -main
  "I don't do a whole lot."
  [x]
  (p/load! { :languages ["en"]})
  (def var_ (p/parse :en$core x [:time]))
  (def date_string "2016-08-14T19:45:48.000+05:30")
  (f/parse (f/formatters :rfc822) date_string))

So can anybody tell me what I am doing wrong here. Or any other way in Clojure to get my desired date-time format. As I am completely naive in Clojure, I request you to answer in detail, it will help me to understand this in a better way. Thank You.


Solution

  • Without actually checking which predefined formatter matches your date format you might just call:

    (f/parse "2016-08-14T19:45:48.000+05:30")
    ;; => #object[org.joda.time.DateTime 0x1bd11b14 "2016-08-14T14:15:48.000Z"]
    

    It will try all predefined formatters and return parsed value from the first one that succeeds.

    Then to print in your custom format:

    (require '[clj-time.core :as t])
    
    (def my-time-zone (t/time-zone-for-offset 5 30))
    
    (def my-formatter
      (f/formatter
        "HHmm'hrs', EEEE, MMMM dd yyyy"
        my-time-zone))
    
    (f/unparse my-formatter some-date)
    ;; => "1945hrs, Sunday, August 14 2016"
    

    Unfortunately to my knowledge JodaTime doesn't handle things like adding st, nd, rd, th to days.