2

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
spentak
  • 4,627
  • 15
  • 62
  • 90

2 Answers2

11

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();
        }

    }
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • did this solve the original posters problem? simply putting quotes around the T character? – topwik Aug 29 '12 at 22:18
  • @towpse - yes it should have. The OP didn't accept, but it's a common problem and has this simple resolution. – CoolBeans Aug 30 '12 at 01:14
  • so what if a date format I'm trying to work with looks like this MM/dd/yyyy hh:mm:ss tt? I think the tt is from the dot net world to specify am or pm pattern at the end of a timestamp? – topwik Nov 21 '12 at 19:42
0

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.
Crypth
  • 1,576
  • 18
  • 32
sottitron
  • 73
  • 1
  • 5