You can calculate a person’s age in years using Java 8’s LocalDate and Period API.
import java.time.LocalDate;
import java.time.Period;
public class AgeCalculator {
public static void main(String[] args) {
LocalDate birthDate = LocalDate.of(1990, 5, 15); // Change to actual birth date
LocalDate currentDate = LocalDate.now();
int age = Period.between(birthDate, currentDate).getYears();
System.out.println("Age: " + age + " years");
}
}
Using ChronoUnit.YEARS
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class AgeCalculatorChrono {
public static void main(String[] args) {
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate currentDate = LocalDate.now();
long age = ChronoUnit.YEARS.between(birthDate, currentDate);
System.out.println("Age: " + age + " years");
}
}
