2011-03-13 59 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; 
}