Cálculo de la edad desde la fecha de nacimiento de las personas nacidas antes del inicio del tiempo de Java

I want to calculate the age of patient. The main issue is the calculation of patients who have born before the start of Java Time. This code works fine in Java console application, but fails in the JSF and JPA based Entity. I only record data of birth in the Entity and Age is a transient property. I can not figure out why the same code not working in Java EE app.

public String getAge() {
    System.out.println("getting age");
    if (person == null) {
        System.out.println("patient is null");
        age = "";
        return age;
    }
    if (person.getDob() == null) {
        System.out.println("dob is null");
        age = "";
        return age;
    }
    System.out.println("this = " + this);
    System.out.println("Person = " + Person);

    Date dob = person.getDob();
    System.out.println("dob = " + dob);

    if (dob == null) {
        System.out.println("dob is null");
        age = "";
        return age;
    }


    long temAge;
    long dobTime = dob.getTime();
    System.out.println("dobTime = " + dobTime);
    long nowTime = Calendar.getInstance().getTime().getTime();
    System.out.println("nowTime = " + nowTime);

    if (dobTime < 0) {
        System.out.println("dobTime = " + dobTime);
        temAge = (nowTime + Math.abs(dobTime));
        System.out.println("nowTime = " + nowTime);
        System.out.println("Math.dob = " + Math.abs(dobTime));
        System.out.println("temAge = " + temAge);
        temAge = temAge / (1000 * 60 * 60 * 24);
        System.out.println("temAge = " + temAge);
        System.out.println("age in days before is minus. now age in days is " + temAge);
    } else {
        temAge = (nowTime - dobTime) / (1000 * 60 * 60 * 24);
        System.out.println("age in days before is minus. now age in days is " + temAge);
    }
    if (temAge < 60) {
        age = temAge + " days";
    } else if (temAge < 366) {
        age = (temAge / 30) + " months";
    } else {
        age = (temAge / 365) + " years";
    }
    System.out.println("age is " + age);
    return age;
}

Editar:

After going through Kaushal's Answer, I changed my code. It was really easy to work with jodatime. Thanks Kaushal for the help.

public static String getAge() {
        LocalDate birthdate = new LocalDate(getDob());
        LocalDate now = new LocalDate();
        Years ageInYears;
        ageInYears = Years.yearsBetween(birthdate, now);
        if (ageInYears.getYears() > 0) {
            return ageInYears.getYears() + " Years";
        } else {
            Months ageInMonths = Months.monthsBetween(birthdate, now);
            if (ageInMonths.getMonths() > 0) {
                return ageInMonths.getMonths() + " Months";
            } else {
                Days ageInDays = Days.daysBetween(birthdate, now);
                return ageInDays.getDays() + " Days";
            }
        }
    }

preguntado el 27 de noviembre de 13 a las 04:11

What is meant by doesn't work? -

Wrong calculations. I will give exact figures as soon as the glassfish starts running. -

2 Respuestas

If possible try using tiempojoda. Its a very nice utility for dates.

public static void main(String[] args) {
    LocalDate birthdate = new LocalDate (1958, 1, 20);
    LocalDate now = new LocalDate();
    Years age = Years.yearsBetween(birthdate, now);
    System.out.println(age.getYears());
}

What do you mean by it fails. Do you get some error or invalid age?

respondido 27 nov., 13:05

Thanks. I was a bit worries to add libraries just for one calculation. If no other solution is available, I will go for this. Thanks. - Buddhika Ariyaratne

That was really cool. As comments can not be more than few characters here, I added my code according to your answer to my question. - Buddhika Ariyaratne

Years years = Years.yearsBetween(new LocalDate(model.getDob()), new LocalDate()); years.getYears();

model.getDob() is java.util object

Respondido 28 ago 15, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.