如何在android中创建年龄计算方法

时间:2011-03-13 18:39:53

标签: java android

我想写一个方法来计算出生日期的年龄,逻辑是否正确以及如何在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;
}

7 个答案:

答案 0 :(得分:50)

以下是我解决问题的方法:

/**
 * 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;  
}

我使用DatePicker来获取此处所需的输入值。此方法与日期选择器一起专门用于获取用户的DoB并计算其年龄。可以进行轻微修改以允许用户DoB的String输入,具体取决于您的具体实现。 String的返回类型用于更新TextView,可以进行轻微的mod以允许类型int输出。

答案 1 :(得分:12)

这完美地运作.....

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;
    }

答案 2 :(得分:6)

以下是您应该可以使用的Android年龄计算示例:

    public int getAge (int _year, int _month, int _day) {

            GregorianCalendar cal = new GregorianCalendar();
            int y, m, d, a;         

            y = cal.get(Calendar.YEAR);
            m = cal.get(Calendar.MONTH);
            d = cal.get(Calendar.DAY_OF_MONTH);
            cal.set(_year, _month, _day);
            a = y - cal.get(Calendar.YEAR);
            if ((m < cal.get(Calendar.MONTH))
                            || ((m == cal.get(Calendar.MONTH)) && (d < cal
                                            .get(Calendar.DAY_OF_MONTH)))) {
                    --a;
            }
            if(a < 0)
                    throw new IllegalArgumentException("Age < 0");
            return a;
    }

答案 3 :(得分:1)

这是经过全面测试和运作的代码

以下是使用日期选择器对话框的示例

@Override
    public void onDateSet(DatePicker view, int mBirthYear, int mMonthOfYear, int mDayOfMonth) 
    {
            mAge = year - mBirthYear;

            if( (mMonthOfYear == month && day < mDayOfMonth) || ( month < mMonthOfYear) ) 
            {
                mAge--;
            }

            String years = getString(R.string.years);

            etAge.setText(mAge+years);      

    }

答案 4 :(得分:1)

private String getAge(int year, int month, int day){

  Calendar dob = Calendar.getInstance();

  Calendar today = Calendar.getInstance();

  dob.set(year, month, day);

  today.set(today.get(Calendar.YEAR), today.get(Calendar.MONTH) + 1,
    today.get(Calendar.DATE));

  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;
}

答案 5 :(得分:0)

所有发布的解决方案都不适合我。所以,我按照自己的逻辑运行,得到了这个,对我来说100%有效:

 Integer getAge(Integer year, Integer month, Integer 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(month == (today.get(Calendar.MONTH)+1) && day > today.get(Calendar.DAY_OF_MONTH)) {
         age--;
     }

     return age;
 }

答案 6 :(得分:0)

以下内容将以最准确的方式为您提供年龄

public static String getAgeFromLong(String birthday) {

    SimpleDateFormat sdf= new SimpleDateFormat("ddMMyyyy",Locale.ENGLISH);
    Calendar dobCal=Calendar.getInstance();
    dobCal.setTime(sdf.parse(birthday));

    Calendar diffCal = Calendar.getInstance();
    diffCal.setTimeInMillis(Calendar.getInstance().getTimeInMillis() - dobCal.getInstance().getTimeInMillis());

    String ageS = "";

    int age = diffCal.get(Calendar.YEAR) - 1970;
    //Check if less than a year
    if (age == 0) {
        age = diffCal.get(Calendar.MONTH);
        //Check if less than a month
        if (age == 0) {
            age = diffCal.get(Calendar.WEEK_OF_YEAR);
            //Check if less than a week
            if (age == 1) {
                age = diffCal.get(Calendar.DAY_OF_YEAR);
                ageS = (age - 1) + " Days";
            } else {
                ageS = (age - 1) + " Weeks";
            }
        } else {
            ageS = age + " Months";
        }
    } else {
        ageS = age + " Years";
    }

    return ageS;
}