如何将此字符串转换为java中的标准日期?

发布于 2024-09-24 18:44:05 字数 815 浏览 4 评论 0原文

早些时候我发布了以下问题: 如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-10-01 18:44:05

两者 SimpleDateFormatjoda-time DateTimeFormat 可以使用以下模式解析它:

String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";

例如:

Date date = new SimpleDateFormat(pattern).parse(dateString);

和 (joda-time):

DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(s);

更新

您涉及两种日期格式 - 一种用于解析输入,另一种用于格式化输出。所以:(

dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));

当然,为了优化,你可以只实例化一次SimpleDateFormat,然后重用它)

Both SimpleDateFormat and joda-time DateTimeFormat can parse this, using this pattern:

String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";

For example:

Date date = new SimpleDateFormat(pattern).parse(dateString);

And (joda-time):

DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
DateTime dateTime = dtf.parseDateTime(s);

Update

You have 2 date formats involved - one for parsing the input, and one for formatting the output. So:

dateText.setText(new SimpleDateFormat("MM/dd/yy 'at' hh:mma").format(date));

(Of course, for the sake of optimization, you can instantiate the SimpleDateFormat only once, and reuse it)

尴尬癌患者 2024-10-01 18:44:05

简而言之,您希望将字符串格式的日期转换为另一种字符串格式的日期。你有:

2010-03-15T16:34:46Z

并且你想要

03/15/10 at 4:34PM

你不想最终使用 java.util.Date 对象,正如您最初在问题中暗示的那样。您也不想使用其 toString() 因为它返回 其 javadoc

Bozho的答案仍然适用。使用 java.text.SimpleDateFormat 。首先,您需要将字符串格式的日期解析Date对象,以便您可以将其格式化为另一种字符串格式。

// First parse string in pattern "yyyy-MM-dd'T'HH:mm:ss'Z'" to date object.
String dateString1 = "2010-03-15T16:34:46Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(dateString1);

// Then format date object to string in pattern "MM/dd/yy 'at' h:mma".
String dateString2 = new SimpleDateFormat("MM/dd/yy 'at' h:mma").format(date);
System.out.println(dateString2); // 03/15/10 at 4:34PM

In a nutshell, you want to convert a date in a string format to a date in another string format. You have:

2010-03-15T16:34:46Z

and you want

03/15/10 at 4:34PM

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 its toString() 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 a Date object so that you can format it back into another string format.

// First parse string in pattern "yyyy-MM-dd'T'HH:mm:ss'Z'" to date object.
String dateString1 = "2010-03-15T16:34:46Z";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(dateString1);

// Then format date object to string in pattern "MM/dd/yy 'at' h:mma".
String dateString2 = new SimpleDateFormat("MM/dd/yy 'at' h:mma").format(date);
System.out.println(dateString2); // 03/15/10 at 4:34PM
玻璃人 2024-10-01 18:44:05

如果要格式化输出字符串,请将代码中的以下行更改

dateText.setText(date.toString());

dateText.setText(String.format("%1$tm/%1$td/%1$ty at %1$tl:%1$tM%1$Tp", date));

If you want to format output string, change following line in your code

dateText.setText(date.toString());

to

dateText.setText(String.format("%1$tm/%1$td/%1$ty at %1$tl:%1$tM%1$Tp", date));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文