在没有星期六,星期日和公众假期的情况下计算java的工作日 [英] Calculate business days in java without saturdays, sunday and public holiday

查看:89
本文介绍了在没有星期六,星期日和公众假期的情况下计算java的工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在oracle有一张桌子,我在墨西哥保存了十二个公共日,我需要计算自你注册后的限制日

i have a table in oracle where I saved the twelve public days in Mexico and I need to calculate a limit day since you registered

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;

}

这是我所拥有的,但3月28日和29日是公开的天,它不工作,任何想法?

This is what i have but March 28 and 29 are public days and it is not working, any idea??

推荐答案

几个问题:

您没有检查您跳过的前3天是否包含周末天。

You are not checking if the first 3 days you skip contain weekend days.

此外,您也正在跳过奇怪的天数并跳过工作日(其中是工作日,因此不应该被跳过)。

Also you are skipping strange amounts of days and skipping for weekdays as well (which are business days and therefore should not be skipped).

我假设方法DiaFestivoDTO.obtenerDiasFestivos()计算某个日期范围内的国定假日数量但是什么是开始日期?是否在创建DiaFestivoDTO时初始化为当前日期?

I assume the method DiaFestivoDTO.obtenerDiasFestivos() calculates the number of national holidays in a certain date range but what is the start date? Is it initialized to the current date when DiaFestivoDTO is created?

当您的日期范围内有国定假日时,您会增加日期范围,但不要检查此新日期范围是否包括新的国家法定假日或周末日。

When there is a national holiday in your date range you increase the date range but never check if this new daterange includes new national holidays or weekend days.

如果您要做的是计算从现在起3个工作日的日期,这里大致是我要做的事情:

If what you are trying to do is calculate a date '3 business days from now' here's roughly what I would do:

// 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'来列出假期因为在内存中存储十二个日期比多次访问数据库以检查它们更有效。

For this to work I would advise you to add a new method 'getAllNationalHolidays' to list al the holidays because it is more efficient to store twelve dates in memory than to access the database multiple times to check for them.

如果你不能更改/添加数据库方法那么你可以做这个

If you cannot change/add database methods then you could do this

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);
}

我假设您可以使用'设置DiaFestivoDTO中的'from'日期' setFechaInitial'或类似的东西。但是通过这种方式,你至少会调用数据库三次,效率很低。

Here I assumed you can set the 'from' date in your DiaFestivoDTO using 'setFechaInitial' or something like that. But in this way you would be calling the database at least three times which is inefficient.

这篇关于在没有星期六,星期日和公众假期的情况下计算java的工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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