如何在Java中将String AM / PM日期转换为Timestamp?

时间:2019-05-23 08:49:35

标签: java date java-7

我想解析一个包含AM / PM格式的日期,从字符串到时间戳。

尽管我尝试解析此日期: 2017年12月31日12:00 AM

我得到

Exception in thread "main" java.text.ParseException: Unparseable date: "Dec 31 2017 12:00AM"
    at java.text.DateFormat.parse(Unknown Source)
    at test.DatteTest.main(DatteTest.java:16)

我的代码:

String endDate = "Dec 31 2017 12:00AM";         
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa");
Date parsedDate = dateFormat.parse(endDate);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
System.out.println(endDate);
System.out.println(timestamp);

我在simpledateformat中做错什么了吗? “ MMM dd yyyy hh:mmaa”?

1 个答案:

答案 0 :(得分:2)

输入日期包含一个英文单词。您必须提供语言环境:

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa", Locale.ENGLISH);
相关问题