How to calculate age, depending on the day on which it is calculated

1

I am making an application, where when entering the date of birth I calculate the age, but the key is that if the next birthday is closer to the previous one already celebrated, I will increase one year at the age. I have this code but I can not get it to work well, especially if I meet at the beginning of the year and we are at the end, I do not add a year.

private String getAge(int year, int month, int day) {
        Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();

        dob.set(year, month, day);
        int diaAsegurado = dob.get(Calendar.DAY_OF_YEAR);
        int diaHoy = today.get(Calendar.DAY_OF_YEAR);

        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

        if (diaHoy < diaAsegurado) {
            if ((diaAsegurado - diaHoy) > (today.getActualMaximum(Calendar.DAY_OF_YEAR) / 3)) {
                age--;
            }
        }

        Integer ageInt = new Integer(age);
        String ageS = ageInt.toString();

        return ageS;
    }
    
asked by jechu85 27.09.2018 в 19:13
source

2 answers

2

This is a method to obtain the age according to the day, month and year of birth, as you comment a check must be made to determine if the birthday is later than the current date, that is, if today is the same month but the day has not yet arrived:

public static int calcularEdad(int day, int month, int year) {
    Calendar fechaNacimiento = new GregorianCalendar(year, month, day);
    Calendar fechaActual = Calendar.getInstance();

    //Calcula diferencias.
    int years = fechaActual.get(Calendar.YEAR) - fechaNacimiento.get(Calendar.YEAR);
    int months = fechaActual.get(Calendar.MONTH) - fechaNacimiento.get(Calendar.MONTH);
    int days = fechaActual.get(Calendar.DAY_OF_MONTH) - fechaNacimiento.get(Calendar.DAY_OF_MONTH);

    if(months < 0 //Aun no es mes de cumpleaños.
        || (months == 0 && days < 0)){//Es el mes pero no ha llegado el día.
        years--; //Se resta 1 a la diferencia de años.
    }
    return years;
}

This is an example of use:

//14 de febrero de 1083
String edad = calcularEdad(14, 2, 1983);

to obtain a value in the age variable of:

35
    
answered by 27.09.2018 в 23:02
1

Hi, I have been able to solve this issue, I pass the code in case someone is interested. calculates your age and increases one year if you are closer to turning years than the one you have already met (I was born in January and I am 33 years old, because the system tells me that I have 34, since I am closer to the 34 than the 33 now in October).

select birth = where I enter my age of birth (editext) CompleteBuy Date = variant where I store the complete date MM / dd / yyyy Birthdate = variant where I store the calculated age

            String fechaInicio = seleccionarnacimiento.getText().toString();
            String fechaActual = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
            Date date = new Date();
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
            String[] aFechaIng = fechaInicio.split("/");
            Integer diaInicio = Integer.parseInt(aFechaIng[0]);
            Integer mesInicio = Integer.parseInt(aFechaIng[1]);
            Integer anioInicio = Integer.parseInt(aFechaIng[2]);

            String[] aFecha = fechaActual.split("/");
            Integer diaActual = Integer.parseInt(aFecha[0]);
            Integer mesActual = Integer.parseInt(aFecha[1]);
            Integer anioActual = Integer.parseInt(aFecha[2]);


            int b = 0;
            int dias = 0;
            int mes = 0;
            int anios = 0;
            int meses = 0;
            mes = mesInicio - 1;
            if (mes == 2) {
                if ((anioActual % 4 == 0) && ((anioActual % 100 != 0) || (anioActual % 400 == 0))) {
                    b = 29;
                } else {
                    b = 28;
                }
            } else if (mes <= 7) {
                if (mes == 0) {
                    b = 31;
                } else if (mes % 2 == 0) {
                    b = 30;
                } else {
                    b = 31;
                }
            } else if (mes > 7) {
                if (mes % 2 == 0) {
                    b = 31;
                } else {
                    b = 30;
                }
            }
            if ((anioInicio > anioActual) || (anioInicio == anioActual && mesInicio > mesActual)
                    || (anioInicio == anioActual && mesInicio == mesActual && diaInicio > diaActual)) {
            } else {
                if (mesInicio <= mesActual) {
                    anios = anioActual - anioInicio;
                    if (diaInicio <= diaActual) {
                        meses = mesActual - mesInicio;
                        dias = b - (diaInicio - diaActual);
                    } else {
                        if (mesActual == mesInicio) {
                            anios = anios - 1;
                        }
                        meses = (mesActual - mesInicio - 1 + 12) % 12;
                        dias = b - (diaInicio - diaActual);
                    }
                } else {
                    anios = anioActual - anioInicio - 1;
                    if (diaInicio > diaActual) {
                        meses = mesActual - mesInicio - 1 + 12;
                        dias = b - (diaInicio - diaActual);
                    } else {
                        meses = mesActual - mesInicio + 12;
                        dias = diaActual - diaInicio;
                    }
                }
            }

            if (meses*30 >= 180){
                asegurado.setFechaNacimientoCompleta(seleccionarnacimiento.getText().toString());
                asegurado.setFechaNacimiento( String.valueOf(anios + 1));

            }else{
                asegurado.setFechaNacimientoCompleta(seleccionarnacimiento.getText().toString());
                asegurado.setFechaNacimiento(String.valueOf(anios));}

I inform: the original code is not mine, it is: link . I have only modified it and adapted it to my need

    
answered by 14.10.2018 в 18:50