有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

simpledateformat Java简单日期格式无法正确使用PM分析日期时间

我有以下代码来解析和格式化日期:

          SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("MM/dd/yyyy HH:mm a");
          System.out.println("Before Format  " + dateTimeFormatter.parse("06/19/2018 11:17 PM"));
          System.out.println(dateTimeFormatter.format(dateTimeFormatter.parse("06/19/2018 11:17 PM")));

其输出如下所示:

Before Format  Tue Jun 19 11:17:00 IST 2018
06/19/2018 11:17 AM

为什么PM是打印机中的AM


共 (1) 个答案

  1. # 1 楼答案

    当您想要以AM/PM格式显示时间时,使用12小时时钟(hh)而不是24小时时钟(HH)是很重要的

    而不是MM/dd/yyyy HH:mm a格式需要是MM/dd/yyyy hh:mm a

    有关SimpleDateFormat的更多信息,请查看文档here