gpt4 book ai didi

clojure - 在 Clojure 中将字符串转换为日期

转载 作者:行者123 更新时间:2023-12-02 23:26:00 28 4
gpt4 key购买 nike

我有以下格式的字符串 "2013-02-20T17:24:33Z""Mon Feb 25 02:42:27 +0000 2013"

是否有一种快速方法将它们转换为日期时间格式,以便可以测试它们的相等性和/或排序。

clj-time 确实允许我采用这种格式(date-time 1986 10 14 4 3 27 456)。然而,为了实现这一点,我必须解析上面的两个字符串。上述字符串是标准格式,有没有办法直接将它们转换为日期时间对象?

谢谢,穆尔塔扎

最佳答案

clj-time 定义了标准格式化程序,请参阅 clj-time.format/show-formatters ,但就 clj-time 而言,您的第二种格式不是“标准”格式(尽管它看起来确实非常接近 rfc822)。您可以创建自定义格式化程序...

(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

关于clojure - 在 Clojure 中将字符串转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129760/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com