如何使用 ThreeTenABP 将 java.util.Date 更改为 ISO 字符串
How can I change java.util.Date to ISO String using ThreeTenABP
我正在使用 ThreeTenABP 转换 Android 的日期时间。
我的问题是如何通过 ThreeTenABP 将 java.util.Date
更改为 ISO 字符串(格式为 2018-05-24T02:33:10.062Z
)?
我不知道 android,但如果 java.text.SimpleDateFormat
可用,你可以这样做:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(new Date())
一个 ThreetenABP-solution 可以是这样的:
java.util.Date d = ...;
org.threeten.bp.Instant instant = org.threeten.bp.DateTimeUtils.toInstant(d);
String iso = instant.toString();
如果您希望更好地控制格式,则可以将即时转换为 ZonedDateTime
(或更好的 to an OffsetDateTime)并使用专用的 DateTimeFormatter
.
表达式:
new Date().toInstant().toString()
结果
2019-12-31T06:32:37.997Z
我正在使用 ThreeTenABP 转换 Android 的日期时间。
我的问题是如何通过 ThreeTenABP 将 java.util.Date
更改为 ISO 字符串(格式为 2018-05-24T02:33:10.062Z
)?
我不知道 android,但如果 java.text.SimpleDateFormat
可用,你可以这样做:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(new Date())
一个 ThreetenABP-solution 可以是这样的:
java.util.Date d = ...;
org.threeten.bp.Instant instant = org.threeten.bp.DateTimeUtils.toInstant(d);
String iso = instant.toString();
如果您希望更好地控制格式,则可以将即时转换为 ZonedDateTime
(或更好的 to an OffsetDateTime)并使用专用的 DateTimeFormatter
.
表达式:
new Date().toInstant().toString()
结果
2019-12-31T06:32:37.997Z