如何将此字符串转换为java中的标准日期?
早些时候我发布了以下问题: 如何在 Java 中转换此日期?
但现在我想知道如何将此字符串转换为日期/时间。
2010-03-15T16:34:46Z
例如: 03/15/10
更新:
String pattern = "MM/dd/yy 'at' HH:mm";
Date date = new Date();
try {
date = new SimpleDateFormat(pattern).parse(q.getUpdated_at());
} catch (ParseException e) {
e.printStackTrace();
}
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
给我一个如下结果:
Mon Mar 15 16:34:50 MST 2010
如何将其格式化为
03/15/10 at 4:34PM
?
Earlier I posted the following question: How can I convert this date in Java?
But now I would like to know how I can convert this string into a date/time.
2010-03-15T16:34:46Z
For example: 03/15/10
UPDATED:
String pattern = "MM/dd/yy 'at' HH:mm";
Date date = new Date();
try {
date = new SimpleDateFormat(pattern).parse(q.getUpdated_at());
} catch (ParseException e) {
e.printStackTrace();
}
dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));
Gives me a result like:
Mon Mar 15 16:34:50 MST 2010
How can I format it to be
03/15/10 at 4:34PM
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两者
SimpleDateFormat
和 joda-timeDateTimeFormat
可以使用以下模式解析它:例如:
和 (joda-time):
更新
您涉及两种日期格式 - 一种用于解析输入,另一种用于格式化输出。所以:(
当然,为了优化,你可以只实例化一次
SimpleDateFormat
,然后重用它)Both
SimpleDateFormat
and joda-timeDateTimeFormat
can parse this, using this pattern:For example:
And (joda-time):
Update
You have 2 date formats involved - one for parsing the input, and one for formatting the output. So:
(Of course, for the sake of optimization, you can instantiate the
SimpleDateFormat
only once, and reuse it)简而言之,您希望将字符串格式的日期转换为另一种字符串格式的日期。你有:
并且你想要
你不想最终使用
java.util.Date
对象,正如您最初在问题中暗示的那样。您也不想使用其toString()
因为它返回 其 javadoc。Bozho的答案仍然适用。使用
java.text.SimpleDateFormat
。首先,您需要将字符串格式的日期解析为Date
对象,以便您可以将其格式化为另一种字符串格式。In a nutshell, you want to convert a date in a string format to a date in another string format. You have:
and you want
You don't want to end up using
java.util.Date
object as you initially implied in your question. You also don't want to use itstoString()
since that returns a fixed format as definied in its javadoc.The answer of Bozho still applies. Use
java.text.SimpleDateFormat
. First, you need to parse the date in string format into aDate
object so that you can format it back into another string format.如果要格式化输出字符串,请将代码中的以下行更改
为
If you want to format output string, change following line in your code
to