Java 中尴尬的日期格式

发布于 2025-01-04 07:52:52 字数 425 浏览 2 评论 0原文

我在解析这个日期时遇到问题:

Satu, 30 Octo 2010 06:00:00 EDT

我认为可以,

EEEE, dd MMMM yyyy HH:mm:ss Z

但它不起作用。我想将其格式化为

Saturday, October 30, 2010

我一直在查看 http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 作为我的资源

I'm having trouble parsing this date:

Satu, 30 Octo 2010 06:00:00 EDT

I think it would be

EEEE, dd MMMM yyyy HH:mm:ss Z

but it is not working. I would like to format it to

Saturday, October 30, 2010

I've been looking at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html as my resource

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浪漫人生路 2025-01-11 07:52:52

老实说,我会放弃 Satu, 并从那时起进行解析。您不需要它,并且始终可以使用Calendar 来计算星期/月份/年份。请参阅此处了解 StringUtils 是什么。

String dateString = "Satu, 30 Octo 2010 06:00:00 EDT";

Map<String, String> weirdMonthMap = new HashMap<String, String>();
weirdMonthMap.put("Janu", "Jan");
//...
weirdMonthMap.put("Octo", "Oct");

for (String key: weirdMonthMap.keySet()) {
    dateString = StringUtils.replace(dateString, key, weirdMonthMap.get(key));
}

String[] pieces = StringUtils.split(dateString, ',');
if (pieces.length != 2)
    throw new IllegalArgumentException("Whoa! " + dateString);

dateString = pieces[1];

SimpleDateFormat format = new SimpleDateFormat(" dd MMM yyyy HH:mm:ss z");

System.out.println( format.parse(dateString) );

Honestly, I'd ditch the Satu, and just parse from then on. You don't need it and can always use Calendar to figure out day of the week/month/year. See here for what StringUtils is.

String dateString = "Satu, 30 Octo 2010 06:00:00 EDT";

Map<String, String> weirdMonthMap = new HashMap<String, String>();
weirdMonthMap.put("Janu", "Jan");
//...
weirdMonthMap.put("Octo", "Oct");

for (String key: weirdMonthMap.keySet()) {
    dateString = StringUtils.replace(dateString, key, weirdMonthMap.get(key));
}

String[] pieces = StringUtils.split(dateString, ',');
if (pieces.length != 2)
    throw new IllegalArgumentException("Whoa! " + dateString);

dateString = pieces[1];

SimpleDateFormat format = new SimpleDateFormat(" dd MMM yyyy HH:mm:ss z");

System.out.println( format.parse(dateString) );
ま柒月 2025-01-11 07:52:52
Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number. 

MMM 并不意味着“前三个字母”。

MMM 和 MMMMMMMMMMMMMM 是相同的。

所以你的输入应该像“Oct”或“October”

Month: If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number. 

MMM doesn't mean "first three letter".

Both MMM and MMMMMMMMMMMMMM are same.

So your input should be like "Oct" or "October"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文