I am currently setting a Date
using the following function.
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;
}
However if (31, 6, 2014) is entered, the date is simply changed to the 1st of July 2014. Is there a way that I could check whether a date is valid if my input is as above?
Thanks for your help.
Reason why you get 1st of July 2014 as output:
To counteract this, set lenient to false:
calendar.setLenient(false);
and an exception will be thrown if the dates are out of bounds
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.