AgeCalculator changes

This commit is contained in:
Marcos Lopez Gonzalez
2019-05-01 13:10:42 +02:00
committed by mikr
parent 89dddaebe8
commit e257fc3a63
2 changed files with 23 additions and 7 deletions

View File

@@ -33,28 +33,28 @@ public class RootCauseFinder {
}
try {
return calculateDifference(birthDate).getYears();
return Period
.between(parseDate(birthDate), LocalDate.now())
.getYears();
} catch (DateParseException ex) {
throw new CalculationException(ex);
}
}
private static Period calculateDifference(String birthDateAsString) throws DateParseException {
private static LocalDate parseDate(String birthDateAsString) throws DateParseException {
LocalDate birthDate = null;
LocalDate birthDate;
try {
birthDate = LocalDate.parse(birthDateAsString);
} catch (DateTimeParseException ex) {
throw new InvalidFormatException(birthDateAsString, ex);
}
LocalDate today = LocalDate.now();
if (birthDate.isAfter(today)) {
if (birthDate.isAfter(LocalDate.now())) {
throw new DateOutOfRangeException(birthDateAsString);
}
return Period.between(birthDate, today);
return birthDate;
}
}