package com.hollywood.applet.utils;
|
|
import java.time.LocalDate;
|
import java.time.Period;
|
import java.time.ZoneId;
|
import java.util.Date;
|
import java.util.concurrent.TimeUnit;
|
|
public class AgeCalculator {
|
|
/**
|
* Calculate the age based on the given birthdate which is a java.util.Date object.
|
*
|
* @param birthdate The birthdate as a java.util.Date object.
|
* @return The age in years.
|
*/
|
public static int calculateAge(Date birthdate) {
|
// Convert java.util.Date to LocalDate
|
LocalDate birthdateLocalDate = birthdate.toInstant()
|
.atZone(ZoneId.systemDefault())
|
.toLocalDate();
|
|
// Get the current date
|
LocalDate currentDate = LocalDate.now();
|
|
// Calculate the age using Period
|
Period agePeriod = Period.between(birthdateLocalDate, currentDate);
|
|
// Return the age in years
|
return agePeriod.getYears();
|
}
|
|
|
}
|