So I have to compare two date as mentionned, and i coded a validator and it seems that it has some errors and it's a bit strange : Annotation class : Here the @Target annotation is imported but the enum inside is not recognized.
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { ValidAfterDateValidator.class })
@Documented
public @interface ValidAfterDate {
String message() default "The End date should be after the starting date." ;
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
ValidAfterDateValidator.class :
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import gestionprojet.java.entities.Project;
public class ValidAfterDateValidator implements ConstraintValidator<ValidAfterDate, Project> {
@Override
public void initialize(ValidAfterDate constraintAnnotation) {
}
@Override
public boolean isValid(Project projet, ConstraintValidatorContext context) {
if (projet == null) {
return true;
}
return projet.getDate_Debut().compareTo(projet.getDate_Fin()) > 0;
}
And here the @ValidAferDate is disallowed :
Project.class :
@ValidAfterDate
public class Project {
@Future
@DateTimeFormat(pattern="dd/MM/YY")
private Date date_Debut;
@Future
@DateTimeFormat(pattern="dd/MM/YY")
private Date date_Fin;
//Getters & Setters
}
Any help would appreciated. Thanks
This :
@Target( { TYPE , ANNOTATION_TYPE })
needs to be changed to :
@Target( {ElementType.ANNOTATION_TYPE, ElementType.TYPE })
and the imporation is :
import java.lang.annotation.ElementType;