Trying to add to fechaProceso(localDate) they days which are not in weekends Java

Viewed 63
LocalDate fechaProceso = r.getFechaProceso();
DayOfWeek dow;
Set<DayOfWeek> weekend = EnumSet.of(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY);
Boolean todayIsWeekend;
LocalDate fechaValor = null;
int i = 1;
while (i <= corr.getDiasValor()) {
    fechaValor = fechaProceso.plusDays(i);  
    dow = fechaValor.getDayOfWeek(); 
    todayIsWeekend = weekend.contains(dow); 
    if (todayIsWeekend) {
        fechaValor.plusDays(2);
    }
    i++;
}

What I am trying to do is add to a date(fechaValor) one day but when it is in order to ignore those days. That is, if I am on Friday 2021-09-10 and I want to add one more day than add two more (that is, skipping Saturday and Sunday) and going to Monday, that is, the next day would be 2021-09-13. What can i do? Actually my code is not working

EDIT2(not working neither):

            switch (fechaProceso.getDayOfWeek()) {
            // when it's a Friday, then add three days to get next Monday
            case FRIDAY:
                fechaValor = fechaProceso.plusDays(corr.getDiasValor() + 3) ;
            // on Saturday, add 2 days
            case SATURDAY:
                fechaValor = fechaProceso.plusDays(corr.getDiasValor() + 2) ;
            // in all other cases add just 1 day
            default:
                fechaValor = fechaProceso.plusDays(corr.getDiasValor() + 1) ;
        }
3 Answers

tl;dr

LocalDate
    .parse( "2021-09-10" )  // Friday
    .with( org.threeten.extra.Temporals.nextWorkingDay() )  // Monday

2021-09-13

TemporalAdjuster

To move to another date, use a TemporalAdjuster. Some implementations are bundled with Java, and you can write your own as well.

Next Monday

In your case, Java provides TemporalAdjusters.next( DayOfWeek ).

Set< LocalDate > weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;
LocalDate x = LocalDate.parse( "2021-09-10" ) ;  // Friday
LocalDate y = x.plusDays( 1 ) ;  // Saturday
if( weekend.contains( y.getDayOfWeek() ) )
{
    y = y.with( TemporalAdjusters.next( DayOfWeek.MONDAY ) ) ;  // Monday
}

As mentioned, you could alternatively implement this logic by writing your own TemporalAdjuster implementation. You would name it something like nextWorkingDay.

Oh, hold on! Somebody already wrote that for you. Add the ThreeTen-Extra library to your project to get org.threeten.extra.Temporals.nextWorkingDay, a TemporalAdjuster.

LocalDate x = LocalDate.parse( "2021-09-10" ) ;  // Friday
LocalDate y = x.with( org.threeten.extra.Temporals.nextWorkingDay() );  // Monday

Note that this org.threeten.extra.Temporals.nextWorkingDay adjuster is hard-coded for Saturday-Sunday. It is not locale-sensitive, for regions using a different definition of weekend. For that you would write your own temporal adjuster.

You could write a method that returns the next working day when you pass it a LocalDate as an argument:

public static LocalDate getNextWorkday(LocalDate localDate, long daysToAdd) {
    long i = 0;
    LocalDate resultingWorkday = localDate;
    while (i < daysToAdd) {
        switch (localDate.getDayOfWeek()) {
        // when it's a Friday, then add three days to get next Monday
        case FRIDAY:
            resultingWorkday.plusDays(3);
        // on Saturday, add 2 days
        case SATURDAY:
            resultingWorkday.plusDays(2);
        // in all other cases add just 1 day
        default:
            resultingWorkday.plusDays(1);
        }
        i++;
    }
    return resultingWorkday;
}

When you try this in a main with your example date, like this

public static void main(String[] args) {
    LocalDate ld = LocalDate.parse("2021-09-10");
    long daysToAdd = 5;
    LocalDate nextWorkday = getNextWorkday(ld, daysToAdd);
    System.out.println("Having added " 
            + daysToAdd 
            + " workdays: " 
            + nextWorkday.format(
                    DateTimeFormatter.ofPattern("EEE, uuuu-MM-dd",
                            Locale.ENGLISH)));
}

you get the output

Having added 5 workdays: Fri, 2021-09-10

You are checking the DayOfWeek of fechaProceso and then adding days for next business day plus corr.getDiasValor(). You should add the days before and then check for weekends.

public LocalDate adjust(LocalDate date, int days) {
    LocalDate dateAdjusted = date.plusDays(days);
    DayOfWeek dayOfWeek = dateAdjusted.getDayOfWeek();
    if (dayOfWeek == DayOfWeek.SATURDAY) {
        return dateAdjusted.plusDays(2);
    } else if (dayOfWeek == DayOfWeek.SUNDAY) {
        return dateAdjusted.plusDays(1);
    } else {
        return dateAdjusted;
    }
}

Then you can do:

LocalDate date = LocalDate.of(2021, 9, 6);
    
IntStream.range(0, 7).forEach(days -> System.out.println(adjust(date, days)));

Output:

2021-09-06
2021-09-07
2021-09-08
2021-09-09
2021-09-10
2021-09-13
2021-09-13
Related