2

I have date of birth and can get the current date.

In java, how can I calculate someone's age while taking leap year into account?

Edit: Can I use unix timestamps and compare the difference?

Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96
  • 7
    Use Java 8's Time API or JodaTime [for example](http://stackoverflow.com/questions/29737159/how-can-i-create-a-calculation-to-get-the-age-of-a-person-from-two-dates/29738430#29738430) – MadProgrammer May 27 '15 at 07:13
  • 1
    What exactly do you mean by _"taking leap year into account"_? – Jim Garrison May 27 '15 at 07:14
  • 2
    You think you don't get older on a leap year? – Stultuske May 27 '15 at 07:15
  • 1
    This is an XY question. Please explain what you are trying to accomplish that means you need to account for anything to do with leap years when determining age. – Jim Garrison May 27 '15 at 07:16
  • It was an interview question. I thought it was sufficient to just say that there are 365 days in a year, 24 hours in a day, 60 minutes in an hour and 60 seconds in a minute. He asked me how I would take leap year into account. Apparently doing the way I did doesn't take leap year into account because there aren't actually 365 days in a year in reality. – Brian T Hannan May 27 '15 at 07:22
  • @BrianTHannan if that was an interview question, my answer is: Don't start working there. Unless the correct answer was "I'd use a library for that". – André Stannek May 27 '15 at 07:25
  • You probably came up with a timeMillis calculation in your interview. The interviewer asked about leap years to make you see that this isn't accurate; you need to use a calendar of some sort to calculate age (since our age is determined by the calendar, not by the milliseconds gone by) – Adriaan Koster May 27 '15 at 07:35

5 Answers5

8

As you may know that java 8 date and time API changes are inspired from Jodatime library itself, so out next solution using java 8 looks almost similar to above code sample:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);

//Now access the values as below
System.out.println(p.getDays());
System.out.println(p.getMonths());
System.out.println(p.getYears());
Community
  • 1
  • 1
Yogesh Funde
  • 767
  • 6
  • 13
1
    LocalDate birthdate = new LocalDate (1990, 12, 2);
    LocalDate now = new LocalDate();
    Years age = Years.yearsBetween(birthdate, now);
Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
1

In Java 8:

LocalDate startDate = LocalDate.of(1987, Month.AUGUST, 10);
LocalDate endDate = LocalDate.of(2015, Month.MAY, 27);

long numberOfYears = ChronoUnit.YEARS.between(startDate, endDate);

Great examples for using dates with Java 8: Java 8 Date Examples

NDY
  • 3,527
  • 4
  • 48
  • 63
0

As @MadProgrammer has suggested, you could use JodaTime.

Here's the sample code.

LocalDate birthdate = new LocalDate (1970, 1, 20);
LocalDate now = new LocalDate();
Years age = Years.yearsBetween(birthdate, now);
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
-2

What about:

    Date birthDate = new Date(85, 03, 24);

    GregorianCalendar birth = new GregorianCalendar();
    birth.setTime(birthDate);
    int month = birth.get(GregorianCalendar.MONTH);
    int day = birth.get(GregorianCalendar.DAY_OF_MONTH);

    GregorianCalendar now = new GregorianCalendar();

    int age = now.get(GregorianCalendar.YEAR) - birth.get(GregorianCalendar.YEAR);

    int birthMonth = birth.get(GregorianCalendar.MONTH);
    int birthDay = birth.get(GregorianCalendar.DAY_OF_MONTH);
    int nowMonth = now.get(GregorianCalendar.MONTH);
    int nowDay = now.get(GregorianCalendar.DAY_OF_MONTH);

    if (nowMonth>birthMonth) {
        age = age+1;
    } else {
        if (nowMonth == birthMonth) {
            if (nowDay >= birthDay) {
                age= age+1;
            }
        }
    }   
    System.out.println("Now it is my " + age+ " year of life");
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32