解析字符串以获取 Clojure 中特定格式的日期和时间
Parse string to get date and time in a specific format in Clojure
我刚开始玩 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 并认为 rfc822 是我的菜。所以我使用了格式化程序 rfc822 但它给了我异常:
java.lang.IllegalArgumentException: Invalid format: "2016-08-16T00:00:00.000+05:30"
这是我的代码:
(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))
谁能告诉我我做错了什么。或者 Clojure 中的任何其他方式来获得我想要的日期时间格式。由于我在 Clojure 方面完全天真,所以我请您详细回答,这将有助于我更好地理解这一点。谢谢。
无需实际检查哪个预定义格式器与您的日期格式匹配,您可以调用:
(f/parse "2016-08-14T19:45:48.000+05:30")
;; => #object[org.joda.time.DateTime 0x1bd11b14 "2016-08-14T14:15:48.000Z"]
它将尝试所有预定义的格式化程序和 return 从第一个成功的解析值。
然后以自定义格式打印:
(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"
不幸的是,据我所知,JodaTime 不处理添加 st、nd、rd[=24 之类的事情=]、th 天。
clj-time
library is a wrapper for Joda-Time. If you're using Java 8, you should prefer the built-in java.time
library over Joda-Time, for reasons explained . In Clojure, you can use the Clojure.Java-Time 库,它包装了 java.time
。首先,将其添加到您的依赖项中:
[clojure.java-time "0.2.1"]
然后就可以使用java-time
命名空间中的offset-date-time
and format
函数了:
(require '[java-time :as time])
(->> (time/offset-date-time "2016-08-14T19:45:48.000+05:30")
(time/format "HHmm'hrs', EEEE, MMMM d y"))
;;=> "1945hrs, Sunday, August 14 2016"
正如@Piotrek 在他的回答中指出的那样,Joda-Time 似乎不支持您原始问题中的“th”。从 DateTimeFormatter
文档来看,java.time
.
也没有
我刚开始玩 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 并认为 rfc822 是我的菜。所以我使用了格式化程序 rfc822 但它给了我异常:
java.lang.IllegalArgumentException: Invalid format: "2016-08-16T00:00:00.000+05:30"
这是我的代码:
(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))
谁能告诉我我做错了什么。或者 Clojure 中的任何其他方式来获得我想要的日期时间格式。由于我在 Clojure 方面完全天真,所以我请您详细回答,这将有助于我更好地理解这一点。谢谢。
无需实际检查哪个预定义格式器与您的日期格式匹配,您可以调用:
(f/parse "2016-08-14T19:45:48.000+05:30")
;; => #object[org.joda.time.DateTime 0x1bd11b14 "2016-08-14T14:15:48.000Z"]
它将尝试所有预定义的格式化程序和 return 从第一个成功的解析值。
然后以自定义格式打印:
(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"
不幸的是,据我所知,JodaTime 不处理添加 st、nd、rd[=24 之类的事情=]、th 天。
clj-time
library is a wrapper for Joda-Time. If you're using Java 8, you should prefer the built-in java.time
library over Joda-Time, for reasons explained java.time
。首先,将其添加到您的依赖项中:
[clojure.java-time "0.2.1"]
然后就可以使用java-time
命名空间中的offset-date-time
and format
函数了:
(require '[java-time :as time])
(->> (time/offset-date-time "2016-08-14T19:45:48.000+05:30")
(time/format "HHmm'hrs', EEEE, MMMM d y"))
;;=> "1945hrs, Sunday, August 14 2016"
正如@Piotrek 在他的回答中指出的那样,Joda-Time 似乎不支持您原始问题中的“th”。从 DateTimeFormatter
文档来看,java.time
.