使用时区偏移值解析 ZonedDateTime 字符串 编辑

问题描述

我正在尝试使用 zoneddatetime 对象的以下行解析格式为“yyyy-MM-dd'T'HH:mm:ssZ”的 ISO8601 时间字符串:

zoneddatetime date = zoneddatetime.parse("2021-02-19T14:32:12+0000",DateTimeFormatter.ISO_ZONED_DATE_TIME);

但是,我在执行此操作时遇到以下错误

java.time.format.DateTimeParseException: Text '2021-02-19T14:32:12+0000' Could not be parsed at index 19

我无法想象这是不允许的,因为 + 符号是解析的有效字符。任何人都可以协助解决这里的问题吗?

解决方法

那是因为 CannotResolveClassException: com.gslab.pepper.config.plaintext.PlainTextConfigElement ---- Debugging information ---- cause-exception : com.thoughtworks.xstream.converters.ConversionException cause-message : first-jmeter-class : org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:66) class : org.apache.jmeter.save.ScriptWrapper required-type : org.apache.jmeter.save.ScriptWrapper converter-type : org.apache.jmeter.save.ScriptWrapperConverter 需要偏移量和区域。 请参阅编辑,并非在所有情况下都需要。这是因为偏移量必须包含一个冒号。

the docs

改用 ISO_ZONED_DATE_TIME。或者,您可以使用 DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxx",Locale.ROOT):

DateTimeFormatterBuilder

编辑

仅阅读 Javadoc 的摘要片段显然是不够的。如注释中所述,new DateTimeFormatterBuilder() .append(DateTimeFormatter.ISO_DATE_TIME) .appendPattern("xx") .toFormatter(Locale.ROOT); 并不严格要求区域 ID。那里的第二个要点提到了它:“如果区域 ID 不可用或者是 ZoneOffset,则格式是完整的”

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...