How do I calculate someone's age in Java?

Posted by nojevive on Stack Overflow See other posts from Stack Overflow or by nojevive
Published on 2009-07-12T14:32:13Z Indexed on 2012/06/04 10:40 UTC
Read the original article Hit count: 140

Filed under:
|
|

I want to return an age in years as an int in a Java method. What I have now is the following where getBirthDate() returns a Date object (with the birth date ;-)):

public int getAge() {
	long ageInMillis = new Date().getTime() - getBirthDate().getTime();

	Date age = new Date(ageInMillis);

	return age.getYear();
}

But since getYear() is deprecated I'm wondering if there is a better way to do this? I'm not even sure this works correctly, since I have no unit tests in place (yet).

© Stack Overflow or respective owner

Related posts about java

Related posts about date