Showing posts with label present. Show all posts
Showing posts with label present. Show all posts

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

Wednesday, 29 April 2020

Check whether given character present in string or not

Using ‘indexOf’ method, we can check whether given character is present in string or not. ‘indexOf()’ method return -1, if given character is not present in the string, else it return the index of the first occurrence of the character in the string.

Example
private static boolean isCharExist(String str, char ch) {
         if (str == null) {
                  throw new IllegalArgumentException("String can't be null");
         }

         return str.indexOf(ch) != -1;
}

Find the following working application.

App.java
package com.sample.app;

public class App {

 private static boolean isCharExist(String str, char ch) {
  if (str == null) {
   throw new IllegalArgumentException("String can't be null");
  }

  return str.indexOf(ch) != -1;
 }

 public static void main(String args[]) {
  String str = "Hello World";

  char ch = 'H';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));

  ch = 'o';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));

  ch = 'a';
  System.out.println("is " + ch + " exists in " + str + " : " + isCharExist(str, ch));
 }

}

Output
is H exists in Hello World : true
is o exists in Hello World : true
is a exists in Hello World : false


You may like