2 September 2014

Java Program to find your age from given date of birth


// Calculate age in years for given date of birth

import java.util.Calendar;
import java.util.Scanner;

public class AgeCalculator {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.println("Enter date of birth [dd-mm-yyyy]:");
        String dobstring = s.nextLine();

       String [] parts= dobstring.split("-");
        Calendar dob = Calendar.getInstance();
        dob.set(Integer.parseInt(parts[2]),Integer.parseInt(parts[1]),Integer.parseInt(parts[0]));

        Calendar cd = Calendar.getInstance();  //get system date
        long ms = cd.getTimeInMillis() - dob.getTimeInMillis();
        long days = ms / (1000*60*60*24);
        System.out.printf("Age in years : %d\n", days/365);

  

    }

  

}


Post a Comment