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

java 8 calculate business days
how to check if a date falls on weekend or holiday with java
java add business days
sql add days to date excluding weekends and holidays
java sql date add days
oracle add business days to date
java date add days
how to add 7 days to current date in android

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.


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.time.LocalDate;
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.jdk8.LocalDateKitCalculatorsFactory;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;

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

    @Before
    public void setUp() {
        HashSet<LocalDate> holidays = new HashSet<LocalDate>();
        holidays.add(LocalDate.of(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(LocalDate.of(2009, 12, 24)));
    }

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

    }

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

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


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

The library defaults the working week to be from Monday to Friday, but you can change the defaults by supplying a custom WorkingWeek to DateCalculator's setWorkingWeek().

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

The library also allows you to use java.util.Calendar or Joda Time's LocalDate. The examples use JDK8's java.time.LocalDate because it is the preferred way since JDK8.

Edit: Updated examples to use java.time.LocalDate

How can I add business days to the current date in Java?, 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  To simply add 2 business weeks to a start date, you call the function CalculateTenBusinessDaysFromInputDate and pass it the StartDate. This function will return you a date that has 10 business days from the StartDate. If the Startdate is a Saturday, we simply add 16 days to the StartDate passed in.


How to skip weekends while adding days to LocalDate in Java 8 , Suppose today's date is 26th Nov 2015-Thursday, when I add 2 business days to it, I want the result as Monday 30th Nov 2015. I am working on my own  Adding Days to the given Date using Calendar class. 2. Adding Days to the current date using Calendar class. 3. Add One day to a Date in Java. 4. Add One day to the current date in Java. 5. Java 8 – Adding 1 day or number of days to the current or given date.


Here is the modified version to find date calculation.

public  Calendar algorithm2(int businessDays){
    Calendar cal2 = Calendar.getInstance();
    Calendar cal = Calendar.getInstance(); 
    int totalDays= businessDays/5*7;
    int remainder = businessDays % 5;
    cal2.add(cal2.DATE, totalDays); 

    switch(cal.get(Calendar.DAY_OF_WEEK)){
        case 1:
                break;
        case 2: 
                break;
        case 3: 
                if(remainder >3)
                cal2.add(cal2.DATE,2);
                break;
        case 4: 
                if(remainder >2)
                cal2.add(cal2.DATE,2);
                break;
        case 5: 
                if(remainder >1)
                cal2.add(cal2.DATE,2);
                break;
        case 6: 
                if(remainder >1)
                cal2.add(cal2.DATE,2);
                break;
        case 7: 
                if(remainder >1)
                cal2.add(cal2.DATE,1);
                break;
    }

    cal2.add(cal2.DATE, remainder); 
    return cal2;

}

Java 8 - Add or subtract N business days to date, Learn to add or subtract N business days to LocalDate instance in Java 8. weekend or holiday, we add again 1 day to date and decrease the days by 1. System.out.println(addBusinessDays(today, 8 , Optional.of(holidays))); // 2020-05​-18. Add or substract days to current date using Java Calendar This example shows how to add or substract days in current date and time values using Java Calendar class.


public static Date addBusinessDays(Date date, int days) {

    DateTime result = new DateTime(date);
    result = isWeekEnd(result)
        ? getPreviousBusinessDate(result)
        : result;

    for (int i = 0; i < days; i++) {
        if (isWeekEnd(result)) {
            i--;
        }
        result = result.plusDays(1);
    }
    return result.toDate();
}

private static boolean isWeekEnd(DateTime dateTime) {
    int dayOfWeek = dateTime.getDayOfWeek();
    return dayOfWeek == DateTimeConstants.SATURDAY || dayOfWeek == DateTimeConstants.SUNDAY;
}

private static DateTime getPreviousBusinessDate(DateTime result) {
    while (isWeekEnd(result)) {
        result = result.minusDays(1);
    }

    return result;
}

Skipping Weekends While Adding Days to LocalDate in Java 8 , However, if the current day is a weekday, then we increment the counter. In this way, we keep on adding days until the counter is equal to the  Now, we displayed the current date in a formatted manner. Our objective here is to add days to a date, for which we are provided with a method to add a number of days to a constraint of date. We passed the no. of the day of the month and along with we passed no. of days we want to add to it. Now simply formatting the updated result of date, we


//supports negative numbers too.

private Calendar addBusinessDay(final Calendar cal, final Integer numBusinessDays)
{
    if (cal == null || numBusinessDays == null || numBusinessDays.intValue() == 0)
    {
        return cal;
    }
    final int numDays = Math.abs(numBusinessDays.intValue());
    final int dateAddition = numBusinessDays.intValue() < 0 ? -1 : 1;//if numBusinessDays is negative
    int businessDayCount = 0;
    while (businessDayCount < numDays)
    {
        cal.add(Calendar.DATE, dateAddition);

        //check weekend
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        {
            continue;//adds another day
        }

        //check holiday
        if (isHoliday(cal))//implement isHoliday yourself
        {
            continue;//adds another day
        }

        businessDayCount++;
    }
    return cal;
}

How to add N working days to the current date in Java?, We need to do addition to current date using add() method of Calendar class that accepts DAY_OF_MONTH constant and number of days that  This article shows you how to add days to the current date, using the classic java.util.Calendar and the new Java 8 date and time APIs.. 1. Calendar.add. Example to add 1 year, 1 month, 1 day, 1 hour, 1 minute and 1 second to the current date.


how to add 10 working days to date object (Java in General forum at , i need help from you, i want to add 10 working days to current date means don't consider Saturday and Sunday. For example today is Aug-14 th  Add days to Date [Java 7] Till Java 7, only good way to add days to Date is using Calendar class. It’s calendar.add () method takes argument of various time units to manipulate the date. It can be used to add days, months or any time unit in underlying Date class.


BusinessDays.java · GitHub, public BusinessDays(String businessHoursName){ Add number of working days in a date public Datetime addDays(Datetime startDate, Integer days). Java LocalDate Example 1: Adding Days to the current Date In this example, we are adding one day to the current date. We are using now() method of LocalDate class to get the current date and then using the plusDays() method to add the specified number of days to the LocalDate.


Adding working days to current date!, Hi All I need to add lead time which is in days i:e working days to the current date. example current date : 6/29/2004 Lead time : 15 working days(excluding sat  Amongst Java's myriad of classes is the Calendar class, which is used to convert dates and time between specific instants and the calendar fields. Getting the current date and time is really easy using a calendar: Calendar calendar = Calendar.getInstance (); // Returns instance with current date and time set.