3

Possible Duplicate:
How do I calculate someone's age in Java?

hi friends

i want to calculate total age of any registered person from birthdate to current date ...i want to count total years and month and days in between birtdate and current date..

for example=birthdate=8th feb 2008 currentdate=8th april2011 so i want answer is =3 years ,2 months

Community
  • 1
  • 1
sam_k
  • 5,983
  • 14
  • 76
  • 110
  • Tempted to close as duplicate of [question #9](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c), but that’s C# and this is Java =p – Josh Lee Apr 08 '11 at 13:24
  • tell me the logic simple yar... – sam_k Apr 08 '11 at 13:25
  • The logic is not simple, because of the varying lengths of days in each month. It will be easiest to use a library as recommended. – roberttdev Apr 08 '11 at 13:45

2 Answers2

4

Here is an example using http://joda-time.sourceforge.net/

import org.joda.time.DateTime;
import org.joda.time.Period;

public class Main{
  public static void main(String[] args) {
      DateTime start = new DateTime(2008, 2, 8, 0, 0, 0, 0);
      DateTime end = new DateTime();
      Period period = new Period(start, end);
      System.out.println(" user is " + period.getYears() + " years " + period.getMonths() + " months old");
  }
}
Kyle
  • 3,170
  • 1
  • 17
  • 20
1

Have a look at http://joda-time.sourceforge.net/ - it's got a pretty nice API. I was using it the other week to do some work with dates. Hopefully it'll have what you need.

Ian Oxley
  • 10,916
  • 6
  • 42
  • 49