56

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.

How can I resolve this?

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
  • 2
    You need to check java version on your server. If it is not Java 8 or less than Java 7 the `Y` will not work there. – Mandar Pandit Aug 16 '14 at 06:04
  • 2
    I replaced YYYY with yyyy(lowercase) it started working, as mentioned below in answers must be issue with Java versions. – vishwa.deepak Mar 30 '17 at 07:47

6 Answers6

75

try

SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy");
CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    As OP is using Java 1.8 the capital `Y` is valid for Week Year. Refer to Java 8 [SimpleDateFormat](http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html). – Mandar Pandit Aug 16 '14 at 05:53
  • @Evgeniy Dorofeev just a little doubt javadoc says- If week year 'Y' is specified and the calendar doesn't support any week years, the calendar year ('y') is used instead. So according to that Y should not have thrown IllegalArgumentException right? – SparkOn Aug 16 '14 at 07:44
  • @M.Sharma it depends on Java version, but even if it is Java 8 and Y does not throw an exception, it has a different meaning and will be interpreted incorrectly – Evgeniy Dorofeev Aug 16 '14 at 08:40
37

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

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
15

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.

aruh
  • 396
  • 6
  • 10
7

As per the 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.

Or simply stay on the safer side use y instead of Y.

One more thing always try to use locale to be on safer side

SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy",java.util.Locale.ENGLISH);
SparkOn
  • 8,806
  • 4
  • 29
  • 34
2

i have taken this table from java docs.

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

jaimin
  • 563
  • 8
  • 25
  • I guess, because the downvoters think that it is irrelevant to the Question. I agree with that proposition. – Stephen C Aug 16 '14 at 06:32
0

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()

DavidPostill
  • 7,734
  • 9
  • 41
  • 60