Inclusive Date Range check in Joda Time

后端 未结 2 773
孤街浪徒
孤街浪徒 2021-02-04 00:24

I am using Joda Time 2.1 library.

I have written a method to compare if a given date is between a date range of not. I want it to be inclusive to the start date and end

2条回答
  •  被撕碎了的回忆
    2021-02-04 01:03

    Yes, isAfter is exclusive, otherwise it should probably have been named isEqualOrAfter or something similar.

    Solution: Use "not before" instead of "after", and "not after" instead of "before".

    boolean isBetweenInclusive(LocalDate start, LocalDate end, LocalDate target) {
        return !target.isBefore(start) && !target.isAfter(end);
    }
    

提交回复
热议问题