In this section you will learn how to calculate the age. For this purpose, through the code, we have prompted the user to enter current date and date of birth in a specified format. Now to determine the age, we have used the following code:
if(cal2.get(Calendar.DAY_OF_YEAR) <cal1.get(Calendar.DAY_OF_YEAR)) { |
Here is the code of CalculateAge.java:
|
import java.text.*; import java.util.*; public class CalculateAge{ public static void main(String[]args){ System.out.println("Enter your Date of Birth in format MM-dd-yyyy"); Scanner input=new Scanner(System.in); String dateOfBirth=input.nextLine() ; System.out.println("Enter Current date in format MM-dd-yyyy"); String currentDate=input.nextLine() ; try { Calendar cal1 = new GregorianCalendar(); Calendar cal2 = new GregorianCalendar(); int age = 0; int factor = 0; Date date1 = new SimpleDateFormat("MM-dd-yyyy").parse(dateOfBirth); Date date2 = new SimpleDateFormat("MM-dd-yyyy").parse(currentDate); cal1.setTime(date1); cal2.setTime(date2); if(cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR)) { factor = -1; } age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor; System.out.println("Your age is: "+age); } catch (ParseException e) { System.out.println(e); } } } |
Output :
| Enter your Date of Birth in format MM-dd-yyyy 8-28-1989 Enter Current date in format MM-dd-yyyy 9-23-2009 Your age is: 20 |