Reputation: 87
I'm running in a problem when i'm trying to convert a String to a java.util.Date.
My date is formatted like that : Jan 23 2016 4:00:24 PM and I want to convert that String to a Date object. So to do that i'm using a SimpleDateFormat with the parameters that seems good to me according to the javadoc : MMM dd yyyy aa.
But when i'm running my code i'm encountering a java.text.ParseException: Unparseable date: "Jan 23 2016 4:00:24 PM"
What could be my problem ?
Thanks !
EDIT : the code in question
String dateStr = "Jan 23 2016 4:00:24 PM";
SimpleDateFormat parserSDF = new SimpleDateFormat("MMM dd yyyy hh:mm:ss aa");
Date date = parserSDF.parse(dateStr);
Upvotes: 1
Views: 502
Reputation: 522
This code runs fine for me, and produces the output Sat Jan 23 16:00:24 EST 2016
String text = "Jan 23 2016 4:00:24 PM";
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy hh:mm:ss aa", Locale.US);
try {
System.out.println(sdf.parse(text));
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1
Reputation: 2891
What is your default system locale? You can check it with Locale.getDefault()
method call. I've tried your code, works fine for me with default en_US
locale. But I tried to use another one (I used Chinese) and caught the same error.
Try to use SimpleDateFormat parserSDF = new SimpleDateFormat("MMM dd yyyy hh:mm:ss aa", Locale.US);
with explicitly defined locale
Upvotes: 2