Monday, 15 April 2013

How to Convert A Clojure/Java Date to Simpler Form -



How to Convert A Clojure/Java Date to Simpler Form -

i want take

(str (date.)) "thu feb 07 12:15:03 est 2013"

and turn string, can input informix date field mm/dd/yyyy.

02/07/2013

i have been looking @ various posts, no success. here's first thing found, , tried, knowing i'd have reverse order of date format. wanted work.

(defn str-to-date [date] (. (simpledateformat. "yyyy-mm-dd") parse date))

i error

(str-to-date (str (date.))) parseexception unparseable date: "thu feb 07 12:44:41 est 2013" java.text.dateformat.parse (dateformat.java:354)

i have tried this

(.parse (simpledateformat. "mm/dd/yyyy") (str (date.))) parseexception unparseable date: "thu feb 07 12:42:02 est 2013" java.text.dateformat.parse (dateformat.java:354)

with no success. documentation pointers or answers appreciated.

(def date (java.util.date.)) date => #inst "2013-02-07t19:08:12.107-00:00"

you can straight format desired format

(.format (java.text.simpledateformat. "mm/dd/yyyy") date) => "02/07/2013"

but if starting string,

(str date) => "thu feb 07 13:08:12 cst 2013"

you must first parse using string's format

(def df (java.text.simpledateformat. "eee mmm d hh:mm:ss zzz yyyy")) (.parse df (str date)) => #inst "2013-02-07t19:08:12.107-00:00"

and desired format

(.format (java.text.simpledateformat. "mm/dd/yyyy") (.parse df (str date))) => "02/07/2013"

you might want time , date libraries: what clojure time , date libraries?.

date clojure

No comments:

Post a Comment