Calculate working days in java without Saturdays, Sundays and holidays

I have a table in oracle where I saved twelve public days in Mexico and I need to calculate the day from registration

public Date calcularFechaLimite() {
    try {
        DiaFestivoDTO dia = new DiaFestivoDTO();

        // Calendar fechaActual = Calendar.getInstance();
        Calendar fechaL = Calendar.getInstance();
        fechaL.add(Calendar.DATE, 3);

        switch (fechaL.get(Calendar.DAY_OF_WEEK)) {
        case Calendar.SATURDAY:
            fechaL.add(Calendar.DATE, 2);
            break;
        case Calendar.SUNDAY:
            fechaL.add(Calendar.DATE, 2);
            break;
        case Calendar.MONDAY:
            fechaL.add(Calendar.DATE, 2);
            break;
        case Calendar.TUESDAY:
            fechaL.add(Calendar.DATE, 1);
        default:
            break;
        }
        dia.setFechaLimite(fechaL.getTime());
        Integer numeroDiasFest = seleccionPagoBO.obtenerDiasFestivos(dia);
        if (numeroDiasFest != 0) {
            fechaL.add(Calendar.DATE, numeroDiasFest);
            dia.setFechaLimite(fechaL.getTime());
        }

        fechaLimite = fechaL.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return fechaLimite;

}

This is what I have, but March 28 and 29 are public days, and it does not work, any idea?

+5
source share
1 answer

A few questions:

You do not check if the first 3 days on weekends are missed.

You also skip a strange number of days and skip the days of the week (which are working days and therefore should not be skipped).

, DiaFestivoDTO.obtenerDiasFestivos() , ? DiaFestivoDTO?

, , , daterange .

, , "3 " , :

// get all the holidays as java.util.Date
DiaFestivoDTO dia = new DiaFestivoDTO();
List<Date> holidays = dia.getAllNationalHolidays();

// get the current date without the hours, minutes, seconds and millis
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

// iterate over the dates from now and check if each day is a business day
int businessDayCounter = 0
while (businessDayCounter < 3) {
    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY && !holidays.contains(cal.getTime())) {
        businessDayCounter++;
    }
    cal.add(Calendar.DAY_OF_YEAR, 1);
}
Date threeBusinessDaysFromNow = cal.getTime();

'getAllNationalHolidays', , , , .

/ ,

while (businessDayCounter < 3) {

    // determine if this day is a holiday
    DiaFestivoDTO dia = new DiaFestivoDTO();
    dia.setFechaInitial(cal.getTime());
    dia.setFechaLimite(cal.getTime());
    boolean isHoliday = seleccionPagoBO.obtenerDiasFestivos(dia) > 0;

    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY && !isHoliday) {
        businessDayCounter++;
    }
    cal.add(Calendar.DAY_OF_YEAR, 1);
}

, 'from' DiaFestivoDTO, 'setFechaInitial' - . , .

+8

All Articles