我正在尝试使用java-time将本地日期转换为使用millis的即时,但是instant返回了一个没有millis的时间戳。
(def UTC (java-time/zone-id "UTC")
(defn local-date-to-instant [local-date]
(-> local-date
(.atStartOfDay UTC)
java-time/instant))
(local-date-to-instant (java-time/local-date))
=> #object[java.time.Instant 0xdb3a8c7 "2021-05-13T00:00:00Z"]但
(java-time/instant)
=> #object[java.time.Instant 0x1d1c27c8 "2021-05-13T13:12:31.782Z"]服务下游需要以下格式的字符串:yyyy-MM-ddTHH:mm:ss.SSSZ。
发布于 2021-05-14 02:53:59
创建一个DateTimeFormatter,即使毫秒(3个小数位)为零,也可以打印ISO即时:
(ns steffan.overflow
(:require [java-time :as jt])
(:import (java.time.format DateTimeFormatterBuilder)))
(def iso-instant-ms-formatter
(-> (DateTimeFormatterBuilder.) (.appendInstant 3) .toFormatter))使用示例:
(def today-inst (jt/truncate-to (jt/instant) :days))
(str today-inst) ; => "2021-05-13T00:00:00Z"
(jt/format iso-instant-ms-formatter today-inst) ; => "2021-05-13T00:00:00.000Z"发布于 2021-05-13 22:06:54
您需要使用DateTimeFormatter。然后,您可以编写如下代码:
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.format(formatter);特别是,请查看以下格式模式代码:
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321我认为S代码是最适合您使用的代码。您需要进行一些实验,因为文档中并不包含所有的细节。
这里有一个例子:
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[schema.core :as s]
[tupelo.java-time :as tjt]
)
(:import
[java.time Instant LocalDate LocalDateTime ZonedDateTime]
[java.time.format DateTimeFormatter]
))
(dotest
(let [top-of-hour (tjt/trunc-to-hour (ZonedDateTime/now))
fmt (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss.SSS")
]
(spyx top-of-hour)
(spyx (.format top-of-hour fmt))
))有结果
-----------------------------------
Clojure 1.10.3 Java 15.0.2
-----------------------------------
Testing tst.demo.core
top-of-hour => #object[java.time.ZonedDateTime 0x9b64076 "2021-05-13T07:00-07:00[America/Los_Angeles]"]
(.format top-of-hour fmt) => "2021-05-13 07:00:00.000"上面的代码基于this template project和库tupelo.java-time。
发布于 2021-05-13 21:47:51
LocalDate没有时间部分。.atStartOfDay在所有时间字段中都设置为零。LocalDateTime可以通过.toInstant转换为Instant。
https://stackoverflow.com/questions/67519949
复制相似问题