简体   繁体   English

我如何将我的日期解析为mm / dd / yy格式?

[英]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. 我希望能够将此日期时间格式“2011-12-02T16:18:12.479-05:00”解析为mm / dd / yy格式。 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? 但我得到一个例外'java.text.ParseException:Unparseable date:“2011-12-02T16:18:12.479-05:00”'对我可能做错的任何见解?

I believe your issue is because of the ' marks around Z . 我相信你的问题是因为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. 您输入的日期2011-12-02T16:18:12.479-05:00包含毫秒。 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. 如果所有其他方法都失败了,joda-time joda-time是一个非常棒的时间实用程序库,可以使日期格式化非常简单。 There are a few tutorials, as well as APIs, on their site. 他们的网站上有一些教程和API。 As for your ParseException, I believe that the answers above should be helpful :) 至于你的ParseException,我相信上面的答案应该是有帮助的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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