Reputation: 171
Error :
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern component: p at org.joda.time.format.DateTimeFormat.parsePatternTo(DateTimeFormat.java:559) at org.joda.time.format.DateTimeFormat.createFormatterForPattern(DateTimeFormat.java:682) at org.joda.time.format.DateTimeFormat.forPattern(DateTimeFormat.java:170) at com.myjavapapers.time.JodaTimeDemo.getDate(JodaTimeDemo.java:29) at com.myjavapapers.time.JodaTimeDemo.main(JodaTimeDemo.java:21)
System.out.println(getDate("09/16/14 1:07 PM", "MM/dd/yy hh:mm pm"));
public static Date getDate(final String dateTimeS, final String format) {
if (StringUtils.isEmpty(dateTimeS)) {
return null;
}
DateTimeFormatter fmt = DateTimeFormat.forPattern(format);
DateTime dateTime = fmt.parseDateTime(dateTimeS);
return dateTime.toDate();
}
Upvotes: 0
Views: 2210
Reputation: 11787
The error tells you that p
is not an allowed character for the parser. You have to use the component a
to parse the AM/PM string. That should work:
getDate("09/16/14 1:07 PM", "MM/dd/yy hh:mm a")
Take a look at the documentation for more explanation.
Upvotes: 2