java.lang.IllegalArgumentException: Illegal pattern character 'Y' for SimpleDateFormat

try

SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy");

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.

Refer To Java 6 Oracle Docs for SimpleDateFormat

You have to write year in small y not in capitals Y.

Like for 2 digit year:

 SimpleDateFormat dt1 = new SimpleDateFormat("yy");

And for 4 digit year:

 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.

Refer to Java 7 Oracle Docs SimpleDateFormat


Android

The documentation differs from the implementation. The supported characters are defined in a string constant in SimpleDateFormat up to API level 23. From the source code:

static final String PATTERN_CHARS = "GyMdkHmsSEDFwWahKzZLc";

Since 'Y' (Week Year) is not included, the pattern validation throws the exception:

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' as a pattern character is supported as of API level 24.

Update

The documentation now lists the supported API levels for pattern characters.

Tags:

Java