Peter Penzov
Peter Penzov

Reputation: 1588

Calculate date range and return true

I want to compare 2 dates with Java 8+ and return false if the password is old than 45 days. All should be into one line if possible because I want to use it for Spring Security. Is this possible?

Something like this:

boolean isExpired = password_changed_at.minusDays(45).isBefore(LocalDateTime.now());

EDIT:

private boolean compareExpireDates(LocalDateTime password_changed_at) {

        long days_left = Duration.between(password_changed_at, LocalDateTime.now()).toDays();

        if (days_left > 45) {
            return false;
        } else {
            return true;
        }       
    }

Upvotes: 0

Views: 61

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40058

Yes you are in right approach, no need of any if block just in one statement return true if duration days greater than 45 or else false. You can also refactor this method to accept the number of days

private boolean compareExpireDates(LocalDateTime password_changed_at,long noOfDays) {

   return Duration.between(password_changed_at, LocalDateTime.now()).toDays()>noOfDays
}

Upvotes: 1

Related Questions