How to create method for age calculation method in android

前端 未结 7 1232
情话喂你
情话喂你 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条回答
  •  猫巷女王i
    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;
        }
    

提交回复
热议问题