[英]Java Check whether Date (made of integers) is valid
我目前正在使用以下功能設置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;
}
但是,如果輸入(2014年6月31日),則日期僅更改為2014年7月1日。如果我的輸入如上所述,是否可以檢查日期是否有效?
謝謝你的幫助。
您得到2014年7月1日作為輸出的原因:
要解決此問題,請將lenient設置為false:
calendar.setLenient(false);
如果日期超出范圍,則會引發異常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.