如何添加工作日到当前日期的Java? [英] How can I add business days to the current date in Java?

查看:151
本文介绍了如何添加工作日到当前日期的Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加工作日到当前日期的Java?

How can I add business days to the current date in Java?

public Calendar addBusinessDate(Calendar cal, int days) {
//
// code goes over here
//
}

注:

它应该考虑周末了。

Note:

It should consider weekends too.

推荐答案

您可能要考虑使用 ObjectLab套件做繁重给你。

You may want to consider using ObjectLab Kit to do the heavy lifting for you.

假设的要求是简单地回到下一个营业日,当计算日期适逢非营业日:

Assuming the requirement is simply to return the next business day when the computed date falls on a non-business day:

package bizdays.example;

import java.util.HashSet;
import net.objectlab.kit.datecalc.common.DateCalculator;
import net.objectlab.kit.datecalc.common.DefaultHolidayCalendar;
import net.objectlab.kit.datecalc.common.HolidayHandlerType;
import net.objectlab.kit.datecalc.joda.LocalDateKitCalculatorsFactory;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import org.joda.time.LocalDate;

public class BizDayTest {
    private DateCalculator<LocalDate> dateCalculator;
    private final LocalDate startDate = new LocalDate(2009, 12, 23);

    @Before
    public void setUp() {
        HashSet<LocalDate> holidays = new HashSet<LocalDate>();
        holidays.add(new LocalDate(2009, 12, 25));  // Friday

        DefaultHolidayCalendar<LocalDate> holidayCalendar =
            new DefaultHolidayCalendar<LocalDate>(holidays);

        LocalDateKitCalculatorsFactory.getDefaultInstance()
                .registerHolidays("example", holidayCalendar);
        dateCalculator = LocalDateKitCalculatorsFactory.getDefaultInstance()
                .getDateCalculator("example", HolidayHandlerType.FORWARD);
        dateCalculator.setStartDate(startDate);
    }

    @Test
    public void should_not_change_calendar_start_date_even_after_moving() {
        assertThat(
            dateCalculator.moveByBusinessDays(6).getStartDate(),
            equalTo(startDate));
    }

    @Test
    public void moveByBusinessDays_will_return_24_dec_2009_as_next_business_day() {
        assertThat(
            dateCalculator.moveByBusinessDays(1).getCurrentBusinessDate(),
            equalTo(new LocalDate(2009, 12, 24)));
    }

    @Test
    public void moveByBusinessDays_will_return_28_dec_2009_as_two_business_days_later() {
        assertThat(
            dateCalculator.moveByBusinessDays(2).getCurrentBusinessDate(),
            equalTo(new LocalDate(2009, 12, 28)));

    }

    @Test
    public void moveByDays_will_also_return_28_dec_2009_as_two_business_days_later() {
        assertThat(
            dateCalculator.moveByDays(2).getCurrentBusinessDate(),
            equalTo(new LocalDate(2009, 12, 28)));
    }

    @Test
    public void moveByBusinessDays_will_exclude_25_26_and_27_dec_when_computing_business_days() {
        assertThat(
            dateCalculator.moveByBusinessDays(5).getCurrentBusinessDate(),
            equalTo(new LocalDate(2009, 12, 31)));
    }


    @Test
    public void moveByDays_will_include_25_26_and_27_dec_when_computing_business_days() {
        assertThat(
            dateCalculator.moveByDays(5).getCurrentBusinessDate(),
            equalTo(new LocalDate(2009, 12, 28)));
    }
}

该库默认的工作周,从周一到周五,但你可以通过提供一个自定义的<一个更改默认设置href="http://objectlabkit.sourceforge.net/apidocs/net/objectlab/kit/datecalc/common/WorkingWeek.html">WorkingWeek到<一个href="http://objectlabkit.sourceforge.net/apidocs/net/objectlab/kit/datecalc/common/DateCalculator.html">DateCalculator's <一href="http://objectlabkit.sourceforge.net/apidocs/net/objectlab/kit/datecalc/common/DateCalculator.html#setWorkingWeek%28net.objectlab.kit.datecalc.common.WorkingWeek%29">setWorkingWeek().

如图最后两个实施例中,moveByDays()移动时的天,而moveByBusinessDays(包括周末)不包括周末。

As shown in the last two examples, moveByDays() includes the weekends when moving the days, whereas moveByBusinessDays() excludes weekends.

该库还允许您使用乔达时间的LocalDate java.util.Calendar中代替。本例使用乔达时间库,因为它是preferred库在Java中处理日期时使用。

The library also allows you to use java.util.Calendar instead of Joda Time's LocalDate. The example uses Joda Time library because it is the preferred library to use when handling dates in Java.

这篇关于如何添加工作日到当前日期的Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆