我有以下代码来解析和格式化日期:
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?
最佳答案
当你想显示AM/PM
中的时间时格式,重要的是使用 12 小时时钟 ( hh
) 而不是 24 小时时钟 ( HH
)。
而不是 MM/dd/yyyy HH:mm a
格式需要为MM/dd/yyyy hh:mm a
有关SimpleDateFormat
的更多信息查看文档 here
关于Java简单日期格式无法正确解析PM的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50927169/