如何将Clojure / Java日期转换为更简单的形式 [英] How to Convert A Clojure/Java Date to Simpler Form
问题描述
我想参加
(str (Date.))
"Thu Feb 07 12:15:03 EST 2013"
并将其转换为字符串,以便可以输入到Informix日期字段mm / dd / yyyy。
and turn it into a string, so it can be input into an Informix date field mm/dd/yyyy.
02/07/2013
我一直在看各种帖子,没有成功。这是我发现的第一件事,并尝试,知道我必须扭转日期格式的顺序。我只想让它工作。
I have been looking at various posts, with no success. Here's the first thing I found, and tried, knowing I'd have to reverse the order of the date format. I just wanted to get it to work.
(defn str-to-date [date] (. (SimpleDateFormat. "yyyy-MM-dd") parse date))
我得到此错误
(str-to-date (str (Date.)))
ParseException Unparseable date:
"Thu Feb 07 12:44:41 EST 2013" java.text.DateFormat.parse (DateFormat.java:354)
也试过这个
(.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)
没有成功。
推荐答案
(def date (java.util.Date.))
date
=> #inst "2013-02-07T19:08:12.107-00:00"
您可以直接格式化到所需格式
You can directly format to desired format
(.format (java.text.SimpleDateFormat. "MM/dd/yyyy") date)
=> "02/07/2013"
但如果从字符串开始,
(str date)
=> "Thu Feb 07 13:08:12 CST 2013"
您必须首先使用该字符串的格式进行解析
you must first parse using that 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"
所需格式
(.format (java.text.SimpleDateFormat. "MM/dd/yyyy") (.parse df (str date)))
=> "02/07/2013"
您可能还想查看一些时间和日期库: a href =http://stackoverflow.com/questions/5024970/what-are-the-clojure-time-and-date-libraries>什么是Clojure时间和日期库?。
You might also want to look into some time and date libraries: What are the Clojure time and date libraries?.
这篇关于如何将Clojure / Java日期转换为更简单的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!