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

我在将字符串格式化为ZonedDateTime时遇到麻烦。

我的客户希望使用ddMMyyyyhhmmss之类的日期作为日期,而没有分隔符或类似的内容。

这是我到目前为止所做的

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变量的解析过程中会发生错误

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的方式

DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")

.toFormatter()

.withZone(ZoneId.systemDefault());

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值