calculate Age on LocalDate - Java java.time

Java examples for java.time:LocalDate

Description

calculate Age on LocalDate

Demo Code


//package com.java2s;
import java.time.LocalDate;

public class Main {
    public static int calculateAge(LocalDate birthDate) {
        LocalDate now = LocalDate.now();

        int birthYear = birthDate.getYear();
        int birthMonth = birthDate.getMonthValue();
        int birthDay = birthDate.getDayOfMonth();

        int currentYear = now.getYear();
        int currentMonth = now.getMonthValue();
        int currentDay = now.getDayOfMonth();

        int age = currentYear - birthYear;

        if (currentMonth < birthMonth) {
            age--;/*from   www  . j a  v a2  s  .co  m*/
        } else if (currentMonth == birthMonth && currentDay < birthDay) {
            age--;
        }
        return age;
    }
}

Related Tutorials