-1

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

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? :)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
SanjX
  • 957
  • 1
  • 8
  • 12

3 Answers3

1

using java 8

public int calculateAge(
  LocalDate birthDate) {
    // validate inputs ...
    return Period.between(birthDate, LocalDate.now());
}
Daan
  • 373
  • 2
  • 9
0
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;

}

Vebbie
  • 1,669
  • 2
  • 12
  • 18
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. 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. Dec 05 '18 at 12:09
  • Not the exact answer which I want :/ – SanjX Dec 05 '18 at 12:12
-2

Create a wrapper method that get you string as argument,

use:

 String[] parts = string.split("/"); 

and select the 3rd item of the list.

then you just have to use:

int year = Calendar.getInstance().get(Calendar.YEAR);

to get your year. Then you just convert your year string into an int and substract them

Aurele Collinet
  • 138
  • 1
  • 16