0

Possible Duplicates:
How can I calculate the age of a person in year, month, days?
How can I calculate the difference between two dates

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
if(petDetails.getDateOfDeath() != null){
    String  formatedDateOfDeath = formatter.format(petDetails.getDateOfDeath());
    String formateDateOfBirth = formatter.format(petDetails.getDateOfBirth());
}

How can i calculate the age of death from the above. I dont want to use any externallibraries

EDIT: please look at what I've got so far.none of the other threads are like mine. most of them are about date from DOB to today and not in the format im using.

Community
  • 1
  • 1
124697
  • 22,097
  • 68
  • 188
  • 315
  • possible duplicate of [How can I calculate the age of a person in year, month, days?](http://stackoverflow.com/questions/453208/how-can-i-calculate-the-age-of-a-person-in-year-month-days). See also [this answer](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c/11942#11942). – Matt Ball Mar 04 '11 at 15:15
  • Or http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java – tim_yates Mar 04 '11 at 15:17
  • 4
    Possible duplicate of his previous question from an hour ago: http://stackoverflow.com/questions/5194216/how-can-i-calculate-the-difference-between-two-dates-closed – Riggy Mar 04 '11 at 15:17
  • Why don't you use a calendar or date object? http://download.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html http://download.oracle.com/javase/1.4.2/docs/api/java/util/Date.html – Mike Mar 04 '11 at 15:17

2 Answers2

0

Try this:

public class Age {

public static void main(String[] args) {
        Calendar birthDate = new GregorianCalendar(1979, 1, 1); 
        Calendar deathDate = new GregorianCalendar(2011, 1, 1);
        int age = deathDate.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR);
        if ((birthDate.get(Calendar.MONTH) > deathDate.get(Calendar.MONTH))
            || (birthDate.get(Calendar.MONTH) == deathDate.get(Calendar.MONTH) && birthDate.get(Calendar.DAY_OF_MONTH) > deathDate
                .get(Calendar.DAY_OF_MONTH))) {
          age--;
        }
        System.out.println(age);
      }

}

blong824
  • 3,920
  • 14
  • 54
  • 75
0

You can solve this without converting them to strings. since the getDateOfBirth and getDateOfDeath return date objects, you can use the .getTime() method on them which Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

A fairly simple way of doing this could be
long millisecondsDiff = petDetails.getDateOfDeath().getTime - petDetails.getDateOfBirth().getTime;

You can then either create a new date object directly from this long, or you can do the proper calculations to change milliseconds into days. ie
long age = millisecondsDiff / (1000 * 60* 60 * 24);

Mike
  • 8,137
  • 6
  • 28
  • 46
  • That'll only work correctly if every year had exactly the same amount of seconds and days, which as we know isn't true and could lead to edge cases I think. – Voo Mar 04 '11 at 15:41
  • from this http://www.java2s.com/Code/Java/Development-Class/Computedaysbetween2dates.htm it looks like they add one hour to the difference before dividing it by days. Maybe that helps with the corner cases? – Mike Mar 04 '11 at 15:48