Calculating end date while skipping holidays + Joda time

前端 未结 4 1507
暖寄归人
暖寄归人 2020-12-19 09:47

I would like to calculate end date (and time) of an event. I know starting date and duration (in minutes). But:

  1. I have to skip holi
4条回答
  •  清酒与你
    2020-12-19 10:02

    Here's some code I use. dtDateTimes can contain your pre-defined holiday dates (e.g. UK Bank Holidays) and dtConstants can contain recurring things you'd like to match against, like DateTimeConstants.SATURDAY.

    /**
     * Returns a tick for each of
     * the dates as represented by the dtConstants or the list of dtDateTimes
     * occurring in the period as represented by begin -> end.
     * 
     * @param begin
     * @param end
     * @param dtConstants
     * @param dtDateTimes
     * @return
     */
    public int numberOfOccurrencesInPeriod(final DateTime begin, final DateTime end, List dtConstants, List dtDateTimes) {
        int counter = 0;
        for (DateTime current = begin; current.isBefore(end); current = current.plusDays(1)) {
            for (Integer constant : dtConstants) {
                if (current.dayOfWeek().get() == constant.intValue()) {
                    counter++;
                }
            }
            for (DateTime dt : dtDateTimes) {
                if (current.getDayOfWeek() == (dt.getDayOfWeek())) {
                    counter++;
                }
            }
    
        }
        return counter;
    }
    
    /**
     * Returns true if the period as represented by begin -> end contains any one of
     * the dates as represented by the dtConstants or the list of dtDateTimes
     *  
     * @param begin
     * @param end
     * @param dtConstants
     * @param dtDateTimes
     */
    public boolean isInPeriod(final DateTime begin, final DateTime end, List dtConstants, List dtDateTimes) {
        return numberOfOccurrencesInPeriod(begin, end, dtConstants, dtDateTimes) > 0;
    }
    

提交回复
热议问题