使用Instant.parse解析2018-05-01T00:00:00日期时出错 [英] Error while parsing 2018-05-01T00:00:00 date using Instant.parse

查看:415
本文介绍了使用Instant.parse解析2018-05-01T00:00:00日期时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来使用Instant.parse解析字符串的代码,

Here is my code which i am using to parse string using Instant.parse ,

String date = "2018-05-01T00:00:00";
Instant.parse(date)

并出现错误

java.time.format.DateTimeParseException: Text '2018-05-01T00:00:00' could not be parsed at index 19
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.Instant.parse(Instant.java:395)

除了 Instant (即时)之外,我不能再使用它,因此只能寻求解决方案!

I cannot use other then Instant so looking solution for it only!

推荐答案

从诸如2007-12-03T10:15:30.00Z之类的文本字符串中获取Instant实例.

Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.

该字符串必须表示UTC中的有效时刻,并使用DateTimeFormatter.ISO_INSTANT进行解析.

The string must represent a valid instant in UTC and is parsed using DateTimeFormatter.ISO_INSTANT.

但是您拥有的字符串表示 LocalDateTime ,因此将其解析为LocalDateTime,然后转换为Instant

But the String you have represents LocalDateTime, so parse it into LocalDateTime and then convert into Instant

ISO-8601日历系统中没有时区的日期时间,例如2007-12-03T10:15:30.

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

LocalDateTime dateTime = LocalDateTime.parse(date);
Instant instant = dateTime.atZone(ZoneId.of("America/New_York")).toInstant();

这篇关于使用Instant.parse解析2018-05-01T00:00:00日期时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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