Reputation: 10039
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
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
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
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
Reputation: 26991
Try..
final DateFormat dateFormatOut = new SimpleDateFormat("MMM, dd, yyyy");
Upvotes: 0