DateUtils.parseDate exception

17.4k views Asked by At

I can't seem to get this to work, it says Unknown pattern character - "T"

Unable to parse the date 2011-07-22T12:01:34.9455820

Date theDate = DateUtils.parseDate(notif.dateStr, new String[]{"yyyy-MM-ddTHH:mm:ss.S"});

This won't work either(which is what I was doing first, and in this case it just gave a parse exception):

Date theDate = DateUtils.parseDate(notif.dateStr);

Where is my error?

2

There are 2 answers

0
sottitron On

Your error is that you are not providing a pattern that matches the format.

DateUtils JavaDoc api-3.3.2

parseDate

public static Date parseDate(String str,
                             String[] parsePatterns)
                      throws ParseException

Parses a string representing a date by trying a variety of different parsers.

The parse will try each parse pattern in turn. A parse is only deemed sucessful
if it parses the whole of the input string. If no parse patterns match, 
a ParseException is thrown.
3
CoolBeans On

Try yyyy-MM-dd'T'HH:mm:ss.S as the format pattern. Example:-

public static void main(String[] args) {
        String date = "2011-07-22T12:01:34.9455820";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
        try {
            Date dt = format.parse(date);
            System.out.println(dt.toString()); //prints Fri Jul 22 14:39:09 CDT 2011
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }