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

后端 未结 6 1531
小蘑菇
小蘑菇 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 17:59

    try

    SimpleDateFormat dt1 = new SimpleDateFormat("MMMM yyyy");
    
    0 讨论(0)
  • 2020-12-18 18:13

    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.

    0 讨论(0)
  • 2020-12-18 18:16

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

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-18 18:19

    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

    0 讨论(0)
  • 2020-12-18 18:20

    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);
    
    0 讨论(0)
提交回复
热议问题