【发布时间】:2014-05-10 12:39:47
【问题描述】:
我目前正在使用以下函数设置Date。
private Date getDate (int day, int month, int year){
Calendar calendar = Calendar.getInstance();
calendar.set(year, month-1, day);
Date date = calendar.getTime();
return date;
}
但是,如果输入 (31, 6, 2014),日期只是简单地更改为 2014 年 7 月 1 日。如果我的输入如上,有没有办法检查日期是否有效?
感谢您的帮助。
【问题讨论】:
-
仅供参考,几乎普遍建议在 Java 的
Date库上使用 JodaTime。 (参见例如stackoverflow.com/a/589940/129570) -
另外,我想你可能想先使用
setLenient(true)。 -
有效日期是什么意思?你能解释一下吗?
-
@Hirak:OP 的例子不是很不言自明吗? 6 月 31 日不存在。