使用 Instant.parse 解析 2018-05-01T00:00:00 日期时出错

Error while parsing 2018-05-01T00:00:00 date using Instant.parse

这是我用来解析字符串的代码 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 以外的其他方法,所以只能寻找解决方案!

Instant.parse will only accept the string that are in ISO INSTANT FORMAT

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

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

但是你的String表示LocalDateTime,所以解析成LocalDateTime再转换成Instant

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();