Android Calendar illegalArgumentException when calendar.month set to 1

梦想的初衷 提交于 2020-01-30 02:42:10

问题


The code below works fine except when calendar.MONTH set to 1 (Feb),and I do not know why ? Thanks.

    Calendar calendar = Calendar.getInstance();
    calendar.setLenient(false);    
    calendar.set(Calendar.YEAR, 2013);    
    calendar.set(Calendar.MONTH, 1); // Only when "Feb" failed with illegalArgumentException    

    int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); <= If Calendar.MONTH set to 1, this line will "java.lang.IllegalArgumentException"        

回答1:


You're getting the current date in getInstance. Then you're setting the month to February. The problem is that today is the 30th. February has 28 days. That's an illegal combo. Change the day to a valid day, then change the month.




回答2:


Change field names in set method

calendar.YEAR   to   Calender.YEAR
calendar.MONTH   to  Calender.MONTH

They are static fields and should be referrred with the Class



来源:https://stackoverflow.com/questions/15715369/android-calendar-illegalargumentexception-when-calendar-month-set-to-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!