Java ZoneDateTime分析异常

Java ZoneDateTime分析异常,java,date,parsing,datetime,zoneddatetime,Java,Date,Parsing,Datetime,Zoneddatetime,我正在尝试将字符串转换为ZoneDateTime 我尝试过以下方法: SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z"); zonedDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT")); long timeMs = zonedDateTimeFormat.parse("2017-07-18T20:2

我正在尝试将字符串转换为ZoneDateTime

我尝试过以下方法:

SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");   
zonedDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 

long timeMs = zonedDateTimeFormat.parse("2017-07-18T20:26:28.582+03:00[Asia/Istanbul]").getTime();
它给出java.text.ParseException:不可解析的日期

如何将以下字符串解析为ZoneDateTime

2017-07-18T20:26:28.582+03:00[Asia/Istanbul]

似乎是为处理您提供的字符串而设计的。对于ZonedDateTime,不需要检查旧的SimpleDateFormat,我们需要将ZonedDateTime.parse方法与DateTimeFormatter一起使用。如果我没有错,您有ISO日期:

您可以使用或。两者都能够解析带有偏移量和区域的日期时间。

java.time API有许多内置格式,可以简化解析和格式化过程。您试图解析的字符串是标准格式。因此,您可以通过以下方式轻松解析它,然后从历元中获取毫秒数:

DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse(
                        "2017-07-18T20:26:28.582+03:00[Asia/Istanbul]", 
                        formatter);  // prints 2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
long timeInMs = zdt.toInstant().toEpochMilli();

不幸的是,你错了。由于方括号中的尾随区域标识符,它不是ISO格式,但仍被称为ISO_u。。。在API中,带有javadoc中的小说明:方括号中的部分不是ISO-8601标准的一部分。@MenoHochschild非常感谢您的澄清。我不确定。
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse(
                        "2017-07-18T20:26:28.582+03:00[Asia/Istanbul]", 
                        formatter);  // prints 2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
long timeInMs = zdt.toInstant().toEpochMilli();