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!

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s