I have a method which returns Optional. Basically, it gets value from DB and if the value from DB is present and if the value is before 20 years, it returns currentDate-20 years or else return as it is.
Optional<Instant> getJoinDate(final Instant instant) {
final Optional<Employee> joinTime = empService.retrieveById(1);
if (joinTime.isPresent()) {
final Instant joinDate = joinTime.get().getJoinTime().toInstant();
if (joinDate.isBefore(instant.minus(20,ChronoUnit.YEARS))) {
return Optional.of(instant.minus(20, ChronoUnit.YEARS));
}
else {
return Optional.of(joinDate);
}
}
return Optional.empty();
}
Is there any simple way to do this with Java 8 without if else?
Thanks