test if duration is between a range

nimo23 :

I have 3 durations

// java.time.Duration
var sec1 = Duration.ofSeconds(1);
var sec2 = Duration.ofSeconds(2);
var min1 = Duration.ofMinutes(5);

and I want to test if sec2 is between sec1 and min1. Something like this

    if(sec2.between(sec1, min1))

does not work.

How can I do that?

Update - Solution:

// tests if x is within start (inclusive) and end (inclusive)
boolean isBetween(Duration x, Duration start, Duration end) {
    return start.compareTo(x) <= 0 && x.compareTo(end) <= 0;
}
vbezhenar :
if (sec1.compareTo(sec2) <= 0 && sec2.compareTo(min1) <= 0)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=295463&siteId=1