How to get age from date in Java

1 Answer

0 votes
import java.time.LocalDate; 
import java.time.Period;

public class MyClass {
    public static void main(String args[]) {
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.parse("1966-02-27");
        
        Period period = Period.between(birthday, today);
        
        System.out.println("Days: " +period.getDays());
        System.out.println("Months: " +period.getMonths());
        System.out.println("Years: " + period.getYears());
    }
}
 
 
 
/*
run:
 
Days: 23
Months: 0
Years: 55
 
*/

 



answered Mar 22, 2021 by avibootz
...