java 为什么这个 SimpleDateFormat 不能解析这个日期字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2603638/
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-10-29 22:00:14  来源:igfitidea点击:

Why can't this SimpleDateFormat parse this date string?

javaparsingdatesimpledateformat

提问by Matt Ball

The SimpleDateFormat:

简单日期格式:

SimpleDateFormat pdf = new SimpleDateFormat("MM dd yyyy hh:mm:ss:SSSaa");

The exception thrown by pdf.parse("Mar 30 2010 5:27:40:140PM");:

抛出的异常pdf.parse("Mar 30 2010 5:27:40:140PM");

java.text.ParseException: Unparseable date: "Mar 30 2010 5:27:40:140PM"

Any ideas?

有任何想法吗?



Edit:thanks for the fast answers. You were all correct, I just missed that one key sentence in the SimpleDateFormat docs - I should probably call it a day.

编辑:感谢您的快速回答。你们都是对的,我只是错过了 SimpleDateFormat 文档中的一个关键句子——我应该收工了。

回答by BalusC

First, three-char months are to be represented by MMM. Second, one-two digit hours are to be represented by h. Third, Marseems to be English, you'll need to supply a Locale.ENGLISH, else it won't work properly in machines with a different default locale.

首先,三个字符的月份由 表示MMM。其次,一位两位数的小时由 表示h。第三,Mar似乎是英语,你需要提供一个Locale.ENGLISH,否则它在具有不同默认语言环境的机器上将无法正常工作。

The following works:

以下工作:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy h:mm:ss:SSSa", Locale.ENGLISH);
System.out.println(sdf.parse("Mar 30 2010 5:27:40:140PM"));

Result (I'm at GMT-4 w/o DST):

结果(我在 GMT-4,没有 DST):

Tue Mar 30 17:27:40 BOT 2010

Also see the java.text.SimpleDateFormatjavadoc.

另请参阅java.text.SimpleDateFormatjavadoc

Why you called it pdfis beyond me, so I renamed it sdf;)

为什么你叫它pdf我无法理解,所以我重命名了它sdf;)

回答by Roman

From SimpleDateFormat javadocs:

SimpleDateFormat javadocs

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

月份:如果模式字母的数量为 3 个或更多,则将月份解释为文本;否则,它被解释为一个数字。

Try to use pattern like "MMM dd yyyy"

尝试使用像“MMM dd yyyy”这样的模式

回答by Eyal Schneider

MM stands for numeric month. Use MMM.

MM 代表数字月份。使用 MMM。

回答by Ole V.V.

java.time

时间

I am providing the modern answer. The other answers are correct, but the SimpleDateFormatclass that you used is notoriously troublesome and long outdated. Instead I am using java.time, the modern and superior Java date and time API.

我正在提供现代答案。其他答案是正确的,但是SimpleDateFormat您使用的课程是出了名的麻烦且已过时。相反,我使用的是 java.time,现代和高级的 Java 日期和时间 API。

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
            "MMM d uuuu h:mm:ss:SSSa", Locale.ROOT);
    String dateTimeString = "Mar 30 2010 5:27:40:140PM";
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeString, formatter);
    System.out.println(dateTime);

Output:

输出:

2010-03-30T17:27:40.140

2010-03-30T17:27:40.140

Link:Oracle tutorial: Date Timeexplaining how to use java.time.

链接:Oracle 教程:解释如何使用 java.time 的日期时间