How To Create Method For Age Calculation Method In Android
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 birthday){ // consider
Solution 1:
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.
*/privateString 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 = newInteger(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.
Solution 2:
This works Perfectly.....
publicintgetAge(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;
} elseif(DOBmonth == currentMonth) {
if(DOBday > todayDay){
--age;
}
}
return age;
}
Solution 3:
Here's an example android age calculation that you should be able to work from:
publicintgetAge (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)
thrownew IllegalArgumentException("Age < 0");
return a;
}
Solution 4:
Here is the fully tested and working code
Below is an example using date picker dialog
@OverridepublicvoidonDateSet(DatePicker view, int mBirthYear, int mMonthOfYear, int mDayOfMonth)
{
mAge = year - mBirthYear;
if( (mMonthOfYear == month && day < mDayOfMonth) || ( month < mMonthOfYear) )
{
mAge--;
}
Stringyears= getString(R.string.years);
etAge.setText(mAge+years);
}
Solution 5:
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;
}
Post a Comment for "How To Create Method For Age Calculation Method In Android"