java.lang.IllegalArgumentException: SimpleDateFormat 的非法模式字符“Y”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25337241/
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
java.lang.IllegalArgumentException: Illegal pattern character 'Y' for SimpleDateFormat
提问by xrcwrn
The following code:
以下代码:
Calendar now = Calendar.getInstance();
month = now.get(Calendar.MONTH) + 1;
year = now.get(Calendar.YEAR);
System.out.println("Month " + month + " year " + year);
SimpleDateFormat dt1 = new SimpleDateFormat("MMMM YYYY");
e.setMonthnYear(dt1.format(now.getTime()));
After deploying on server is showing following exception:
在服务器上部署后显示以下异常:
java.lang.IllegalArgumentException: Illegal pattern character 'Y'
java.text.SimpleDateFormat.compile(SimpleDateFormat.java:768)
java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:575)
java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:500)
java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:475)
iland.employee.EmployeeAction.fetchAllAtted(EmployeeAction.java:169)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
On my local host I am using JDK v1.8
and the above code is working perfectly, but on server it is not working.
在我正在使用的本地主机上JDK v1.8
,上面的代码运行良好,但在服务器上却无法运行。
How can I resolve this?
我该如何解决这个问题?
采纳答案by Evgeniy Dorofeev
try
尝试
SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy");
回答by Mandar Pandit
On your Local you might be using Java 8, so do check the version of Java on your Server. If it is less than Java JDK 7 the capital Y
will not work.
在你的本地你可能使用的是 Java 8,所以一定要检查你服务器上的 Java 版本。如果它低于 Java JDK 7,则资本Y
将不起作用。
Refer To Java 6 Oracle Docs for SimpleDateFormat
有关SimpleDateFormat 的信息,请参阅 Java 6 Oracle Docs
You have to write year in small y
not in capitals Y
.
你必须用小写年份而y
不是大写字母Y
。
Like for 2 digit year:
像 2 位数年份:
SimpleDateFormat dt1 = new SimpleDateFormat("yy");
And for 4 digit year:
对于 4 位数年份:
SimpleDateFormat dt1 = new SimpleDateFormat("yyyy");
In case if you are using Java 7 or above:
You can use the capital Y
which represents Week Year
.
如果您使用的是 Java 7 或更高版本:您可以使用Y
代表Week Year
.
Refer to Java 7 Oracle Docs SimpleDateFormat
请参阅 Java 7 Oracle 文档SimpleDateFormat
回答by DavidPostill
From java.text.SimpleDateFormat:
Letter Date or Time Component Presentation Examples
y Year Year 1996; 96
Y Week year Year 2009; 09
You are asking for Week year
instead of year
in your call to SimpleDateFormat()
你要求Week year
而不是year
在你的电话中SimpleDateFormat()
回答by jaimin
i have taken this table from java docs.
我从java文档中获取了这张表。
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 1996; 96
M Month in year Month July; Jul; 07
w Week in year Number 27
W Week in month Number 2
D Day in year Number 189
d Day in month Number 10
F Day of week in month Number 2
E Day in week Text Tuesday; Tue
In your case just replace"Y" to "y" you can see Docs here
回答by SparkOn
As per the javadocs
根据javadocs
If week year 'Y' is specified and the calendar doesn't support any week years,
the calendar year ('y') is used instead. The support of week years can be tested
with a call to getCalendar().isWeekDateSupported().
So the only problem is guess is your version of java < 1.7 because JRE1.7 has added 'Y' pattern for Week year and in JRE1.6 there is no pattern for this.
因此,唯一的问题是猜测您的 java < 1.7 版本,因为 JRE1.7 为周年添加了“Y”模式,而在 JRE1.6 中没有此模式。
Or simply stay on the safer side use y
instead of Y
.
或者干脆留在更安全的一边使用y
而不是Y
.
One more thing always try to use locale to be on safer side
还有一件事总是尝试使用语言环境来更安全
SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy",java.util.Locale.ENGLISH);
回答by aruh
Android
安卓
The documentationdiffers from the implementation. The supported characters are defined in a string constant in SimpleDateFormat
up to API level 23. From the source code:
该文件不同于实施。支持的字符在SimpleDateFormat
API 级别 23的字符串常量中定义。来自源代码:
static final String PATTERN_CHARS = "GyMdkHmsSEDFwWahKzZLc";
Since 'Y' (Week Year) is not included, the pattern validation throws the exception:
由于不包括“Y”(周年),因此模式验证会引发异常:
java.lang.IllegalArgumentException: Unknown pattern character 'Y'
A quick fix, when week year behaviour isn't required, is to use the 'y', e.g.: yyyy-MM-dd
.
当不需要周年行为时,快速修复是使用“y”,例如:yyyy-MM-dd
。
'Y' as a pattern character is supported as of API level 24.
从 API 级别 24 开始,支持“Y”作为模式字符。
Update
更新
The documentationnow lists the supported API levels for pattern characters.
该文档现在列出了模式字符支持的 API 级别。