I'm trying to setup a javax Validation to compare two dates with each other.
One date should be less than the other.
Also, I created a custom constraint for both of my dates. But the problem is, that my annotation interface can't handle with non-primitive data types.
Here is my Code: (I know that this is wrong, but maybe you can understand my problem if I show my source)
Bean:
@DateValidator(firstDate="assignmentStart", secondDate="assignmentEnd")
public class ProjectAssignment implements IsSerializable {
private String id;
private Project project;
private String projectResponsibility;
private Date assignmentStart;
private Date assignmentEnd;
}
Annotation-Interface:
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = DateValidatorImpl.class)
public @interface DateValidator {
String firstDate();
String secondDate();
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
So here is one Problem: I try to put the Date type into the String type.
But if I declare my firstDate and secondDate as a Date, there is an issue:
Invalid type Date for the annotation attribute `DateValidator.firstDate;
only primitive type, `String`, `Class`, annotation, enumeration are permitted
or 1-dimensional arrays thereof
Are there any other ways to compare 2 Dates with each other?
What am I missing?
As Java annotations can't accept Date fields, you will need to use String class.
You can convert a Date to a String with DateFormat, and then convert it back with the same DateFormat.
However, it implies that your assignment fields in ProjectAssignment must be of String type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With