如何从#inst Date文字中使用clojure.java-time获取java.time DateTime? [英] How can one get a java.time DateTime using clojure.java-time from #inst Date literal?

查看:130
本文介绍了如何从#inst Date文字中使用clojure.java-time获取java.time DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 clj-time 可以解析#inst Date文字,例如所以:

Using clj-time it is possible to parse an #inst Date literal like so:

(require '[clj-time.coerce :as c])
(c/from-date #inst "2012-12-12")
;; => #object[org.joda.time.DateTime 0x4a251269 "2012-12-12T00:00:00.000Z"]

如何使用新的java.time包装器 clojure.java-time a>?

How would this be done using the new java.time wrapper clojure.java-time?

推荐答案

请注意:不赞成使用Joda Time和包装它的clj-time库.您应该通过Java使用java.time对大多数任务进行互操作.这里也有一些帮助程序功能,您可能会觉得有用.

PLEASE NOTE: Joda Time and the clj-time library which wraps it are both deprecated. You should use java.time via Java interop for most tasks. There are also some helper functions here you may find useful.

Clojure将每个#inst文字转换为java.util.Date对象.您只需要内置的.toInstant()方法:

Clojure converts each #inst literal into a java.util.Date object. All you need is the built-in .toInstant() method:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test)
  (:import [java.time ZonedDateTime ZoneId]))

(defn inst->date-time
  "Convert a java.time.Instant to a DateTime for the supplied ZoneId"
  [inst zoneid]
  (.toLocalDate zdt-utc
    (ZonedDateTime/ofInstant instant zoneid)))
    
(dotest
  (let [may-4    #inst "2018-05-04T01:23:45.678-00:00" ; a java.util.Date
        instant  (.toInstant may-4) ]
    (spyxx may-4)
    (spyx instant)
    (println "utc =>" (inst->date-time instant (ZoneId/of "UTC")))
    (println "nyc =>" (inst->date-time instant (ZoneId/of "America/New_York")))
    ))

有结果

may-4    => <#java.util.Date #inst "2018-05-04T01:23:45.678-00:00">
instant  => #object[java.time.Instant 0x2207eb9f "2018-05-04T01:23:45.678Z"]
utc      => #object[java.time.LocalDate 0x62b5a16f 2018-05-04]
nyc      => #object[java.time.LocalDate 0x379b6a27 2018-05-03]

j.u.Date的扩展是在与java.time软件包同时添加到Java的,正是为了促进代码从java.util.Datejava.time的过渡.

This extension to j.u.Date was added to Java at the same time as the java.time package exactly to facilitate transition of code away from java.util.Date to java.time.

请注意,您仍然要小心,因为示例瞬间会根据所使用的时区产生两个不同的LocalDate对象.

Note that you still have to be careful, as the sample instant yields two different LocalDate objects, depending on the time zone used.

如果您使用的是java.time软件包,则可能对单元测试给出了很好的例子辅助功能以及与本机java.time函数的互操作.

If you are working with the java.time package, you may be interested in some helper functions I wrote. The unit tests give good examples of both the helper functions and interop with native java.time functions.

如果您正在那里寻找一个函数而找不到它,那么很有可能它已经存在于java.time包中,并且很容易通过Java interop轻松使用. java.time是一个经过深思熟虑的库(由以前的Joda Time库的作者编写). java.time的大多数功能都是简单的& Clojure易于使用,并且没有包装功能.

If you are looking for a function there and don't find it, there is a good chance that it already exists in the java.time package, and is easy to use directly via Java interop. java.time is a very well thought out library (by the author of the previous Joda Time library). Most features of java.time are simple & easy to use from Clojure, and do not benefit from having a wrapper function.

这篇关于如何从#inst Date文字中使用clojure.java-time获取java.time DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆