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

后端 未结 6 1556
小蘑菇
小蘑菇 2020-12-18 17:44

The following code:

Calendar now = Calendar.getInstance();
month = now.get(Calendar.MONTH) + 1;
year = now.get(Calendar.YEAR);
System.out.println(\"Month \"          


        
6条回答
  •  情深已故
    2020-12-18 18:18

    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

提交回复
热议问题