Java Calculate Age
In this section, we will create a Java program that calculates age from the given date of birth or current date.
In order to get the date of birth from the current date or any specific date, we should follow the steps given below.
There are the following three ways to calculate age:
Let's discuss each one by one.
Using Period Class
Java 8 provides the Period class that belongs to java.time package. It is an immutable class that models an amount of time (date-based) in years, months, and days. The class provides various static methods that can be used to manipulate date. In order to calculate the age, the Period class provides the between() method.
The method accepts two parameters i.e. start date and end date of LocalDate type. The start date is included but the end date is excluded. It returns the period between the two dates (birth date and current date). It may also give a negative period if the end date is before the start date.
Enter date of birth in YYYY-MM-DD format: 1999-09-23 You are 22 years old.
We can also get age in year, month and days. The following program demonstrates the same.
Your age is 32 years 11 months and 13 days.
Using Enum ChronoUnit
In Java, ChronoUnit is an enum that provides a standard set of date period units. It allows unit-based access to manipulate a date, time, or date-time.
The ChronoUnit enum also provides the between() method which is the same as the between()method of the Java Period class. It also calculates the amount of time (in terms of units) between two specified temporal objects.
The method accepts two parameters temoral1 and temporal2 of type Temporal.
In the following program, we have used the of() method of the Java LocalDate class. The method obtains an instance of LocalDate from a year, month and date. It returns a LocalDate with the specified year, month, and day-of-month. The of() method is an alternative to the LocalDate.now() method.
You are 26 years old.
Using Java Calendar Class
Java Calendar class is an abstract class that belongs to java.util package. It provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits the Object class and implements the Comparable interface.
Let's use the Calendar class for calculating the age.
Your age is: 33
Using Joda Library
Joda is a third-party library that provides a set of duration fields values (such as hour, minutes, days, month, year). Like other Java date and time classes, it is also an immutable date-time class. The property of a class is that it represents a date without a time zone. It uses ISOChronology to represent date and time.
In the following program, we have created two constructors of the LocalDate class (org.joda.time). The first LocalDate constructor accepts the date of birth (dob) and the second LocalDate constructor accepts the current date.
year: Accepts year in yyyy format.
monthOfYear: Accepts a value between 1 to 12.
dayOfMonth: Accepts a value between 1 to 31.
We have used another immutable class named YEARS. The class is used when we deal with year only. It represents the number of years. To get the difference between years of two date instances, the class provides the yearsBetween() method. It accepts two LocalDate objects as parameters.
In order to get the years, the class provides the getYears() method. It demines the number of years that this period represents.
Let's use the above methods in a Java program.
You are 30 years old.
We can also use some other third-party libraries such as Date4J and Time 4J.
The use of the Date4J library is simple but does not give the desired result in some cases such as in the case of leap year. Therefore, evaluating the day of the year is not reliable.
The Time4J library is a complete high-end replacement of old Java classes such as Date, Calendar, and SimpleDateFormat. It is an advanced date, time, and interval library for Java.
It works similarly to Java 8 solution. Just replace LocalDate by PlainDates, and ChronoUnit. YEARS by CalendarUnit.YEARS.