Java Age Calculate calculateAge(Date birthDate)

Here you can find the source of calculateAge(Date birthDate)

Description

This method is used to calculate Age.

License

Open Source License

Parameter

Parameter Description
birthDate a parameter

Return

int.

Declaration

public static int calculateAge(Date birthDate) 

Method Source Code

//package com.java2s;
/**//from ww w .ja  v  a2s . c  o  m
 * Copyright (C) 2013 Company. All Rights Reserved. 
 * 
 * This software is the proprietary information of Company . 
 * Use is subjected to license terms. 
 *
 * @since Jul 17, 2013 11:48:25 PM
 * @author SPA
    
 *
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * 
     * This method is used to calculate Age.
     * 
     * @param birthDate
     * @return int.
     */
    public static int calculateAge(Date birthDate) {
        int age = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(birthDate);

        age = Calendar.getInstance().get(Calendar.YEAR) - c.get(Calendar.YEAR);

        // Also need compare month and day
        if (Calendar.getInstance().get(Calendar.MONTH) < c.get(Calendar.MONTH)) {
            age--;
        } else if (Calendar.getInstance().get(Calendar.MONTH) == c.get(Calendar.MONTH)
                && Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < c.get(Calendar.DAY_OF_MONTH)) {
            age--;
        }

        return age;
    }
}

Related

  1. age(int year, int month, int date)
  2. ageAcceptable(final Date doB, final Date now, final int acceptedAge)
  3. ageInYears(java.util.Date dateUtil)
  4. calculateAge(Date aBirthdate)
  5. calculateAge(Date aDateFrom, Date aDateTo)
  6. calculateAge(Date birthday)
  7. calculateAge(Date dateOfBirth)
  8. calculateAge(Date dob)
  9. calculateAge(Date DOB)