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
Here is a Java method called getAge which takes integers for year month and day and returns a String type which holds an integer that represents age in years.
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;
}
This is the shortest I could get it to.
static int calculateAge(Calendar birthDay){
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - birthDay.get(Calendar.YEAR);
if (birthDay.get(Calendar.DAY_OF_YEAR) < today.get(Calendar.DAY_OF_YEAR)) {
age--;
}
return age;
}
For the sage of completeness and being up-to-date concerning packages, here is the way using java.time
(Java 8+).
Java
public int getAge(int year, int month, int dayOfMonth) {
return Period.between(
LocalDate.of(year, month, dayOfMonth),
LocalDate.now()
).getYears();
}
Kotlin
fun getAge(year: Int, month: Int, dayOfMonth: Int): Int {
return Period.between(
LocalDate.of(year, month, dayOfMonth),
LocalDate.now()
).years
}
Both snippets need the following imports from java.time
:
import java.time.LocalDate;
import java.time.Period
It's not recommended to use java.util.Date
and java.util.Calendar
anymore except from situations where you have to involve considerably large amounts of legacy code.
See also Oracle Tutorial.
For projects supporting Java 6 or 7, this functionality is available via the ThreeTenBP,
while there is special version, the ThreeTenABP for API levels below 26 in Android.
UPDATE
There's API Desugaring now in Android, which makes (a subset of) java.time
directly available (no backport library needed anymore) to API levels below 26 (not really down to version 1, but will do for most of the API levels that should be supported nowadays).
String getAgeInOther(int year, int month, int day) {
Calendar today = Calendar.getInstance();
Calendar birth = Calendar.getInstance();
birth.set(year, month, day);
Calendar temp = Calendar.getInstance();
temp.set(year, month, day);
int totalDays = 0;
int intMonth=0,intDays=0;
for (int iYear = birth.get(Calendar.YEAR); iYear <= today.get(Calendar.YEAR); iYear++) {
if (iYear == today.get(Calendar.YEAR) && iYear == birth.get(Calendar.YEAR)) {
for (int iMonth = birth.get(Calendar.MONTH); iMonth <= today.get(Calendar.MONTH); iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == today.get(Calendar.MONTH)) && (iMonth == birth.get(Calendar.MONTH))) {
totalDays += today.get(Calendar.DAY_OF_MONTH) - birth.get(Calendar.DAY_OF_MONTH);
} else if ((iMonth != today.get(Calendar.MONTH)) && (iMonth != birth.get(Calendar.MONTH))) {
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
intMonth++;
}else if ((iMonth == birth.get(Calendar.MONTH))) {
totalDays +=( birth.getActualMaximum(Calendar.DAY_OF_MONTH)- birth.get(Calendar.DAY_OF_MONTH));
} else if ((iMonth == today.get(Calendar.MONTH))){
totalDays += today.get(Calendar.DAY_OF_MONTH);
if (birth.get(Calendar.DAY_OF_MONTH)<today.get(Calendar.DAY_OF_MONTH))
{
intMonth++;
intDays=today.get(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH);
}else {
temp.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH)-1,1);
intDays=temp.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH)+today.get(Calendar.DAY_OF_MONTH);
}
}
}
} else if ((iYear != today.get(Calendar.YEAR)) && (iYear != birth.get(Calendar.YEAR))) {
for (int iMonth = 0; iMonth < 12; iMonth++) {
temp.set(iYear, iMonth, 1);
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
intMonth++;
}
} else if (((iYear) == birth.get(Calendar.YEAR))) {
for (int iMonth = birth.get(Calendar.MONTH); iMonth < 12; iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == birth.get(Calendar.MONTH))) {
totalDays += (birth.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH));
} else {
intMonth++;
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
} else if (iYear == today.get(Calendar.YEAR)) {
for (int iMonth = 0; iMonth <= today.get(Calendar.MONTH); iMonth++) {
temp.set(iYear, iMonth, 1);
if ((iMonth == today.get(Calendar.MONTH))) {
totalDays += today.get(Calendar.DAY_OF_MONTH);
if (birth.get(Calendar.DAY_OF_MONTH)<today.get(Calendar.DAY_OF_MONTH))
{
intMonth++;
intDays=today.get(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH);
}else {
temp.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH)-1,1);
intDays=temp.getActualMaximum(Calendar.DAY_OF_MONTH)-birth.get(Calendar.DAY_OF_MONTH)+today.get(Calendar.DAY_OF_MONTH);
}
} else {
intMonth++;
totalDays += temp.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
}
}
int ageYear=intMonth/12;
int ageMonth=intMonth%12;
int ageDays=intDays;
//TODO if you want age in YEAR:MONTH:DAY REMOVE COMMENTS
//TODO return ageYear+":"+ageMonth+":"+ageDays;
return ""+totalDays;//todo TOTAL AGE IN DAYS
}
New Language Dart using DateTime
static int getPerfectAgeInYears(DateTime dob,DateTime today) {
dob = DateTime(dob.year,dob.month,dob.day);
int ageInteger = 0;
today = DateTime(today.year,today.month,today.day);
ageInteger = today.year-dob.year;
if (today.month == dob.month) {
if (today.day < dob.day) {
ageInteger = ageInteger - 1;
}
} else if (today.month < dob.month) {
ageInteger = ageInteger - 1;
}
return ageInteger;}
Call as print(getPerfectAgeInYears(DateTime(2000,6,4),DateTime.now())); 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
Now for kotlin Language:
import java.util.Calendar
fun main(args: Array<String>) {
print(getAge(yyyy,mm,dd))
}
fun getAge(year: Int, month: Int, day: Int): String {
val dob = Calendar.getInstance()
val today = Calendar.getInstance()
dob.set(year, month, day)
var age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
age--
}
val ageInt = age + 1
return ageInt.toString()
}