【问题标题】:Java DateTime 12 hour format [duplicate]Java DateTime 12 小时格式 [重复]
【发布时间】:2020-11-09 20:00:14
【问题描述】:

我在 LocalDateTime 对象中传递日期和时间(12 小时格式)时遇到问题。 这是我所做的:

LocalDateTime MyDate;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm a");
    MyDate.parse(date_frome_string, formatter)

当我尝试过去的文本时:

2020-09-04 09:15 PM 

我有以下错误:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-09-04 09:15 PM' could not be parsed: Conflict found: Field AmPmOfDay 0 differs from AmPmOfDay 1 derived from 09:15

当我经过时:

2020-09-04 09:15 AM

一切都好。

【问题讨论】:

  • 您将解决这个问题,而且 - 您的代码应该准备好处理 DateTimeParseException 并可能 retry 解析其他格式,因为在您粘贴时你想要什么,人们尝试输入其他格式。

标签: java date time


【解决方案1】:

The format symbol HH explicitly means 24-hour time. 使用 hh 代替 12 小时时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多