How to create method for age calculation method in android

前端 未结 7 1230
情话喂你
情话喂你 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 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;
    }
    

提交回复
热议问题