Thursday, 1 April 2021

Bean Validation: PastOrPresent: Element must represent a past or current instant, date or time.

@Past annotation used to validate the instant, date or time in the past or present.

 

Example

@PastOrPresent

public Date joinedDate;

 

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.PastOrPresent;

public class Employee {

	private int id;

	private String name;

	@PastOrPresent
	public Date joinedDate;

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

	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 getJoinedDate() {
		return joinedDate;
	}

	public void setJoinedDate(Date joinedDate) {
		this.joinedDate = joinedDate;
	}

}

 

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
************************************
No validation errors....

Validation errors on bean emp2
************************************
joinedDate,must be a date in the past or in the present

 

 

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment