![]() |
VOOZH | about |
Calculate the age from a birthdate using the Data/Time API in Java programming, which was introduced in Java 8 to perform this calculation efficiently and accurately.
LocalDate is a pre-defined class of the Java Date/Time API. It depicts a date without a time zone and keeps the year, month, and day of the month.
Syntax:
LocalDate localDate = LocalDate.of(year, month, dayOfMonth);Examples:
LocalDate januaryFirst2023 = LocalDate.of(2023, 1, 1); //it takes certain date
LocalDate currentDate = LocalDate.now(); //it takes the current date
Period is a class of the Java Date/Time API which is used to depict a period in terms of years, months, and days and to describe the amount of time between two dates in terms of calendar.
Syntax:
Period periodBetween = Period.between(startDate, endDate);It takes the starting date, ending date, and return year difference between the two dates.
Sample Program:
Age: 26 years