0

I have a string in the form of YYYY-MM-DDThh:mm:ss, which was extracted from an xml file.

This string represents a date of birth.

How can I find the person's age in years(integer) using java?

Cap Barracudas
  • 2,347
  • 4
  • 23
  • 54
  • 1
    Date of birth usually is just a date, not a time. If you are including a time you need to know the timezone (or assuming all calculations are in the same timezone which doesn't have daylight savings) – Peter Lawrey Apr 09 '19 at 13:28
  • To how much detail would you like to calculate age ? – Ali Ben Zarrouk Apr 09 '19 at 13:31
  • 1
    In years. I have edited the question. Thanks – Cap Barracudas Apr 09 '19 at 13:33
  • I cant understand how after so many years of development experience you still cannot comprehend the difference between specifications. This is not a duplicate question but for some interesting reason you urged to mark it as such. – Cap Barracudas Apr 09 '19 at 14:55
  • 1
    `ChronoUnit.YEARS.between( LocalDateTime.parse( "1960-01-23T12:34:56" ).toLocalDate() , LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) )` All this has been covered many many times already. Search and study Stack Overflow before posting. – Basil Bourque Apr 09 '19 at 16:15
  • I think this question is really two questions in one: (1) How to parse a datetime string in (ISO 8601) format `yyyy-mm-ddThh:mm:ss` (2) How to calculate the difference between a past datetime and now in years. The former is covered in [How to convert a string date “2019-04-21T12:08:35” to SimpleDateFormat, there by convert to Date?](https://stackoverflow.com/questions/54023877/how-to-convert-a-string-date-2019-04-21t120835-to-simpledateformat-there-by) The latter in [How do I calculate someone's age in Java?](https://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java) – Ole V.V. Apr 09 '19 at 17:34
  • No reason to fight about it. I have reopened. – Ole V.V. Apr 09 '19 at 17:34

1 Answers1

-1

Given with the specific date format, the age can be calculated as following:

public class AgeCalculator {

public static int calculateAge(Date birthdate) {
    Calendar birth = Calendar.getInstance();
    birth.setTime(birthdate);
    Calendar today = Calendar.getInstance();

    int yearDifference = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR);

    if (today.get(Calendar.MONTH) < birth.get(Calendar.MONTH)) {
        yearDifference--;
    } else {
        if (today.get(Calendar.MONTH) == birth.get(Calendar.MONTH)
                && today.get(Calendar.DAY_OF_MONTH) < birth.get(Calendar.DAY_OF_MONTH)) {
            yearDifference--;
        }
    }
    return yearDifference;
}

public static void main(String[] args) throws ParseException {
    // date format yyyy-MM-dd'T'HH:mm:ss
    String birthdateStr = "2013-07-09T12:17:58-04:00";
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date birthdate = df.parse(birthdateStr);
    System.out.println(AgeCalculator.calculateAge(birthdate));
}
}
Vebbie
  • 1,669
  • 2
  • 12
  • 18
  • 2
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class and the equally outdated `Date` and `Calendar`. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Apr 09 '19 at 13:54