Java 日期格式

2022-01-15 00:00:00 format date date-format java

有 String str "May 23 2011 12:20:00",想把它转换成这样的日期:

Have String str "May 23 2011 12:20:00", want to convert it to date such this:

Date date = (new SimpleDateFormat("MMM dd yyyy HH:mm:ss")).parse(str);

它总是给我 ParseException Unparsable 日期格式:'May 23 2011 12:20:00'.

It always gives me ParseException Unparsable date format: 'May 23 2011 12:20:00'.

寻找类似的问题,似乎一切正常.

Looked for similar issues, seems everything right.

怎么了?

推荐答案

当您的 VM 的默认语言环境不是英文时,您可能需要额外指定 Locale:

You may need to additionally specify the Locale, when the default Locale of your VM is not an English one:

Date date = (new SimpleDateFormat("MMM dd yyyy HH:mm:ss", Locale.US)).parse(str);

相关文章