Java dateformat illegal pattern character ‘y’

We recently made a strange error in the production environment (the test environment works normally)

java. Lang. illegalargumentexception: illegal mode character 'y'

This is caused by the following code

SimpleDateFormat dateFormat = (SimpleDateFormat)DateFormat.getDateInstance();
dateFormat.applyLocalizedPattern("yyyy-MM-dd");

This error is usually thrown when, for example, you use "Y" instead of "Y" It's not here. You can see it above I am not 100% sure of the locale set on the server Linux env Lang is set to "de_de.utf_8", so this may be used

Enter simpledateformat Java source code, I found the method translatepattern (string pattern, string from, string to) When any character in the pattern does not exist, the mentioned exception is thrown These values look like this when debugging locally on different computers

pattern =“yyyy-MM-dd” from =“GyMdkHmsSEDFwWahKzZ”

From the exception on the server, it is obvious that the first "Y" does not exist in From is from formatdata Getlocalpatternchars(), which is dateformatsymbols initialized from the locale on the server

Is there a locale that can even use a format without 'y'? This error has started to occur without any code changes, and the server configuration has not been changed from my knowledge

Solution

From simpledateformat Javadoc:

In your case, the local is De, so the localization mode will be JJJ mm TT J stands for Jahr and t for tag

If you don't want to work with localized patterns, just use simpledateformat applyPattern(“yyyy-MM-dd”).

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>