Question: I want to convert date of birth into age.
This is my code.

```html
String patientDOB = driver.findElement(id("patient_profile_widget_form_birthday")).getAttribute("value");
```

I'm getting date as: 03/01/1961 

How could I convert this into age? 
I want output like => 57 Years

Any idea? :)

Answer: 
using java 8

```
public int calculateAge(
  LocalDate birthDate) {
    // validate inputs ...
    return Period.between(birthDate, LocalDate.now());
}

```
```
private static final DateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.US);
String patientDOB = driver.findElement(id("patient_profile_widget_form_birthday")).getAttribute("value");

Date dateOfBirth = getDateFromString(patientDOB);
public Date getDateFromString(final String patientDOB) {
    try {
        return FORMATTER.parse(param);
    } catch (ParseException e) {
        throw new Exception("ParseException occurred while parsing date ", e);
    }
}

public static int getAge(Date dateOfBirth) {
Calendar today = Calendar.getInstance();
Calendar birthDate = Calendar.getInstance();
birthDate.setTime(dateOfBirth);
if (birthDate.after(today)) {
    throw new IllegalArgumentException("You don't exist yet");
}
int todayYear = today.get(Calendar.YEAR);
int birthDateYear = birthDate.get(Calendar.YEAR);
int todayDayOfYear = today.get(Calendar.DAY_OF_YEAR);
int birthDateDayOfYear = birthDate.get(Calendar.DAY_OF_YEAR);
int todayMonth = today.get(Calendar.MONTH);
int birthDateMonth = birthDate.get(Calendar.MONTH);
int todayDayOfMonth = today.get(Calendar.DAY_OF_MONTH);
int birthDateDayOfMonth = birthDate.get(Calendar.DAY_OF_MONTH);
int age = todayYear - birthDateYear;

// If birth date is greater than todays date (after 2 days adjustment of leap year) then decrement age one year
if ((birthDateDayOfYear - todayDayOfYear > 3) || (birthDateMonth > todayMonth)){
    age--;

// If birth date and todays date are of same month and birth day of month is greater than todays day of month then decrement age
} else if ((birthDateMonth == todayMonth) && (birthDateDayOfMonth > todayDayOfMonth)){
    age--;
}
return age;

```

}