DateConstraint extends Constraint<Object> { private boolean isValid(String value) { try { DateFormat dateFormat = new SimpleDateFormat(format, Locale.getDefault()); dateFormat.setLenient(false); dateFormat.parse(value); } catch (ParseException e) { return false; } return true; } DateConstraint(@NonNull Object object, @NonNull String propertyName);  DateConstraint(@NonNull Object object, @NonNull String propertyName, @NonNull String format); @NonNull @Override Set<Violation> validate(); void setMessage(String message); void setFormat(String format); static final String ERROR_CODE_DATE_INVALID; }
@Test public void isValid() throws Exception { assertEmpty("is valid with String", new DateConstraint(VALID, PROPERTY_NAME).validate()); assertEmpty("is valid with EditText", new DateConstraint(generateEditText(VALID), PROPERTY_NAME).validate()); assertEmpty("is valid with Spinner", new DateConstraint(generateSpinner(VALID), PROPERTY_NAME).validate()); }