Calculate age from BirthDate

后端 未结 17 1342
攒了一身酷
攒了一身酷 2021-02-07 08:33

I have DatePicker Dialog, When I select date at that time I want to calculate age it\'s working but when I select date of current year at that time it showing the -1 age instead

相关标签:
17条回答
  • 2021-02-07 08:55
    public static int getPerfectAgeInYears(int year, int month, int date) {
    
        Calendar dobCalendar = Calendar.getInstance();
        
        dobCalendar.set(Calendar.YEAR, year);
        dobCalendar.set(Calendar.MONTH, month);
        dobCalendar.set(Calendar.DATE, date);
    
        int ageInteger = 0;
    
        Calendar today = Calendar.getInstance();
    
        ageInteger = today.get(Calendar.YEAR) - dobCalendar.get(Calendar.YEAR);
    
        if (today.get(Calendar.MONTH) == dobCalendar.get(Calendar.MONTH)) {
    
            if (today.get(Calendar.DAY_OF_MONTH) < dobCalendar.get(Calendar.DAY_OF_MONTH)) {
    
                ageInteger = ageInteger - 1;
            }
    
        } else if (today.get(Calendar.MONTH) < dobCalendar.get(Calendar.MONTH)) {
    
            ageInteger = ageInteger - 1;
    
        }
    
        return ageInteger;
    }
    

    Consider Today's Date - 30th August 2020

    If Birthdate - 29th July 1993, the output - 27

    If Birthdate - 29th August 1993, the output - 27

    If Birthdate - 30th August 1993, the output - 27

    If Birthdate - 31st August 1993, the output - 26

    If Birthdate - 31st September 1993, the output - 26

    0 讨论(0)
  • 2021-02-07 08:58
    public String getAge(int year, int month, int day) {
            Calendar dob = Calendar.getInstance();
            Calendar today = Calendar.getInstance();
    
            dob.set(year, month-1, 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;
        }
    
    0 讨论(0)
  • 2021-02-07 08:58
    public int getAge(int year, int month, int day) {
        final Calendar birthDay = Calendar.getInstance();
        birthDay.set(year, month, day);
        final Calendar current = Calendar.getInstance();
        if (current.getTimeInMillis() < birthDay.getTimeInMillis())
            throw new IllegalArgumentException("age < 0");
        int age = current.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
        if (birthDay.get(Calendar.MONTH) > current.get(Calendar.MONTH) ||
                (birthDay.get(Calendar.MONTH) == current.get(Calendar.MONTH) &&
                        birthDay.get(Calendar.DATE) > current.get(Calendar.DATE)))
            age--;
        return age;
    }
    
    0 讨论(0)
  • 2021-02-07 08:59
    static int calculateAge(int birthdayDay, int birthdayMonth, int birthdayYear)
    {
        DateTime date = DateTime(birthdayYear, birthdayMonth, birthdayDay).toLocal();
        DateTime now = DateTime.now().toLocal();
    
        return now.difference(date).inDays ~/ 365.2425;
    }
    
    0 讨论(0)
  • 2021-02-07 09:00
          private void calculateAge() {
        age.calcualteYear();
        age.calcualteMonth();
        age.calcualteDay();
        age.calculateMonths();
        age.calTotalWeeks();
        age.calTotalHours();
        age.calTotalMins();
        age.calTotalSecs();
        age.calTotalMilsecs();
        // Toast.makeText(getContext(), "click the resulted button"+age.getResult() , Toast.LENGTH_SHORT).show();
        result.setText("AGE (DD/MM/YY) :" + age.getResult());
    }
    

    after that create one class

     public class AgeCalculation {
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private int resYear;
    private int resMonth;
    private int resDay;
    private Calendar start;
    private Calendar end;
    public String getCurrentDate()
    {
          end=Calendar.getInstance();
          endYear=end.get(Calendar.YEAR);
          endMonth=end.get(Calendar.MONTH);
          endMonth++;
          endDay=end.get(Calendar.DAY_OF_MONTH);
          return endDay+":"+endMonth+":"+endYear;
    }
    public void setDateOfBirth(int sYear, int sMonth, int sDay)
    {
     startYear=sYear;
     startMonth=sMonth;
     startDay=sDay;
    
    }
    public void calcualteYear()
    {
        resYear=endYear-startYear/(365);
    
    }
    
    public void calcualteMonth()
    {
        if(endMonth>=startMonth)
        {
             resMonth= endMonth-startMonth;
        }
        else
        {
            resMonth=endMonth-startMonth;
            resMonth=12+resMonth;
            resYear--;
        }
    
    }
    public void  calcualteDay()
    {
    
        if(endDay>=startDay)
        {
             resDay= endDay-startDay;
        }
        else
        {
            resDay=endDay-startDay;
            resDay=30+resDay;
            if(resMonth==0)
            {
                resMonth=11;
                resYear--;
            }
            else
            {
                resMonth--;
            }
    
        }
    }
    
    public String getResult()
    {
        return resDay+":"+resMonth+":"+resYear;
    }
    
    0 讨论(0)
提交回复
热议问题