java.time.format.DateTimeParseException:无法以格式ddMMyyyyhhmmss从TemporalAccessor获取ZonedDateTime

编程入门 行业动态 更新时间:2024-10-11 07:35:38
本文介绍了java.time.format.DateTimeParseException:无法以格式ddMMyyyyhhmmss从TemporalAccessor获取ZonedDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在将字符串格式化为ZonedDateTime时遇到麻烦. 我的客户希望使用ddMMyyyyhhmmss这样的日期作为日期,而没有分隔符或类似的内容. 这是我到目前为止所做的

I'm having trouble in formatting a string to a ZonedDateTime. My customer wants to have the date in a format such as ddMMyyyyhhmmss, with no separators or stuff like that. This is what I've done so far

import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class MyClass { public static void main(String args[]) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyyhhmmss"); String test = formatter .format(ZonedDateTime.now()).toString(); System.out.println(test); ZonedDateTime a = ZonedDateTime.parse(test,formatter); System.out.println(a.toString()); } }

虽然正确生成了字符串,但在创建LocalDateTime变量的解析过程中发生了错误

While it correctly produces the string, the error occurs at the parsing process for creating the LocalDateTime variable

28032019100707 Exception in thread "main" java.time.format.DateTimeParseException: Text '28032019100707' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855) at java.time.ZonedDateTime.parse(ZonedDateTime.java:597) at MyClass.main(MyClass.java:14) Caused by: java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed at java.time.ZonedDateTime.from(ZonedDateTime.java:565) at java.time.format.Parsed.query(Parsed.java:226) at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851) ... 2 more Caused by: java.time.DateTimeException: Unable to obtain ZoneId from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed at java.time.ZoneId.from(ZoneId.java:466) at java.time.ZonedDateTime.from(ZonedDateTime.java:553) ... 4 more Command exited with non-zero status 1

在SO上进行搜索时,我发现针对同一问题的一些答案建议使用LocalDateTime类作为中间对象,然后解析为ZonedDateTime,但它仍然无法正常工作,并抛出相同的错误. 我还尝试通过此过程更改初始化DateTimeFormatter的方式

Searching on SO, I saw that some answers to the same issue suggested to use the LocalDateTime class as an intermediate and then parse to ZonedDateTime but it is still not working, throwing the same error. I've also tried in changing the way I initialize a DateTimeFormatter with this procedure

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss") .toFormatter() .withZone(ZoneId.systemDefault());

但是它仍然无法正常工作.我知道我肯定会丢失一些愚蠢的东西,但是我看不到.有人能指出我正确的方向吗?

But it is still not working. I know that I am surely missing something stupid but I can't see what. Can anybody point me in the right direction?

推荐答案

您想要的:

String test = ZonedDateTime.now().format(formatter);

更多推荐

java.time.format.DateTimeParseException:无法以格式ddMMyyyyhhmmss从TemporalAccessor获取Zo

本文发布于:2023-11-25 06:42:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628714.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   format   java   time   DateTimeParseException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!