Buddhika Ariyaratne
Buddhika Ariyaratne

Reputation: 2423

Calculating Age from Date of Birth of people born before the start of java time

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;
}

Edit :

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";
            }
        }
    }

Upvotes: 1

Views: 4150

Answers (2)

Ali Abbas
Ali Abbas

Reputation: 488

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

model.getDob() is java.util object

Upvotes: 0

Kaushal
Kaushal

Reputation: 675

If possible try using jodatime. 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?

Upvotes: 5

Related Questions