Reputation: 1067
What is an efficient and easy-to-read expression of testing if LocalDate dayX
falls in a duration that is described with a starting day LocalDate day0
and a length Period length
?
Currently I am doing like this:
boolean match = !day0.isAfter(dayX) && day0.plus(length).isAfter(dayX);
I just feel this looks a bit dumb and every time I read this it takes several seconds for my brain to tell if the boundaries are correct. So I am looking for a smarter way that maybe involves the Duration
or Interval
classes.
Upvotes: 0
Views: 69
Reputation: 3891
Somewhat similar approach: day0 < dayx < (day0 + period)
boolean match = (dayx.isAfter(day0) && dayx.isBefore(day0.plus(length));
Upvotes: 1
Reputation: 30809
What you are doing is correct (except that you need to use before
method for the first condition). Maybe you can wrap it into a method and call it to make it reusable and look more elegant, e.g.:
public boolean isInRange(Localdate start, Period period, Localdate target){
return !target.before(start) && !target.after(start.plus(period));
}
Upvotes: 2