如何使用Java中的DateFormat解析月份完整表单字符串? [英] How to parse month full form string using DateFormat in Java?
问题描述
我试过
DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy");
Date d = fmt.parse("June 27, 2007");
线程main中的异常java.text.ParseException:无法解析的日期: 2007年6月27日
java文档说我应该使用四个字符来匹配完整的表单。
我只能使用缩短的月份(如Jun)成功使用MMM,但我需要匹配完整的表格。
The java docs say i should use four characters to match the full form. I'm only able to use MMM successfully with abbreviated months like "Jun" but i need to match full form.
文本:对于格式化,如果模式字母的数字
为4或更多,则使用
完整形式;否则,如果
可用,则使用简短的
或缩写形式。对于解析,两个表单都接受
,与
模式字母的数量无关。
Text: For formatting, if the number of pattern letters is 4 or more, the full form is used; otherwise a short or abbreviated form is used if available. For parsing, both forms are accepted, independent of the number of pattern letters.
http://java.sun.com/j2se/1.6。 0 / docs / api / java / text / SimpleDateFormat.html
推荐答案
你可能正在使用一个区域设置月份名称不是1月,2月等,而是用您当地语言的其他一些词语。
You are probably using a locale where the month names are not "January", "February", etc. but some other words in your local language.
尝试指定您想要使用的语言环境,例如 Locale.US
:
Try specifying the locale you wish to use, for example Locale.US
:
DateFormat fmt = new SimpleDateFormat("MMMM dd, yyyy", Locale.US);
Date d = fmt.parse("June 27, 2007");
此外,日期字符串中有额外的空格,但实际上这对结果没有影响。无论哪种方式都可以。
Also, you have an extra space in the date string, but actually this has no effect on the result. It works either way.
这篇关于如何使用Java中的DateFormat解析月份完整表单字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!