age in java 1

age in java

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1987, 09, 24);

Period period = Period.between(birthday, today);

//Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());

Here is what the above code is Doing:
1. We are creating two LocalDate objects, one for today and one for the birthday.
2. We are using the between() method of the Period class to get the difference between the two dates.
3. We are using the getDays(), getMonths() and getYears() methods to get the difference in days, months and years.

Similar Posts