乔达时间:如何获取某个日期间隔的工作日日期?

乔达时间:如何获取某个日期间隔的工作日日期?,第1张

乔达时间:如何获取某个日期间隔工作日日期?

解决方案:懒惰地走一个星期。

import org.joda.time.LocalDate;import java.util.Iterator;public class DayOfWeekIterator implements Iterator<LocalDate>{    private final LocalDate end;    private LocalDate nextDate;    public DayOfWeekIterator(LocalDate start, LocalDate end, int dayOfWeekToIterate){        this.end = end;        nextDate = start.withDayOfWeek(dayOfWeekToIterate);        if (start.getDayOfWeek() > dayOfWeekToIterate) { nextDate = nextDate.plusWeeks(1);        }    }    public boolean hasNext() {        return !nextDate.isAfter(end);    }    public LocalDate next() {        LocalDate result = nextDate;        nextDate = nextDate.plusWeeks(1);        return result;    }    public void remove() {        throw new UnsupportedOperationException();    } }

测试

import org.joda.time.DateTimeConstants;import org.joda.time.LocalDate;public class DayOfWeekIteratorTest {    public static void main(String[] args) {        LocalDate startDate = new LocalDate(2010, 12, 1);//1st Dec 2010        LocalDate endDate = new LocalDate(2010, 12, 31);//31st Dec 2010        DayOfWeekIterator it = new DayOfWeekIterator(startDate, endDate, DateTimeConstants.FRIDAY);        while (it.hasNext()) { System.out.println(it.next());        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5489375.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存