1

i have users signing up entering their birth date, and the application saves the birthdate as yyyy-MM-dd.

How can I use this along with java code to determine the persons age?

Is there a way to convert this to days, and use that to determine the difference? Or is there a better way to go about this

EDIT

The main problem I am having is using the date as it is currently formatted, to be able to convert it. It is being pulled via an API call from a database that saves it in that format. I have seen ways using Joda to convert, but thats using a different format

Jake Weso
  • 535
  • 1
  • 6
  • 14
  • All you really need is the difference between the dates, as seen here: http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java – durbnpoisn Aug 14 '14 at 19:14

1 Answers1

3

Here's an example from here: http://howtodoinjava.com/2014/05/26/java-code-to-calculate-age-from-date-of-birth/

+1 to using JodaTime.

LocalDate birthdate = new LocalDate (1970, 1, 20); // Birth date
LocalDate now = new LocalDate(); // Today's date
Period period = new Period(birthdate, now, PeriodType.yearMonthDay());
//Now access the values as below
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.getYears());
Kenny Cason
  • 11,382
  • 9
  • 42
  • 71