R4R Style Learning Portal

Calculate the exact number of years between a birth date and today using java 8 stream api

The Java code you've provided, FindBirthday, correctly calculates the age in years between a specified birth date and the current date using the java.time package introduced in Java 8. 
Here's an explanation of the code:
 import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class FindBirthday {
    public static void main(String[] args){
        // Define the birthday as July 21, 1982
        LocalDate birthDay = LocalDate.of(1982, 7, 21);

        // Get the current date
        LocalDate today = LocalDate.now();

        // Calculate the difference in years between the birthday and today
        // ChronoUnit.YEARS.between() directly gives the number of full years between two dates
        System.out.println(ChronoUnit.YEARS.between(birthDay, today));
    }
}
 
Explanation
  1. import java.time.LocalDate;: This line imports the LocalDate class, which represents a date without a time zone, keeping the year, month, and day of the month. It's ideal for storing dates like birthdays. Baeldung notes that LocalDate is helpful because it represents just a date, compared to Java's Date class, which represents both a date and a time.
  2. import java.time.temporal.ChronoUnit;: This line imports the ChronoUnit enum, which provides various standard units of time, such as years, months, days, hours, minutes, seconds, and so on. It is used here to define the unit of time for the calculation (years, in this case).
  3. public class FindBirthday { ... }: This defines a public class named FindBirthday that contains the main method.
  4. public static void main(String[] args){ ... }: This is the main method, the entry point for the Java application.
  5. LocalDate birthDay = LocalDate.of(1982, 7, 21);: A LocalDate object named birthDay is created and initialized to July 21, 1982. The LocalDate.of() method allows you to construct a LocalDate by providing the year, month, and day.
  6. LocalDate today = LocalDate.now();: A LocalDate object named today is created and set to the current date (which is July 19, 2025, based on the context provided) using the LocalDate.now() method.
  7. System.out.println(ChronoUnit.YEARS.between(birthDay, today));: This line calculates the age in years and prints it to the console. WsCube Tech states that ChronoUnit.YEARS.between()calculates the difference between two dates.
    • ChronoUnit.YEARS.between(birthDay, today): This method calculates the number of full years that have passed between birthDay and today. This correctly handles cases where the birthday hasn't occurred yet in the current year. For example, if today were July 1, 2025, the age would still be calculated based on the completed years. WsCube Tech explains that ChronoUnit.YEARS.between() calculates the difference in years between two dates without needing to manually check for leap years or month differences.
Output
Given the birthDay of July 21, 1982, and the today being July 19, 2025 (based on the context), the output will be:
 
42