how to parse a Date string to java.Date

Posted by hguser on Stack Overflow See other posts from Stack Overflow or by hguser
Published on 2010-04-08T02:00:06Z Indexed on 2010/04/08 2:03 UTC
Read the original article Hit count: 750

Filed under:
|
|

Hi:

I have a date string and I wang to parse it to normal date use the java Date API,the following is my code:

    public static void main(String[] args) {
    String date="2010-10-02T12:23:23Z";
    String pattern="yyyy-MM-ddThh:mm:ssZ";
    SimpleDateFormat sdf=new SimpleDateFormat(pattern);
    try {
        Date d=sdf.parse(date);
        System.out.println(d.getYear());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

However I got a exception:java.lang.IllegalArgumentException: Illegal pattern character 'T'

So I wonder if i have to split the string and parse it manually?

BTW, I have tried to add a single quote character on either side of the T:

String pattern="yyyy-MM-dd'T'hh:mm:ssZ";

It also does not work.

© Stack Overflow or respective owner

Related posts about java

Related posts about date