问题
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