本文介绍了解析日期"2020-05-22T12:51:20.765111Z"用Java即时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中将"2020-05-22T12:51:20.732111Z" 解析为Instant?

How to parse "2020-05-22T12:51:20.732111Z" to Instant in Java?

我用过:

LocalDateTime.parse(
              startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US))
          .atZone(ZoneId.of("America/Toronto"))
          .toInstant()

但有错误:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-05-22T12:51:20.732111Z' could not be parsed at index 24
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at Instant.Main.main(Main.java:54)

推荐答案

使用 Instant#parse(String):

Instant i = Instant.parse("2020-05-22T12:51:20.732111Z");

如果您想将其转换为您的时区:

And if you want to convert it to your timezone:

ZonedDateTime z = i.atZone(ZoneId.of("America/Toronto"));

打印它会产生:

2020-05-22T08:51:20.732111-04:00[America/Toronto]

这篇关于解析日期"2020-05-22T12:51:20.765111Z"用Java即时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 16:14