3

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?

Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
DenisK
  • 153
  • 1
  • 2
  • 10

1 Answers1

1

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.

Vianney Dupoy de Guitard
  • 2,254
  • 1
  • 14
  • 10