Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating dates given two dates excluding weekend

I am using Joda time api in a Spring 3.0 project for to calculate dates. Now I have a start and end date and I want to get everyday exept weekend or Saturday or Sunday between these two dates. How can I achieve this ?

I looked at this post Joda time - all mondays between two dates. It offered some guidance but still vague as how to exclude two dates.

like image 539
pundit Avatar asked Nov 17 '11 17:11

pundit


People also ask

How do you calculate dates between two dates excluding weekends?

If you'd like to calculate the difference between two dates while excluding weekends and holidays, use the NETWORKDAYS function instead. This also looks for 3 arguments: the start date, the end date, and optional holidays. Unlike the WORKDAY function, the NETWORKDAYS function does include or count the start day.

How do I exclude weekends in Excel between two dates?

The NETWORKDAYS Function[1] calculates the number of workdays between two dates in Excel. When using the function, the number of weekends are automatically excluded. It also allows you to skip specified holidays and only count business days. It is categorized in Excel as a Date/Time Function.

How do I calculate the difference between two dates excluding weekends in SQL?

date difference -- All dates between 01/07/2021 and 15/07/2021 excluding weekends SELECT CAL_DATE FROM ( SELECT to_date('01/07/2021', 'DD/MM/YYYY') + ROWNUM - 1 AS CAL_DATE FROM ALL_OBJECTS WHERE ROWNUM <= to_date('15/07/2021', 'DD/MM/YYYY') - to_date('01/07/2021', 'DD/MM/YYYY') + 1) WHERE to_char(CAL_DATE, 'DY', ' ...


1 Answers

I assume your question is how to

get every day except weekend or Saturday or Sunday between two dates.

Solution :

public static void main(String[] args) {
    final LocalDate start = LocalDate.now();
    final LocalDate end = new LocalDate(2012, 1, 14);

    LocalDate weekday = start;

    if (start.getDayOfWeek() == DateTimeConstants.SATURDAY ||
            start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
        weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
    }

    while (weekday.isBefore(end)) {
        System.out.println(weekday);

        if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
            weekday = weekday.plusDays(3);
        else
            weekday = weekday.plusDays(1);
    }
}
like image 150
lschin Avatar answered Oct 26 '22 18:10

lschin