0

I want to parse a date that includes AM/PM format from String to Timestamp.

Although when I try to parse this date: Dec 31 2017 12:00AM

I get a

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)

My code:

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);

Am I doing something wrong in the simpledateformat? "MMM dd yyyy hh:mmaa"?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Ioanna Katsanou
  • 484
  • 2
  • 7
  • 24

1 Answers1

2

The input date contains a word in english. You have to give the locale :

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa", Locale.ENGLISH);
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148