0

I have date formatted in this way:

        2010-02-04T00:00:00

I use this code to parse it:

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-ddTHH:mm:ss");
        Date parsed = dateFormat.parse("2010-02-04T00:00:00");

It returns exception. The problem is in "T" letter. How can I parse this date if server returns it in such a format?

4 Answers 4

2
SimpleDateFormat("yyyy-mm-dd'T'HH:mm:ss");

one can read a bit more about it here Using Alphabetic Characters in SimpleDateFormat Pattern String

1

Simply use this string instead: "2010-02-04'T'00:00:00". The '-character escapes letters so they are not parsed as part of the date.

Or as the documentation of SimpleDateFormat explains it:

Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

1

try this one:

  dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

edit lowecase m stays per minute in hour. see the doc

1
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MMM dd'T' hh:mm:ss z yyyy");

Date myDate = sdf.parse(strDate);
sdf2.format(myDate);

System.out.println(myDate);

This will work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.