In the previous article we have discussed about Java Program to Calculate Commission Percentage
In this article you will see how to find age from given date of birth by using Java programming language.
Java Program to Calculate Age from Birth Date
Age of a person refers to the time period during which the person has lived. Generally we calculate the age of person from his date of birth to current date.
For example:
If the date of birth of a boy is 10th April 2005 and current date is 10th April 2015. That means the age of boy is 15 years old.
Let’s see different ways to calculate the age of a person from its’ date of birth.
Method-1: Java Program to Calculate Age from Birth Date by Using Java 8 Period Class
In Java 8, we have java.time.Period
class which is used to measure a period of time in terms of years, months and days. Specifically it provides between()
method by using which you can obtain a period of time having number of years, months and days between two date.
Syntax:
public static Period.between(Start_Date, End_Date);
Approach:
- Take the input of your birth date in string format.
- Then convert that date to an instance of
LocalDate
class by usingparse()
method, which will be used as your start date. - Create another instance of
LocalDate
class to get the current date by usingnow()
method, which will be used as your end date during age calculation. - The find the time period between your date of birth and current date by using
between()
method of period class, which is your age. - Then print the age by using
getYears()
,getMonths()
andgetDays()
method.
Program:
import java.util.Scanner; import java.time.LocalDate; import java.time.Period; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); //Ask user to enter date of birth System.out.print("Enter birth date in yy-mm-dd format: "); //taking input from user String input = sc.nextLine(); //Get an instance of LocalDate class by using the parse() method, \ //here parameter is the inputted date in String format LocalDate birthDate = LocalDate.parse(input); //create an instance of LocalDate class by invoking the now() method //it will give you current date LocalDate currentDate = LocalDate.now(); //calculate the time period between your date of birth and current date //by using between() method of Period class if ((birthDate != null) && (currentDate != null)) { Period age = Period.between(birthDate, currentDate); System.out.print("Your Age: "); System.out.println(age.getYears()+" Years "+age.getMonths()+" Months "+age.getDays()+" Days"); } } }
Output: Enter birth date in yy-mm-dd format: 2000-04-10 Your Age: 21 Years 10 Months 21 Days
Method-2: Java Program to Calculate Age from Birth Date by Using ChronoUnit enum
In Java, java.time.temporal.ChronoUnit
enum provides the standards units which are used in Java Date Time API. It also provides between()
method which works similar to Period class.
Approach:
- Take the input of your birth date in string format.
- Then convert that date to an instance of
LocalDate
class by usingparse()
method, which will be used as your start date. - Create another instance of
LocalDate
class to get the current date by usingnow()
method, which will be used as your end date during age calculation. - The find the time period between your date of birth and current date by using
between()
method of ChronoUnit enum, which is your actual age. - Then print the result.
Program:
import java.util.Scanner; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class Main { public static void main(String args[]) { //Scanner class object created Scanner sc = new Scanner(System.in); //Ask user to enter date of birth System.out.print("Enter birth date in yy-mm-dd format: "); //taking input from user String input = sc.nextLine(); //Get an instance of LocalDate class by using the parse() method, \ //here parameter is the inputted date in String format LocalDate birthDate = LocalDate.parse(input); //create an instance of LocalDate class by invoking the now() method //it will give you current date LocalDate currentDate = LocalDate.now(); //calculate the time period between your date of birth and current date //by using between() method of ChronoUnit enum if ((birthDate != null) && (currentDate != null)) { long years = ChronoUnit.YEARS.between(birthDate, currentDate); System.out.print("Your Age: "+years); } } }
Output: Enter birth date in yy-mm-dd format: 2000-04-10 Your Age: 21
Method-3: Java Program to Calculate Age from Birth Date by Using Calendar Class
Approach:
- Pass your date of birth to constructor of Calendar class.
- Then get the difference between current year and birth year.
- If birth date month is greater than or equal to current date month and day of birth date is greater than day of current date then decrement age by 1.
- Print the final age.
Program:
import java.util.Calendar; import java.util.GregorianCalendar; public class Main { public static void main(String args[]) { //date of birth in yy-mm-dd format Calendar birthDate = new GregorianCalendar(2000, 04, 10); Calendar currentDate = new GregorianCalendar(); //got age in between year of date of birth and current year int age = currentDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR); //But Decrementing age by 1, if birth date month is greater than or equal to current date month //and day of birth date is greater than day of current date if ((birthDate.get(Calendar.MONTH) > currentDate.get(Calendar.MONTH)) || (birthDate.get(Calendar.MONTH) == currentDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) > currentDate.get(Calendar.DAY_OF_MONTH))) { //decrements age by 1 age--; } //prints the age System.out.println("Your current Age: "+age); } }
Output: Your current Age: 21
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding
Related Java Programs: