简体   繁体   English

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

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

Here is my code which i am using to parse string using Instant.parse,这是我用来使用 Instant.parse 解析字符串的代码,

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

And getting below error并低于错误

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)

I cannot use other then Instant so looking solution for it only!我不能使用其他Instant所以只能寻找解决方案!

Instant.parse will only accept the string that are in ISO INSTANT FORMAT Instant.parse将只接受ISO INSTANT 格式的字符串

Obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.从 2007-12-03T10:15:30.00Z 等文本字符串中获取 Instant 的实例。

The string must represent a valid instant in UTC and is parsed using DateTimeFormatter.ISO_INSTANT.该字符串必须表示 UTC 中的有效瞬间,并使用 DateTimeFormatter.ISO_INSTANT 进行解析。

But the String you have represents LocalDateTime , so parse it into LocalDateTime and then convert into 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. ISO-8601 日历系统中没有时区的日期时间,例如 2007-12-03T10:15:30。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何格式化此日期“ 2018-08-02T00:00:00 + 05:30”字符串为此“ 2018-18-02” - How to Format this Date “2018-08-02T00:00:00+05:30” String to this “2018-18-02” 如何在 -01:-05:00 到 -01:05:00 中制作时间格式 - how to make a time format in -01:-05:00 to -01:05:00 java:如何将日期从“ 2014年5月1日星期四00:00:00西部”投射到“ 2014-01-05 00:00:00.0” - java : How can I cast Date from “Thu May 01 00:00:00 WEST 2014 ” to “2014-01-05 00:00:00.0” 将字符串解析为时间使得01:00:00 - Parsing String to Time makes 01:00:00 为什么将“0000:00:00 00:00:00”解析为日期返回-0001-11-28T00:00:00Z? - Why does parsing “0000:00:00 00:00:00” into a Date return -0001-11-28T00:00:00Z? 在Android中将字符串转换为日期“ 2016-01-28T12:08:47.676706-05:00” - Convert String to Date “2016-01-28T12:08:47.676706-05:00” in android Java 8 LocalDateTime 在解析具有 00 秒的日期字符串值(如“2018-07-06 00:00:00”)时丢弃 00 秒值 - Java 8 LocalDateTime dropping 00 seconds value when parsing date string value with 00 seconds like "2018-07-06 00:00:00" 时间转换为util,Date后的时间为00:00:00的java 8给出了时间02:00:00 - java 8 instant with time 00:00:00 gives time 02:00:00 after conversion to util,Date 如何获取此字符串的 08:00 2015-01-01T08:00:00-02:00 - How to get 08:00 of this String 2015-01-01T08:00:00-02:00 什么是日期字符串的结尾,如2014-01-01T00:00:00.588Z - What is the Z ending on date strings like 2014-01-01T00:00:00.588Z
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM