Calculate the age of a person in Android Java

2

How can you obtain the age of a person having your birth date in Date format?

DateFormat dateFormat = dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date dob = dateFormat.parse("1989-10-10");
    
asked by Webserveis 29.09.2017 в 22:45
source

2 answers

4

There are several ways to do the calculation. Here are two of them:

  • java.time.chrono.ChronoPeriod

    The java.time package can be used from the API level 26. Example:

    Code:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", 
            Locale.getDefault());
    ChronoLocalDate from = ChronoLocalDate.from(formatter.parse("1989-10-10"));
    ChronoLocalDate to = ChronoLocalDate.from(formatter.parse("2017-09-29"));
    ChronoPeriod period = ChronoPeriod.between(from, to);
    
    Formatter fmt = new Formatter();
    if (period.get(ChronoUnit.YEARS) > 0) {
        fmt.format("%d años ", period.get(ChronoUnit.YEARS));
    }
    if (period.get(ChronoUnit.MONTHS) > 0) {
        fmt.format("%d meses ", period.get(ChronoUnit.MONTHS));
    }
    if (period.get(ChronoUnit.DAYS) > 0) {
        fmt.format("%d días ", period.get(ChronoUnit.DAYS));
    }
    System.out.println(fmt.toString());
    

    Exit:

    27 años 11 meses 19 días 
    
  • java.util.GregorianCalendar

    You can also use java.util.GregorianCalendar , although it is not as accurate:

    Code:

    DateFormat dateFormat = dateFormat = new SimpleDateFormat("yyyy-MM-dd", 
            Locale.getDefault());
    Date dob = dateFormat.parse("1989-10-10");
    GregorianCalendar cal = (GregorianCalendar) Calendar.getInstance();
    cal.setGregorianChange(new Date(Long.MIN_VALUE));
    cal.clear();
    cal.set(Calendar.YEAR, 0);
    cal.setTimeInMillis( cal.getTimeInMillis() + new Date().getTime() - dob.getTime());
    
    Formatter fmtr = new Formatter();
    if (cal.get(Calendar.YEAR) > 0) {
        fmtr.format("%d años ", cal.get(Calendar.YEAR));
    }
    if (cal.get(Calendar.MONTH) > 0) {
        fmtr.format("%d meses ", cal.get(Calendar.MONTH));
    }
    if (cal.get(Calendar.DAY_OF_MONTH) > 0) {
        fmtr.format("%d días ", cal.get(Calendar.DAY_OF_MONTH));
    }
    System.out.println(fmtr.toString());
    

    Exit:

    27 años 11 meses 21 días 
    
answered by 29.09.2017 / 23:23
source
2

One method would be to apply the format "yyyyMMdd" to the date of birth and current, a subtraction is made and divided by 1000, this way we can obtain the number of years, for this you can perform a method:

public  int getEdad(Date fechaNacimiento, Date fechaActual) {
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    int dIni = Integer.parseInt(formatter.format(fechaNacimiento));
    int dEnd = Integer.parseInt(formatter.format(fechaActual));
    int age = (dEnd-dIni)/10000;
    return age;
}

and apply it in this way, using the date of birth "1989-10-10":

   try {
            DateFormat dateFormat = dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            Date fechaNacimiento = dateFormat.parse("1989-10-10");
            Calendar cal = Calendar.getInstance();
            Date fechaActual = cal.getTime();

            System.out.println("Edad : " + String.valueOf(getEdad(fechaNacimiento, fechaActual)));

        } catch (ParseException e) {
            e.printStackTrace();
        }

having as output:

Edad : 27
    
answered by 30.09.2017 в 01:44