How to create method for age calculation method in android

前端 未结 7 1183
情话喂你
情话喂你 2020-12-14 09:50

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 b         


        
相关标签:
7条回答
  • 2020-12-14 09:53

    Here is the fully tested and working code

    Below is an example using date picker dialog

    @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);      
    
        }
    
    0 讨论(0)
  • 2020-12-14 09:56

    None of the posted solutions worked for me. So, I worked my own logic and got this, which works 100% for me:

     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 讨论(0)
  • 2020-12-14 10:01

    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;
        }
    
    0 讨论(0)
  • 2020-12-14 10:01

    Here's an example android age calculation that you should be able to work from:

        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 讨论(0)
  • 2020-12-14 10:01
    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 讨论(0)
  • 2020-12-14 10:06

    The below will provide you age in the most accurate way possible

    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;
    }
    
    0 讨论(0)
提交回复
热议问题