Java: how do I check if a Date is within a certain range?

后端 未结 12 654
耶瑟儿~
耶瑟儿~ 2020-11-22 16:39

I have a series of ranges with start dates and end dates. I want to check to see if a date is within that range.

Date.before() and Date.after() seem to be a little a

12条回答
  •  爱一瞬间的悲伤
    2020-11-22 17:12

    For covering my case -> I've got a range start & end date, and dates list that can be as partly in provided range, as fully (overlapping).

    Solution covered with tests:

    /**
     * Check has any of quote work days in provided range.
     *
     * @param startDate inclusively
     * @param endDate   inclusively
     *
     * @return true if any in provided range inclusively
     */
    public boolean hasAnyWorkdaysInRange(LocalDate startDate, LocalDate endDate) {
        if (CollectionUtils.isEmpty(workdays)) {
            return false;
        }
    
        LocalDate firstWorkDay = getFirstWorkDay().getDate();
        LocalDate lastWorkDay = getLastWorkDay().getDate();
    
        return (firstWorkDay.isBefore(endDate) || firstWorkDay.equals(endDate))
                && (lastWorkDay.isAfter(startDate) || lastWorkDay.equals(startDate));
    }
    

提交回复
热议问题