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

Bean validation: Past: Element must represent a past instant, date or time.

 

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

 

Example

@Past

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

public class Employee {

	private int id;

	private String name;

	@Past
	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 past date





 

 

 

Previous                                                    Next                                                    Home

Bean validation: Future: Element must represent a future instant, date or time

 

@Future annotation used to validate the instant, date or time in the future. If you annotate any element with @Future annotation, then the annotated element must be an instant, date or time in the future. In simple terms, @Future annotation ensures that the date in the future.

 

Example

@Future

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

import java.util.Date;

import javax.validation.constraints.Future;

public class Employee {

	private int id;

	private String name;

	@Future
	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.test;

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

************************************
joinedDate,must be a future date

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

 

 

 

 

 

 

Previous                                                    Next                                                    Home

Php: Add an element to the array

Syntax

$array_name[] = new_value;

 

Example

$arr1[] = 11;

 

Above statement add the number 11 to the end of the array. Find the below working application.

 

array_add_element.php

#!/usr/bin/php

<?php
$arr1 = array(
    2,
    3,
    5,
    7
);

print_r($arr1);

echo "\nAdding another element 11 to arr1\n";

$arr1[] = 11;
print_r($arr1);

echo "\nAdding another element 13 to arr1\n";

$arr1[] = 13;
print_r($arr1);
?>

 

Output

$./array_add_element.php 

Array
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 7
)

Adding another element 11 to arr1
Array
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 11
)

Adding another element 13 to arr1
Array
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 11
    [5] => 13
)

 

 

 

  

Previous                                                    Next                                                    Home

Php: Arrays

Arrays are used to store collection of values. Values can be of any type.

 

How to define an array?

Arrays can be created either using index notation or using array() function.

 

Example

$arr1 = [2, 3, 'Hello', true];

$arr2 = array(2, 3, 'Hello', true);

 

Different types of arrays

There are two types of arrays.

a.   Indexed arrays: Use numbers as keys

Ex: $arr1 = [2, 3, 'Hello', true];

 

b.   Associative array: Use strings as keys

Ex:

$arr1 = [

"id" => 1,

"age" => 32,

 "name" => "Sailu",

 "male" => false

];

 

arrays_demo.php

#!/usr/bin/php

<?php
    
   $arr1 = [2, 3, 'Hello', true];
   
   $arr2 = array(2, 3, 'Hello', true);
   
   echo "Contents of arr1 are : \n";
   print_r($arr1);
   
   echo "\nContents of arr2 are : \n";
   print_r($arr2);
   
?>

 

Output

$./arrays_demo.php 

Contents of arr1 are : 
Array
(
    [0] => 2
    [1] => 3
    [2] => Hello
    [3] => 1
)

Contents of arr2 are : 
Array
(
    [0] => 2
    [1] => 3
    [2] => Hello
    [3] => 1
)

 

Analyze output

Array

(

    [0] => 2

    [1] => 3

    [2] => Hello

    [3] => 1

)

 

 

From the output, you can confirm

a.   Variable is of type Array

b.   Elements in array are counted from index 0. First element is stored at index 0, 2nd element is at index 1 etc.,

 

You can access the elements of array using index. For example, arr1[0] return the value at index 0.

 

array_access_elements_demo.php

#!/usr/bin/php

<?php
$arr1 = [
    2,
    3,
    'Hello',
    true
];

echo "arr1[0] = $arr1[0]\n";
echo "arr1[1] = $arr1[1]\n";
echo "arr1[2] = $arr1[2]\n";
echo "arr1[3] = $arr1[3]\n";
?>

 

Output

$./array_access_elements_demo.php 

arr1[0] = 2
arr1[1] = 3
arr1[2] = Hello
arr1[3] = 1

 

Associative arrays

Unlike other programming languages, arrays in php can act as key->value pairs.

 

Example

$arr1 = [

"id" => 1,

"age" => 32,

 "name" => "Sailu",

 "male" => false

];

 

 

array_named_index.php

#!/usr/bin/php

<?php
$arr1 = [
    "id" => 1,
    "age" => 32,
    "name" => "Sailu",
    "male" => false
];

echo "Contents of arr1 are : \n";
print_r($arr1);

?>

 

Output

$./array_named_index.php 

Contents of arr1 are : 
Array
(
    [id] => 1
    [age] => 32
    [name] => Sailu
    [male] => 
)

 

 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home