How to generate age from date of Birth – (Java 8) – java.time.*

How to generate age from date of Birth – Java 8 – java.time.*

package unit.test.timejava8;

import java.time.LocalDate;
import java.time.Period;
/**
 * @author fndong
 * generate age from date of Birth Java 8 - java.time.*
 */
public class Java8TimeAge {
    public void getAge(){
      String dob = "1989-07-20";
      
      
     LocalDate localDate = LocalDate.parse(dob);
     LocalDate now = LocalDate.now();
     
     Period  period = Period.between(localDate,now);
        System.out.printf("%d years,%d months and %d days  old!\n",p.getYears(), p.getMonths(), p.getDays());
    }
    
    public static void main(String args[]){
        Java8TimeAge java8TimeAge = new Java8TimeAge();
        java8TimeAge.getAge();
    }
}

Output :

32 years,1 months and 2 days  old!

 

vaadin – How to add try catch exception inside addValueChangeListener with lambda expression

Good Day !

Title : Vaadin – How to add try catch exception inside addValueChangeListener with lambda expression (java 8)

Example :

// add lambda expression – java 8
s.addValueChangeListener((ValueChangeListener)
(ValueChangeEvent) -> {
           try {
objectsDistrict.addAll(findDistrictSelection((RefState)                                        ValueChangeEvent.getProperty().getValue()));
   } catch (Exception e) {
 // TODO: handle exception

    e.printStackTrace();
                          Notification.show(“Error on Selection Records“,
                        Type.ERROR_MESSAGE);
                       }
 }
});

2) The Above codes are from Source code tutorial – vaadin – How to create a second Combobox which is dependent from the First Combobox.

lambda expressions – java version 8

About lambda Expressions:

Lambda Expression been introduced by java 8 , its  a functional programming to make java 8 more faster, clearer coding.
1) A lambda expression Syntax :

parameter -> expression body
or 
(parameters) ->{ statements; }

2.) Lambda Expressions Example:

2.1) First we try traditional approaches :

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
for(Integer n: list) {
System.out.println(n);
}

2.2) Then we try the lambda Expressions:

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
list.forEach(n -> System.out.println(n));

2.3) Math Operations:

//with type declaration
MathOperation addition = (int a, int b) -> a + b;

//with out type declaration
MathOperation subtraction = (a, b) -> a – b;

 

3. characteristics of a lambda expression: 

  • Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
  • Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
  • Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
  • Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.