Suchi
Suchi

Reputation: 10039

How would I parse my date into the mm/dd/yy format?

I want to be able to parse this date-time format "2011-12-02T16:18:12.479-05:00" into an mm/dd/yy format. What is the best way to do that? I am trying to do -

final DateFormat dateFormatIn = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S'Z'");
final DateFormat dateFormatOut = new SimpleDateFormat("MMMMMMMMM d, yyyy");
String timestamp = dateFormatOut.format(dateFormatIn.parse(someDate);

But I get an exception 'java.text.ParseException: Unparseable date: "2011-12-02T16:18:12.479-05:00"' Any insights into what I could be doing wrong?

Upvotes: 0

Views: 672

Answers (4)

Cody S
Cody S

Reputation: 4824

If all else fails, joda-time joda-time is a pretty awesome time utility library that would make date formatting pretty straightforward. There are a few tutorials, as well as APIs, on their site. As for your ParseException, I believe that the answers above should be helpful :)

Upvotes: 0

ahodder
ahodder

Reputation: 11439

I believe your issue is because of the ' marks around Z.

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

Wrote up a quick script to test it. Works fine now.

Upvotes: 1

GETah
GETah

Reputation: 21449

Your input date 2011-12-02T16:18:12.479-05:00 contains milliseconds. Use the following instead

final DateFormat dateFormatIn = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

Upvotes: 1

coder_For_Life22
coder_For_Life22

Reputation: 26991

Try..

final DateFormat dateFormatOut = new SimpleDateFormat("MMM, dd, yyyy");

Upvotes: 0

Related Questions