Age Calculator Program in Java - Java Program Example

Write a java program for Age calculate from date of birth to current date. It can also be used to calculate time difference between two dates. 
this program can calculate you age in years, months days, hours, minutes and even in seconds.

Program Code


package com.androidpro.in;

import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.PrintStream;
import
java.text.ParseException;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
java.util.Scanner;

public class
Main {

   
static int d=0,day,month;
    static void
monthCalculator(int x, Date cDate){

       
if((x<=31)&&(x>=0) ){
           
d=day;
           
month =0;
        
}
       
else if((x<=59)&&(x>31))
        {
           
d = day-31;
           
month =1;
       
}
       
else if((x<=90)&&(x>=60)) {
           
if((cDate.getYear()-100)%4==0){d =d-60;}
           
else{d=day-59;}
           
month =2;         }
       
else if((x<=120)&&(x>90)) {
           
d=day-90;
           
month =3;         }
       
else if((x<=151)&&(x>120))
        {
d=day-120;
           
month =4;         }
       
else if((x<=181)&&(x>151) )
        {
d=day-151;
           
month =5;         }
       
else if((x<=212)&&(x>181)) {
           
d=day-181;
           
month =6;         }
       
else if((x<=243)&&(x>212)) {
           
d=day-212;
           
month =7;         }
       
else if((x<=273)&&(x>243)) {
           
d= day-243;
           
month =8;         }
       
else if((x<=304)&&(x>273)) {
           
d=day-273;
           
month = 9;
           
System.out.println(d);         }
       
else if((x<=334)&&(x>304)) {
           
d=day-304;
           
month =10;         }
        
else {
           
d=day-334;
           
month = 11;}              }
   
public static void main(String []args)
    {
        System.
out.println("Enter Date Of Birth");
       
System.out.println("dd-MM-yyyy");
       
Date iDate =new Date();
        
Scanner xx = new Scanner(System.in);
       
String InputDate = xx.next();
       
xx.close(); String [] arr = {"dd/MM/yyyy","dd:MM:yyyy","dd-MM-yyyy","dd/MM/yyyy-hh:mm:ss","dd:MM:yyyy-hh:mm:ss","dd-MM-yyyy-hh:mm:ss"};
        int
t = 0;
        for
(String s:arr){
           
try{ iDate =(new SimpleDateFormat(s)).parse(InputDate);}
           
catch(ParseException e) {t++; if(t==arr.length){
                System.
out.println("An error ocurred");
               
System.out.println("possible solutions :"                     +'\n'+ "1>Enter the complete date. "                     +'\n'+ "2>Check for the errors in date format."                             );         } }}
        Date cDate =
new Date();
        long
x = cDate.getTime()-iDate.getTime();
        int
z =(int)(x/(86400000)); int y = (int)(z/365.25); day = z%365; if(y>4) {
       
int i = (int)(y/4);     day = day-i;
        if
(day<0) {
            y= y-
1;
           
day=day+365;              } }
        monthCalculator(
day,cDate);
        if
(z>0){ System.out.println("you are " +y+ " years " + month+ " months and " +d+ " days old.");
           
System.out.println("\nIn months you are "+ (y*12+month) + " months and "+d+" days old");
           
System.out.println("\nIn weeks you are "+ (int)(x/(7*24*3600000))+" weeks and "+(x/(24*3600000))%7+ " days old");
           
System.out.println("\nIn Days you are " + x/(24*3600000) + " days old.");
           
System.out.println("\nIn Hours you are "+x/3600000+ " hours old.");
           
System.out.println("\nin Minutes you are " + x/60000+ " minutes old");
           
System.out.println("\nin Seconds you are " + x + " seconds old"); }
    }


}




Output

Enter Date Of Birth

dd-MM-yyyy

12-11-1996

you are 25 years 1 months and 20 days old.

In months you are 301 months and 20 days old

In weeks you are 1311 weeks and 5 days old

In Days you are 9182 days old.

In Hours you are 220389 hours old.

in Minutes you are 13223394 minutes old

in Seconds you are 793403681668 seconds old

Process finished with exit code 0