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 楼答案
当您想要以
AM/PM
格式显示时间时,使用12小时时钟(hh
)而不是24小时时钟(HH
)是很重要的而不是
MM/dd/yyyy HH:mm a
格式需要是MM/dd/yyyy hh:mm a
有关
SimpleDateFormat
的更多信息,请查看文档here