ParseExeption inconsistent with Java data format

I wrote a simple Util method to convert Stringto Java on util.Date. What I cannot understand is why the method works for the first input and does not work for the second, given that the inputs are identical:

the code:

package util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

    public Date getDateFromString(String strDate, String dateFormat) {

        DateFormat df = new SimpleDateFormat(dateFormat);
        Date date = null;

        try {

            date = df.parse(strDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(date);

        return date;
    }

    public static void main(String[] args)  {

        StringToDate s2d = new StringToDate();

        s2d.getDateFromString("12-18-11, 10:36 AM","MM-dd-yy, hh:mm a");
        s2d.getDateFromString("02-04-12, 01:17 PM","MM-dd-yy, hh:mm a");
    }
}

Conclusion:

Sun Dec 18 10:36:00 CET 2011
null
java.text.ParseException: Unparseable date: "02-04-12, 01:17 PM"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at util.StringToDate.getDateFromString(StringToDate.java:17)
    at util.StringToDate.main(StringToDate.java:33)

Logically, the exit was Sat Feb 04 13:17:00 CET 2012to be the first exit. Why is chosen ParseException?

EDIT . The following two lines work correctly:

s2d.getDateFromString("02-04-12", "MM-dd-yy");
s2d.getDateFromString("01:17 PM", "hh:mm a");

Conclusion:

Sat Feb 04 00:00:00 CET 2012
Thu Jan 01 13:17:00 CET 1970

But an exception happens when I try to parse a date and time together.

+5
source share
1 answer

Do you have inextricable space or some other Unicode space character somewhere in your date string or format mask?

, , Unicode character 160.

+7

All Articles