java本地日期时间解析
我正在尝试将输入字符串解析为本地日期时间
下面是我的一段代码
ZonedDateTime z = ZonedDateTime.parse("2019-11-26T19:30:00Z", MY_DATE_TIME_FORMATTER);
在哪里
MY_DATE_TIME_FORMATTER= new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.append(ISO_LOCAL_TIME)
.appendLiteral('Z')
.appendOffset("+HH:mm", "+0000")
.toFormatter();
我得到以下例外
爪哇。时间总体安排DateTimeParseException:无法在索引19处分析文本“2019-11-26T19:30:00Z”
你能告诉我我做错了什么吗
# 1 楼答案
Instant.parse
不需要格式化模式
您的输入格式符合ISO 8601标准。该特定格式的末尾有一个
Z
。这个字母的意思是UTC(零小时分秒的offset),发音为“祖鲁”java中的
Instant
类。时间表示UTC中的一个时刻,始终为UTC对该输入使用
ZonedDateTime
类不是最合适的。我们有:要查看调整为特定区域(时区)的人使用的偏移量的同一时刻,请应用
ZoneId
获取ZonedDateTime
对象见此code run live at IdeOne.com
# 2 楼答案
加
而不是这两行:
整个生成器示例:
另外,在您的具体情况下,我宁愿使用标准ISO格式化程序(如Hristo所述):
此外,即使没有显式格式化程序,
ZonedDateTime::parse
方法也可以工作。因为它是默认使用的:# 3 楼答案
使用内置的ISO分区时间格式化程序