将calendar.month设置为1时,Android日历非法ArgumentException [英] Android Calendar illegalArgumentException when calendar.month set to 1

查看:62
本文介绍了将calendar.month设置为1时,Android日历非法ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码工作正常,除非 calendar.MONTH设置为1(2月),并且我不知道为什么?谢谢.

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"        

推荐答案

您正在getInstance中获取当前日期.然后,您将月份设置为2月.问题是今天是30日.二月有28天.那是非法的组合.将日期更改为有效日期,然后更改月份.

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.

这篇关于将calendar.month设置为1时,Android日历非法ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆