英文:
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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论