Thursday 1 April 2021

Bean Validation: FutureOrPresent: Validate present or future date

@FutureOrPresent annotation used to validate the instant, date or time in the present or future.

 

Example

@FutureOrPresent

public Date joiningDate;

 

Supported Types

a.   java.util.Date

b.   java.util.Calendar

c.    java.time.Instant

d.   java.time.LocalDate

e.   java.time.LocalDateTime

f.     java.time.LocalTime

g.   java.time.MonthDay

h.   java.time.OffsetDateTime

i.     java.time.OffsetTime

j.     java.time.Year

k.    java.time.YearMonth

l.     java.time.ZonedDateTime

m. java.time.chrono.HijrahDate

n.   java.time.chrono.JapaneseDate

o.   java.time.chrono.MinguoDate

p.   java.time.chrono.ThaiBuddhistDate

 

Where can I apply this annotation?

a.   METHOD

b.   FIELD

c.    ANNOTATION_TYPE

d.   CONSTRUCTOR

e.   PARAMETER

f.     TYPE_USE

 

Employee.java

package com.sample.app.model;

import java.util.Date;

import javax.validation.constraints.FutureOrPresent;

public class Employee {

	private int id;

	private String name;

	@FutureOrPresent
	public Date joiningDate;

	public Employee(int id, String name, Date joiningDate) {
		this.id = id;
		this.name = name;
		this.joiningDate = joiningDate;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getJoiningDate() {
		return joiningDate;
	}

	public void setJoiningDate(Date joiningDate) {
		this.joiningDate = joiningDate;
	}

}

Test.java

package com.sample.app;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class Test {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {

		Employee emp1 = new Employee(1, "Krishna", Calendar.getInstance().getTime());
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		/* Setting tomorrow date to emp2 */
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, 1);

		Employee emp2 = new Employee(2, "Siva", c.getTime());
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);
	}
}

Output

Validation errors on bean emp1
************************************
joiningDate,must be a date in the present or in the future

Validation errors on bean emp2
************************************
No validation errors....






Previous                                                    Next                                                    Home

No comments:

Post a Comment