Java Age Calculator Program (from Date/Year of Birth)
In a world driven by technology and innovation, programming is an invaluable skill that opens up several new possibilities. Java, as one of the most popular and versatile programming languages, provides a solid foundation for developing a wide range of applications.
In this tutorial, we will create a practical and useful program for Age Calculator in Java.
The Age Calculator program in Java is a simple yet effective tool that allows users to find their age or the age of others by date of birth. Whether you're a budding programmer looking to expand your skills or someone with a curiosity for how software applications work, this tutorial will guide you through the process step by step.
Concepts Used:
Age Calculator in Java Using Class
Here's an example of an age calculator program in Java using the Date and Calendar classes:
Code
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
public class AgeCalculator {
public static void main(String[] args) {
// Get the current date
Calendar currentDate = Calendar.getInstance();
Date now = currentDate.getTime();
// Set the birthdate
Calendar birthDate = Calendar.getInstance();
birthDate.set(1990, Calendar.JANUARY, 1); // Replace with the birthdate you want to calculate from
// Calculate the age
int age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
// Check if the birthday hasn't occurred yet this year
if (currentDate.get(Calendar.MONTH) < birthDate.get(Calendar.MONTH) ||
(currentDate.get(Calendar.MONTH) == birthDate.get(Calendar.MONTH) &&
currentDate.get(Calendar.DAY_OF_MONTH) < birthDate.get(Calendar.DAY_OF_MONTH))) {
age--;
}
// Format and display the result
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String birthDateString = dateFormat.format(birthDate.getTime());
String currentDateString = dateFormat.format(now);
System.out.println("Date of Birth: " + birthDateString);
System.out.println("Current Date: " + currentDateString);
System.out.println("Age: " + age + " years");
}
}
Output
Date of Birth: 1990-01-01
Current Date: 2023-09-28
Age: 33 years
Explanation
This program first obtains the current date and time using Calendar.getInstance() and getTime().
It sets a date of birth (you can replace it with the birthdate you want to calculate from) using another Calendar instance.
It calculates the age by subtracting the birth year from the current year.
It then checks if the birthday for the current year has already occurred. If not, it subtracts one from the calculated age.
Finally, it formats and displays the birthdate, current date, and age in years.
Java Age Calculator Using java.time
Here's an age calculator program in Java using the java.time package introduced in Java 8:
Code
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class AgeCalculator {
public static void main(String[] args) {
// Define the birthdate
LocalDate birthDate = LocalDate.of(1990, 1, 1); // Replace with the birthdate you want to calculate from
// Get the current date
LocalDate currentDate = LocalDate.now();
// Calculate the age
Period age = Period.between(birthDate, currentDate);
// Format and display the result
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String birthDateString = birthDate.format(dateFormatter);
String currentDateString = currentDate.format(dateFormatter);
System.out.println("Birthdate: " + birthDateString);
System.out.println("Current Date: " + currentDateString);
System.out.println("Age: " + age.getYears() + " years, " + age.getMonths() + " months, " + age.getDays() + " days");
}
}
Output
Birthdate: 1990-01-01
Current Date: 2023-09-28
Age: 33 years, 8 months, 27 days
Explanation
-
This program uses the java.time.LocalDate class to work with dates.
-
It defines the birthdate using LocalDate.of().
-
The current date is obtained using LocalDate.now().
-
The age is calculated using Period.between().
Calculate Age in Java Using External Library
Here's an age calculator program in Java using external libraries like Joda-Time (although it's deprecated in favor of the java.time package):
Please note that you may need to include the Joda-Time library in your project to use this method.
Code
import org.joda.time.DateTime;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class AgeCalculator {
public static void main(String[] args) {
// Define the birthdate
String birthDateString = "1990-01-01"; // Replace with the birthdate you want to calculate from
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime birthDate = DateTime.parse(birthDateString, dateFormatter);
// Get the current date
DateTime currentDate = DateTime.now();
// Calculate the age
Years age = Years.yearsBetween(birthDate, currentDate);
// Format and display the result
System.out.println("Birthdate: " + birthDateString);
System.out.println("Current Date: " + currentDate.toString(dateFormatter));
System.out.println("Age: " + age.getYears() + " years");
}
}
Output
Birthdate: 1990-01-01
Current Date: 2023-09-28
Age: 33 years
Explanation
This program uses the Joda-Time library for date and time calculations.
-
It defines the birthdate as a string and parses it into a DateTime object using a specified date format.
-
The current date is obtained using DateTime.now().
- The age is calculated using Years.yearsBetween().
Learn Next: