Skip to main content

Java Program to accept the Date of Birth and print the Age

The following code is to accept the Date of Birth of a person and than calculate his/her age according to the Date of Birth and current Date. The Age is shown in years, months, weeks and days and also includes the leap year days.
Code:-

import java.util.*;
public class Age {
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    String dob, month;
    long age, temp, day, months, year, dd, mm, yy, ww;
    Scanner scan = new Scanner(System.in);
    Integer i = 10;
    @SuppressWarnings("static-access")
    public Age(String name) {
        mm = calendar.MONTH;
        if (mm == 11) {
            month = "Dec";
        } else if (mm == 10) {
            month = "Nov";
        } else if (mm == 9) {
            month = "Oct";
        } else if (mm == 8) {
            month = "Sep";
        } else if (mm == 7) {
            month = "Aug";
        } else if (mm == 6) {
            month = "Jul";
        } else if (mm == 5) {
            month = "Jun";
        } else if (mm == 4) {
            month = "May";
        } else if (mm == 3) {
            month = "Apr";
        } else if (mm == 2) {
            month = "Mar";
        } else if (mm == 1) {
            month = "Feb";
        } else {
            month = "Jan";
        }
        name = "Your";
    }
    public long input(String name) {
        try {
            System.out.printf(name + " Date Of Birth (Example: " + month + "-" + (date.getDate()) + "-" + calendar.getWeekYear() + ") : ");
            dob = scan.nextLine();
            age = compute(dob);
        } catch (InputMismatchException e) {
            System.err.println("Error Occur!" + e.getMessage());
            System.exit(0);
        }
        daysOld();
        return age;
    }
    public long compute(String dob) {
        try {
            System.out.println("Current Date: " + (date = new Date()));
            date = new Date(dob);
            System.out.println("Date Of Birth: " + date);
            day = date.getDate();
            months = date.getMonth();
            year = date.getYear();
            date = new Date();
            dd = date.getDate();
            mm = date.getMonth();
            yy = date.getYear();
            age = yy - year;
            if (mm < months) {
                age--;
            }
            if (months == mm && dd < day) {
                age--;
            }
        } catch (Exception e) {
            System.err.println("Error Occur!\n" + e.getMessage());
            System.exit(0);
        }
        return age;
    }
    public void daysOld() {
        int leap = 0, year1, year2;
        long mill1, mill2, diff, days;
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        date = new Date();
        year1 = date.getYear();
        cal2.set(date.getYear(), date.getMonth(), date.getDate());
        date = new Date(dob);
        year2 = date.getYear();
        cal1.set(date.getYear(), date.getMonth(), date.getDate());
        mill1 = cal1.getTimeInMillis();
        mill2 = cal2.getTimeInMillis();
        diff = mill2 - mill1;
        days = diff / (24 * 60 * 60 * 1000);
        for (i = year2; i <= year1; i++) {
            if (i % 4 == 0) {
                leap++;
            }
        }
        dd = days;
        yy = days / 365;
        days %= 365;
        mm = days / 31;
        days %= 31;
        ww = days / 7;
        days %= 7;
        date = new Date(dob);
        if (date.before(date = new Date())) {
            System.out.println("Age: " + age + " Years , " + mm + " Months ,  " + ww + " Weeks & " + (days + leap) + " Days Old (Total " + (dd) + " Days & " + leap + " LY Day(s) Including)!");
        } else {
            System.err.println("Invalid Date Of Birth!\nCurrent Date is " + (date = new Date()) + " & your Date Of Birth is " + (date = new Date(dob)));
        }
    }
    public static void main(String[] args) {
        Age Ag = new Age("");
        String name = "Your";
        Ag.input(name);
    }
}
Output:-
Your Date Of Birth (Example: Mar-28-2013) : dec-08-1997
Current Date: Thu Mar 28 01:42:59 IST 2013
Date Of Birth: Mon Dec 08 00:00:00 IST 1997
Age: 15 Years , 3 Months ,  3 Weeks & 4 Days Old (Total 5589 Days & 4 LY Day(s) Including)!

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...