joda时间 - 添加工作日到目前为止

Bob*_*r02 9 java jodatime

是否可以将工作日添加到joda时间?

例如,如果当前日期是01/03周五,则日期+ 1应该返回04/03周一,而不是02/03.

Jes*_*per 8

据我所知,在Joda Time中没有为你自动执行此操作的内置方法.但是,您可以编写自己的方法,在循环中递增日期,直到达到工作日.

请注意,根据您的需要,这可能会比您想象的要复杂得多.例如,它是否应该跳过假期?哪些日子是假期取决于你所在的国家.此外,在一些国家(例如,阿拉伯国家),周末是周四和周五,而不是周六和周日.

  • 根据JodaTime规范,1是星期一,7是星期日.你确定0也适用于星期天吗?来源:http://joda-time.sourceforge.net/apidocs/org/joda/time/Chronology.html#dayOfWeek() (2认同)

vij*_*jay 5

LocalDate newDate = new LocalDate();
int i=0;
while(i<days)//days == as many days as u want too
{
    newDate = newDate.plusDays(1);//here even sat and sun are added
    //but at the end it goes to the correct week day.
    //because i is only increased if it is week day
    if(newDate.getDayOfWeek()<=5)
    {
        i++;
    }

}
System.out.println("new date"+newDate);
Run Code Online (Sandbox Code Playgroud)