coder1608
coder1608

Reputation: 171

Illegal pattern component: p while dealing with DATE

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

Answers (1)

LaurentG
LaurentG

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

Related Questions