Java “EEEE,MMMM d,yyyy 'at' HH:mm”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18727755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:31:48  来源:igfitidea点击:

"EEEE, MMMM d, yyyy 'at' HH:mm"

javasimpledateformat

提问by andi-andi

I am trying to convert from string to date. An example is : Wednesday, September 4, 2013 at 5:07pm

我正在尝试从字符串转换为日期。例如:2013 年 9 月 4 日,星期三,下午 5:07

I first convert it to: Wednesday, September 4, 2013 at 5:07 PM

我首先将其转换为:2013 年 9 月 4 日,星期三,下午 5:07

And use the following code:

并使用以下代码:

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");

But I always get the Unparseable date Exception.

但我总是得到无法解析的日期异常。

Any help is appreciated!

任何帮助表示赞赏!

采纳答案by LaurentG

The following works for me:

以下对我有用:

String str = "Wednesday, September 4, 2013 at 5:07 PM";
SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a", Locale.US);

But if I remove the Locale, then I get a ParseException. Your computer Localecorresponds probably not to a english speaking locale.

但是如果我删除了Locale,那么我会得到一个ParseException. 您的计算机Locale可能不对应于讲英语的语言环境。

回答by tmh

This seems to work fine:

这似乎工作正常:

SimpleDateFormat in = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mma");
Date date = in.parse("Wednesday, September 4, 2013 at 5:07pm");
SimpleDateFormat out = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");
String result = out.format(date);
System.out.println(result);

回答by Mohsen Abasi

Why not use 'dd' instead of 'd'

为什么不使用“ dd”而不是“ d

So, instead of:

所以,而不是:

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM d, yyyy 'at' hh:mm a");

Use:

用:

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, yyyy 'at' hh:mm a");