Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create method for age calculation method in android

Tags:

java

android

I want to write a method to calculate the age from the birth date, is the logic correct and how to write it in android Java:

public int calculateAge(String birthday){ 
 // consider that birthday format is ddmmyyyy;

 String today = SystemDate(ddmmyyyy);
 int bDay = birthday(1,2).toInteger;
 int bMonth = birthday(3,4).toInteger;
 int bYear = birhtday(5,8).toInteger;

 int tDay = today(1,2).toInteger;
 int tMonth = today(3,4).toInteger;
 int tYear = today(5,8).toInteger;

 if (tMonth == bMonth){
     if (tday>= bDay){ 
        age = tYear - bYear;
      else 
        age = tYear - bYear - 1;}
 else
   if (tMonth > bMonth) {
       age = tYear - bYear;
   else 
       age = tYear - bYear - 1;}
 }
 return age;
}
like image 555
Heba Avatar asked Mar 13 '11 18:03

Heba


People also ask

What is the method of age calculation?

The method of calculating age involves the comparison of a person's date of birth with the date on which the age needs to be calculated. The date of birth is subtracted from the given date, which gives the age of the person. Age = Given date - Date of birth.

Can Google Forms calculate age?

You can use the DIFF function to calculate the age based on the date of birth entered by the user. To use this feature, you need our Gsuite addon. Install this addon to customize Google Forms.

How do you calculate age in a big query?

The most basic way of calculating age is to use DATE_DIFF function to get the number of years between two dates. However, this function just subtracts the years, regardless if the date of birth has already passed or not, which is entirely inaccurate.


2 Answers

Here is my solution to the problem:

/**
 * Method to extract the user's age from the entered Date of Birth.
 * 
 * @param DoB String The user's date of birth.
 * 
 * @return ageS String The user's age in years based on the supplied DoB.
 */
private String getAge(int year, int month, int day){
    Calendar dob = Calendar.getInstance();
    Calendar today = Calendar.getInstance();

    dob.set(year, month, day); 

    int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

    if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
        age--; 
    }

    Integer ageInt = new Integer(age);
    String ageS = ageInt.toString();

    return ageS;  
}

I used a DatePicker to get the input values required here. This method, together with the date picker, is specifically to get the user's DoB and calculate their age. A slight modification can be made to allow for String input(s) of the user's DoB, depending upon your specific implementation. The return type of String is for updating a TextView, a slight mod can be made to allow for type int output also.

like image 150
Kingsolmn Avatar answered Oct 07 '22 02:10

Kingsolmn


This works Perfectly.....

public int getAge(int DOByear, int DOBmonth, int DOBday) {

        int age;

        final Calendar calenderToday = Calendar.getInstance();
        int currentYear = calenderToday.get(Calendar.YEAR);
        int currentMonth = 1 + calenderToday.get(Calendar.MONTH);
        int todayDay = calenderToday.get(Calendar.DAY_OF_MONTH);

        age = currentYear - DOByear;

        if(DOBmonth > currentMonth) {
            --age;
        } else if(DOBmonth == currentMonth) {
            if(DOBday > todayDay){
                --age;
            }
        }
        return age;
    }
like image 40
W I Z A R D Avatar answered Oct 07 '22 02:10

W I Z A R D