With this implementation seems to work:
package net.orique.stackoverflow.question11740273;
import java.text.DateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
public class NotLenientDateEditor extends CustomDateEditor {
public NotLenientDateEditor(DateFormat dateFormat, boolean allowEmpty,
int exactDateLength) {
super(dateFormat, allowEmpty, exactDateLength);
dateFormat.setLenient(false);
}
}
Class with main
method:
package net.orique.stackoverflow.question11740273;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd.m.yy");
NotLenientDateEditor n = new NotLenientDateEditor(sdf, false, 7);
n.setAsText("12.1.20"); // Ok
n.setAsText("12.1.20asa21"); // Throws java.lang.IllegalArgumentException
}
}
Notes:
How do you instantiate NotLenientDateEditor
? Which format do you set for DateFormat? Note the single digit for month
and 7
for the exactDateLength
constructor argument.