JSR 303 - javax.validation - Check date

I have a Java EE application and I want to check the date. Using String, I do this:

import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; ... @NotNull @Size(min = 1, max = 255) private String myString; 

But now I have two dates that I want to check. The user can write a string in a text field in the interface system that will be transmitted via JSON (I have to use a text field, I cannot use datepicker).

So my backend has this in my domain class:

 @DateTimeFormat(pattern = "dd.MM.yy") @Temporal(value=TemporalType.TIMESTAMP) private Date myStartDate; @DateTimeFormat(pattern = "dd.MM.yy") @Temporal(value=TemporalType.TIMESTAMP) private Date myEndDate; 

I want to check for the format "dd.MM.yyyy". How can I do that?

And, I donโ€™t think so, but is there an automatic check to check if there is a start date before the end date? I found only @Future and @Past .

So the only solution is to use @Pattern, regex ?!

Thank you in advance for your help, best regards.

+7
source share
1 answer

@DateTimeFormat is used when binding web data when matching request parameters for an object (provided that you included it using <mvc:annotation-driven/> or manually.) Usually it is not used when deserializing JSON into an object. How do you read in your JSON? What do you use for deserialization? You cannot check the Java data object after the fact of formatting, before deserializing you need to check the front.

Within built-in constraints, there are no multi-user constraints. You need to write your own level restriction on a level if you want to compare two properties of an object.

+3
source

All Articles