Here is my solution to the problem:
/**
* 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;
}
I used a DatePicker to get the input values required here. This method, together with the date picker, is specifically to get the user's DoB and calculate their age. A slight modification can be made to allow for String input(s) of the user's DoB, depending upon your specific implementation. The return type of String is for updating a TextView, a slight mod can be made to allow for type int
output also.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…