如何将“20201220T00:00:00.000Z”转换为java。util。日期
我试着用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(d.toString(), formatter);
但它抛出了一个错误
有没有办法转换JSON默认时间戳
你可以在下面搜索框中键入要查询的问题!
我试着用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
LocalDate date = LocalDate.parse(d.toString(), formatter);
但它抛出了一个错误
有没有办法转换JSON默认时间戳
# 1 楼答案
这似乎是默认格式,请尝试此格式
# 2 楼答案
您不需要
DateTimeFormatter
来解析日期时间字符串将给定的日期字符串直接解析为
OffsetDateTime
。现代日期时间API基于ISO 8601,只要日期时间字符串符合ISO 8601标准,就不需要显式使用DateTimeFormatter
对象。日期时间字符串中的^{cd4>}是零时区偏移的timezone designator。它代表Zulu并指定Etc/UTC
时区(其时区偏移量为+00:00
小时)将
OffsetDateTime
转换为Instant
使用^{} 将} 代表时间线上的一个瞬时点。它独立于时区,因此始终以UTC为单位
OffsetDateTime
转换为Instant
。一个^{停止使用旧的日期时间API
随着2014年3月Java SE 8的发布,过时且容易出错的遗留日期时间API(} 获取它
java.util
日期时间类型及其格式类型,SimpleDateFormat
等)被java.time
取代,即modern date-time API*。强烈建议停止使用旧API,并切换到这个新API。如果需要java.util.Date
,请使用^{注意
java.util.Date
对象不是像modern date-time types那样的实时对象;相反,它表示自被称为“历元”的标准基准时间(即January 1, 1970, 00:00:00 GMT
(或UTC))以来的毫秒数。打印java.util.Date
的对象时,其toString
方法返回JVM时区中的日期时间,根据该毫秒值计算。如果需要在不同的时区打印日期时间,则需要将时区设置为SimpleDateFormat
,并从中获取格式化字符串,例如:可以将
Instant
转换为其他日期时间类型您可以轻松地将
Instant
转换为其他日期时间类型,例如,如果您想将其转换为表示伦敦日期时间的ZonedDateTime
实例,您可以按以下方式进行转换:LocalDateTime
对你来说没用Quoted below是对
LocalDateTime
用法的一个很好的描述:日期时间字符串的最佳用途是作为
OffsetDateTime
,这是您在第一步中获得的演示:
输出:
了解更多关于^{cd15>},来自Trail: Date Time的modern date-time API*
*无论出于何种原因,如果您必须坚持使用Java 6或Java 7,您可以使用ThreeTen-Backport来支持大部分Java。Java 6&;的时间功能;7.如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请选中Java 8+ APIs available through desugaring和How to use ThreeTenABP in Android Project。
# 3 楼答案
关于如何将
LocalDate
转换为java.util.Date
的问题,可以使用Date.from
方法,如下所示。让我知道这是否是你期望实现的目标# 4 楼答案
你需要使用
LocalDateTime
# 5 楼答案
{}是祖鲁时区(即UTC时区),而不是字面上的Z
整个格式是ISO-8601即时格式
有一个预先存在的格式化程序:DateTimeFormatter#ISO_INSTANT
摘自javadoc: