Last active
April 30, 2019 09:47
-
-
Save bhawna94/7dd06296e62ef968628f9bfdb25b8671 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
import java.time.LocalDate; | |
import java.time.format.DateTimeFormatter; | |
import java.time.temporal.ChronoUnit; | |
public class FutureAndWithinSevenDaysValidator implements ConstraintValidator<FutureAndWithinSevenDays, String> { | |
@Override | |
public final boolean isValid(final String value, | |
final ConstraintValidatorContext context) { | |
if (value != null) { | |
LocalDate formattedDate = LocalDate.parse(value, DateTimeFormatter.BASIC_ISO_DATE); | |
long day = ChronoUnit.DAYS.between(LocalDate.now(), formattedDate); | |
return formattedDate.isAfter(LocalDate.now()) && day <= 7; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment