’03/03/2023 01:56′ 无法解析:无法从TemporalAccessor获取LocalDateTime

huangapple go评论69阅读模式
英文:

Text '03/03/2023 01:56' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor

问题

I get an unexpected exception when I try to parse a LocalDateTime (using JDK 8).

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");

String dateStr = LocalDateTime.now().format(DTF);
// => dateStr = 03/03/2023 01:56

LocalDateTime date = LocalDateTime.parse(dateStr, DTF);
// => throw DateTimeParseException

I don't understand why the parse method throws a DateTimeParseException whereas the format method works properly. I'm using the same DateTimeFormatter to format the date to string.

Exception in thread "main" java.time.format.DateTimeParseException: Text '03/03/2023 01:56' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=56, HourOfAmPm=1},ISO resolved to 2023-03-03 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.LocalDateTime.parse(LocalDateTime.java:492)

Do you have an idea?

I changed the pattern of DateTimeFormatter for hour-of-day (0-23).

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
=> This works fine.

Why doesn't it work with the clock-hour-of-am-pm (1-12)?

英文:

I get an unexpected exception when I try to parse a LocalDateTime (using JDK 8).

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");

String dateStr = LocalDateTime.now().format(DTF);
// => dateStr = 03/03/2023 01:56

LocalDateTime date = LocalDateTime.parse(dateStr, DTF);
// => throw DateTimeParseException

I don't undestand why the parse method throw a DateTimeParseException wheareas the format method work properly. I'm using the same DateTimeFormatter to format the date to string.

> Exception in thread "main" java.time.format.DateTimeParseException: Text '03/03/2023 01:56' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {MinuteOfHour=56, HourOfAmPm=1},ISO resolved to 2023-03-03 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.LocalDateTime.parse(LocalDateTime.java:492)

Do you have an idea ?

I changed the pattern of DateTimeFormatter for hour-of-day (0-23).

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
=> This work fine.

Why it doesn't work with the clock-hour-of-am-pm (1-12) ?

答案1

得分: 2

如@OH GOD SPIDERS所解释,我需要添加a来指定AM/PM,或者使用HH来指定24小时制。

要使用上午/下午的小时格式(1-12),请添加a

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mma");

要使用小时制(0-23),将hh更改为HH

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
英文:

As explained by @OH GOD SPIDERS, I needed to either add a to specify AM/PM or use HH to specify 24-hour format.

So to use the clock-hour-of-am-pm (1-12) format, add a:

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mma");

Or to use hour-of-day format (0-23), change hh to HH:

DateTimeFormatter DTF = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

huangapple
  • 本文由 发表于 2023年3月3日 22:03:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定