How do I calculate someone's age in Java?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Calculating someone's age in Java can be accomplished in several ways depending on the Java version and libraries being used. The logic, however, remains essentially the same: calculate the difference between the current date and the birth date. This article will explore multiple techniques, including using java.util.Calendar, java.time.LocalDate from Java 8 onwards, and external libraries such as Joda-Time.
Using java.util.Calendar
This approach works in Java versions prior to Java 8 but remains compatible in later versions. Here's how you can calculate age using Calendar.
Explanation
- A
Calendarobject is initialized to represent the birth date. - Another
Calendarobject is initialized to represent the current date. - Compute the difference in years between the two
Calendarinstances. - Adjust for incomplete years by comparing the ordinal day (
DAY_OF_YEAR) of both dates.
Using java.time.LocalDate (Java 8 and later)
Java 8 introduced the new java.time package, which offers better clarity and simplicity when working with dates.
Explanation
LocalDate.of(year, month, day)is used to create a birth date.LocalDate.now()provides the current date.Period.between()calculates the temporal difference between the twoLocalDateobjects.- Extract the difference in years with
getYears().
Using Joda-Time
Joda-Time was a popular library for handling dates and times before Java 8. If you're working in an environment that hasn't upgraded to Java 8 or higher, this library is still an excellent choice.
Explanation
LocalDatefrom Joda-Time is similar but predatesjava.time.LocalDate.Years.yearsBetween()conveniently handles the calculation of the years' difference.
Key Points Summary
| Topic | Description |
java.util.Calendar | Used for age calculation prior to Java 8 |
java.time.LocalDate | Introduced in Java 8, simpler & more robust |
| Handling incomplete years | Both approaches check if the birthday passed |
| Joda-Time | Alternative library pre-Java 8, still useful |
| Ease of Use | java.time is recommended for new projects |
Considerations
- Leap Years: All above methods inherently handle leap years due to the calendar system they rely on.
- Timezone: Dates are treated as local to the system's time zone. Differences in time zones can affect exact age if time is a factor.
Utilizing the modern java.time.LocalDate is generally recommended for its simplicity and alignment with ISO 8601, making it more intuitive to understand and work with, particularly for new projects. However, the methods involving java.util.Calendar and Joda-Time are supported and still quite viable for legacy applications or pre-Java 8 environments.
Related reading
- How do I convert an existing callback API to promises?
- How do I create padded batches in Tensorflow for tf.train.SequenceExample data using the DataSet API?
- How do I debug AWS Api Gateway Lambda's AWS/ApiGateway 5XXError
- How do I expose Kubernetes service to the internet?
- How do I change log level in runtime without restarting Spring Boot application
- How do i change logback version in spring boot?
- How do I find out the external IP of a Load Balancer service?
- How do I find which CNI plugin is my k8s is using? Where is its config files?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.