2011-03-13 51 views
13

我想写一个方法,从诞生之日起计算年龄的年龄计算方法方法,是逻辑正确,以及如何把它写在android系统的Java:如何创建于Android的

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

回答

48

这里是我的问题的解决方案:

/** 
* 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的返回类型用于更新TextView,也可以使用轻微的Mod来允许输出int输出。

+0

我喜欢你的方式使用day_of_year,聪明:) – Heba 2011-03-15 06:55:33

+0

我用DatePicker也得到了DOB,但我将它转换为字符串以便将其存储在数据库(SQLite)中,并且每次我需要年龄计算我将dob作为参数返回,所以我添加了substring方法来获取年,月和日。 – Heba 2011-03-15 06:56:20

+0

我在SO上使用了一些答案构建了这个解决方案,我相信'Calendar.DAY_OF_YEAR'来自指向www.coderanch.com的答案。请享用! – Kingsolmn 2011-03-15 16:26:53

5

这里是一个例如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; 
    } 
+0

非常感谢你它帮助我很多。 – Heba 2011-03-14 08:49:04

+0

@Jon Egerton链接是禁用更改它。 – Ironman 2016-05-26 12:35:22

+0

链接不工作.. PLZ修复它或删除它。 – 2017-12-05 05:17:59

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

    } 
8

这工作完全.....

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

它的工作正常...以上所有的例子都给错误的年龄,当你输入27-11-1989,我的年龄是27,但它显示26 ... – 2016-12-07 06:41:12

+0

这对我工作!谢谢 – Darsh 2017-03-27 10:59:20

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