Error: "Unable to obtain ZonedDateTime from TemporalAccessor" for format "yyyy-MM-dd hh:mm:ss z"
Error: "Unable to obtain ZonedDateTime from TemporalAccessor" for format "yyyy-MM-dd hh:mm:ss z"
我有一个输入数据流,其中包含 "yyyy-MM-dd hh:mm:ss z" 格式的日期,其中在进一步传播日期时需要保留时区。
下面是测试程序(带有错误堆栈)——我的解决方案有什么问题?
public class Test {
public static void main(String[] args) {
try {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
ZonedDateTime zdt = ZonedDateTime.parse("2016-12-09 09:30:21 UTC", dtf);
System.out.println(zdt);
} catch (Exception e) {
System.err.println("Exception in 1st approach: " + e.getMessage());
}
try {
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd hh:mm:ss z").toFormatter();
ZonedDateTime zdt = ZonedDateTime.parse("2016-12-09 09:30:21 UTC", dtf);
System.out.println(zdt);
} catch (Exception e) {
System.err.println("Exception in 2nd approach: " + e.getMessage());
}
}
}
Output (exception msg):
Exception in 1st approach: Text '2016-12-09 09:30:21 UTC' could not be
parsed: Unable to obtain ZonedDateTime from TemporalAccessor:
{HourOfAmPm=9, MinuteOfHour=30, MicroOfSecond=0, SecondOfMinute=21,
NanoOfSecond=0, MilliOfSecond=0},ISO,UTC resolved to 2016-12-09 of
type java.time.format.Parsed
Exception in 2nd approach: Text '2016-12-09 09:30:21 UTC' could not be
parsed: Unable to obtain ZonedDateTime from TemporalAccessor:
{HourOfAmPm=9, MinuteOfHour=30, MicroOfSecond=0, SecondOfMinute=21,
NanoOfSecond=0, MilliOfSecond=0},ISO,UTC resolved to 2016-12-09 of
type java.time.format.Parsed
JDK 版本 1.8.0_111
我已阅读 zoneddatetime 标签中的类似问题,但找不到问题的解决方案。
你应该使用
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
而不是使用
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
所以你的模式中的 h
必须是 H
。根据 documentation h
在您定义 clock-hour-of-am-pm
时使用,因此此外您还应该在您的模式中将 a
作为 am-pm-of-day
或者只使用 H
我有一个输入数据流,其中包含 "yyyy-MM-dd hh:mm:ss z" 格式的日期,其中在进一步传播日期时需要保留时区。 下面是测试程序(带有错误堆栈)——我的解决方案有什么问题?
public class Test {
public static void main(String[] args) {
try {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
ZonedDateTime zdt = ZonedDateTime.parse("2016-12-09 09:30:21 UTC", dtf);
System.out.println(zdt);
} catch (Exception e) {
System.err.println("Exception in 1st approach: " + e.getMessage());
}
try {
DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd hh:mm:ss z").toFormatter();
ZonedDateTime zdt = ZonedDateTime.parse("2016-12-09 09:30:21 UTC", dtf);
System.out.println(zdt);
} catch (Exception e) {
System.err.println("Exception in 2nd approach: " + e.getMessage());
}
}
}
Output (exception msg):
Exception in 1st approach: Text '2016-12-09 09:30:21 UTC' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {HourOfAmPm=9, MinuteOfHour=30, MicroOfSecond=0, SecondOfMinute=21, NanoOfSecond=0, MilliOfSecond=0},ISO,UTC resolved to 2016-12-09 of type java.time.format.Parsed
Exception in 2nd approach: Text '2016-12-09 09:30:21 UTC' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {HourOfAmPm=9, MinuteOfHour=30, MicroOfSecond=0, SecondOfMinute=21, NanoOfSecond=0, MilliOfSecond=0},ISO,UTC resolved to 2016-12-09 of type java.time.format.Parsed
JDK 版本 1.8.0_111
我已阅读 zoneddatetime 标签中的类似问题,但找不到问题的解决方案。
你应该使用
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
而不是使用
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss z");
所以你的模式中的 h
必须是 H
。根据 documentation h
在您定义 clock-hour-of-am-pm
时使用,因此此外您还应该在您的模式中将 a
作为 am-pm-of-day
或者只使用 H