private void getLicenseWithNewState(License license) throws IOException, MessagingException { Date validFrom = license.getValidFrom(); Date validTill = license.getValidTill(); if (validFrom != null && validTill != null) { Instant now = Instant.now(); Instant lastValid = validTill.toInstant(); Instant lastActive = lastValid.minus(2, ChronoUnit.DAYS); if (now.isBefore(lastActive)) { license.setState(State.ACTIVE); } if (now.isAfter(lastActive) && now.isBefore(lastValid)) { license.setState(State.EXPIRATION_NEARING); // send mail to Admin notifying about soon expiring license mailService.sendExpirationNearingMail(license); } if (now.isAfter(lastValid)) { license.setState(State.TERMINATED); } } }