简体   繁体   中英

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?

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.

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");

尝试..

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM