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

后端 未结 6 1550
小蘑菇
小蘑菇 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: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);
    

提交回复
热议问题