我想使用日历方法来设置年份和月份,但如果日期无效则需要某种指示,例如
calendar.set(2013,Calendar.JANUARY , 23) //is a valid date
calendar.set(2013,Calendar.JANUARY , 33) //is not
我设置了 calendar.setLenient(false)
并期望将该值设置为 1 月 33 日会引发异常,但它没有。
最佳答案
看起来检查是懒惰地完成的:
A non-lenient GregorianCalendar throws an exception upon calculating its time or calendar field values if any out-of-range field value has been set.
所以这会抛出一个异常:
Calendar c = new GregorianCalendar();
c.setLenient(false);
c.set(2013, Calendar.JANUARY, 33);
c.getTime();
但并非没有 getTime
。
关于java - 使用日历检查日期是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17436524/