I'm using this SimpleDateFormat yyyy/MM/dd to pass a date value from String to Date. But there is this issue: when I input a non-date value, like 2020/12/2x, it will return to 2020/12/02. And when I try 2020/12/x2, the function will return an error. Anyone know why this is happening? Thank you so much. Here is my code.
Declare SimpleDateFormat
SimpleDateFormat df2 = new SimpleDateFormat("yyyy/MM/dd");
df2.setLenient(false);
Function I used to check if the string I input is a valid date or not
private static Boolean validDate(DateFormat df, String s) {
Boolean valid=false;
try {
Date d= df.parse(s);
valid=true;
} catch (Exception e) {
valid=false;
}
return valid;
}
I used input:
2020/12/2x