没有考虑Java中的周末和银行假期的经过时间

时间:2022-11-30 11:51:28

I've implemented a stopwatch that works fine without considering that bank holidays and weekends shouldn't be counted in the total duration. I was looking for some open-source library where I could get the elapsed time, passing a start instant, end instant and a set of bank holidays (weekends aren't counted in). The only library that makes me things easier is net.sf.jtemporal, but I have still to amplify the functionality. Could anyone tell me if there is some useful library to get the wanted functionality?

我已经实施了一个工作正常的秒表,没有考虑到银行假期和周末不应计入总持续时间。我正在寻找一些开源库,在那里我可以获得经过的时间,通过开始时刻,结束时刻和一组银行假期(周末不计入)。唯一能让我更轻松的库是net.sf.jtemporal,但我还是要放大功能。谁能告诉我是否有一些有用的库来获得想要的功能?

Thanks

5 个解决方案

#1


2  

As I have mentioned there, probably the best and easiest approach is to create a table containing information about each day (work day count from beginning / bank holiday, etc; one row per day = 365 rows per year) and then just use count function / with proper selection.

正如我在那里提到的,可能最好和最简单的方法是创建一个包含每天信息的表(从开始/银行假日开始的工作日计数等;每天一行=每年365行)然后只使用计数功能/适当选择。

#2


1  

I doubt you can find something that specific. But it's easy enough to create your own logic. Here's some pseudocode...

我怀疑你能找到具体的东西。但是创建自己的逻辑很容易。这是一些伪代码......

private long CalculateTimeSpan(DateTime BeginDate, DateTime EndDate, ArrayList<DateTime> BankHollidays)
{
    long ticks = 0;
    while (BeginDate <= EndDate) // iterate until reaching end
    {
        if ((BeginDate is holliday?) || (BeginDate is Weekend?))
             skip;
        else
             ticks += (24*60*60*1000);

        BeginDate = BeginDate + 1 day; // add one day and iterate
    }

    return ticks;
}

#3


0  

Do you only count Bank Hours too? 9AM - 3PM? Or is it 24 hours a day?

你也只计算银行营业时间吗?上午9点 - 下午3点?还是一天24小时?

#4


0  

You should take a look at Joda Time. It is a much better date/time API than the one included with Java

你应该看看Joda Time。它是比Java中包含的API更好的日期/时间API

#5


0  

I think this would be a valid solution to what your are looking for. It calculates the elapsed time (considering that one working day has 24 hours) without count the bank holidays and weekends in:

我认为这将是您正在寻找的有效解决方案。它计算经过的时间(考虑到一个工作日有24小时),而不计算银行假日和周末:

/**
 * Calculate elapsed time in milliseconds
 * 
 * @param startTime
 * @param endTime
 * @return elapsed time in milliseconds
 */

protected long calculateElapsedTimeAux(long startTime, long endTime) { 
    CustomizedGregorianCalendar calStartTime = new CustomizedGregorianCalendar(this.getTimeZone());
    CustomizedGregorianCalendar calEndTime = new CustomizedGregorianCalendar(this.getTimeZone());
    calStartTime.setTimeInMillis(startTime);
    calEndTime.setTimeInMillis(endTime);
    long ticks = 0;

    while (calStartTime.before(calEndTime)) { // iterate until reaching end 
        ticks = ticks + increaseElapsedTime(calStartTime, calEndTime);
    }

    return ticks;
}

private long increaseElapsedTime(CustomizedGregorianCalendar calStartTime, CustomizedGregorianCalendar calEndTime) {
    long interval;
    long ticks = 0;

    interval = HOURS_PER_DAY*MINUTES_PER_HOUR*SECONDS_PER_MIN*MILLISECONDS_PER_SEC; // Interval of one day

    if ( calEndTime.getTimeInMillis() - calStartTime.getTimeInMillis() < interval) {
        interval = calEndTime.getTimeInMillis() - calStartTime.getTimeInMillis();
    }

    ticks = increaseElapsedTimeAux(calStartTime, calEndTime, interval);
    calStartTime.setTimeInMillis(calStartTime.getTimeInMillis() + interval);

    return ticks;
}

protected long increaseElapsedTimeAux(CustomizedGregorianCalendar calStartTime, CustomizedGregorianCalendar calEndTime, long interval) {
    long ticks = 0;

    CustomizedGregorianCalendar calNextStartTime = new CustomizedGregorianCalendar(this.getTimeZone());
    calNextStartTime.setTimeInMillis(calStartTime.getTimeInMillis() + interval);

    if ( (calStartTime.isWorkingDay(_nonWorkingDays) && calNextStartTime.isWorkingDay(_nonWorkingDays)) ) { // calStartTime and calNextStartTime are working days
        ticks = interval;

    }
    else {
        if (calStartTime.isWorkingDay(_nonWorkingDays)) { // calStartTime is a working day and calNextStartTime is a non-working day
            ticks = (calStartTime.getNextDay().getTimeInMillis() - calStartTime.getTimeInMillis());
        }
        else {
            if (calNextStartTime.isWorkingDay(_nonWorkingDays)) { // calStartTime is a non-working day and calNextStartTime is a working day
                ticks = (calNextStartTime.getTimeInMillis() - calStartTime.getNextDay().getTimeInMillis());
            }
            else {} // calStartTime and calEndTime are non-working days
        }
    }

    return ticks;
}

#1


2  

As I have mentioned there, probably the best and easiest approach is to create a table containing information about each day (work day count from beginning / bank holiday, etc; one row per day = 365 rows per year) and then just use count function / with proper selection.

正如我在那里提到的,可能最好和最简单的方法是创建一个包含每天信息的表(从开始/银行假日开始的工作日计数等;每天一行=每年365行)然后只使用计数功能/适当选择。

#2


1  

I doubt you can find something that specific. But it's easy enough to create your own logic. Here's some pseudocode...

我怀疑你能找到具体的东西。但是创建自己的逻辑很容易。这是一些伪代码......

private long CalculateTimeSpan(DateTime BeginDate, DateTime EndDate, ArrayList<DateTime> BankHollidays)
{
    long ticks = 0;
    while (BeginDate <= EndDate) // iterate until reaching end
    {
        if ((BeginDate is holliday?) || (BeginDate is Weekend?))
             skip;
        else
             ticks += (24*60*60*1000);

        BeginDate = BeginDate + 1 day; // add one day and iterate
    }

    return ticks;
}

#3


0  

Do you only count Bank Hours too? 9AM - 3PM? Or is it 24 hours a day?

你也只计算银行营业时间吗?上午9点 - 下午3点?还是一天24小时?

#4


0  

You should take a look at Joda Time. It is a much better date/time API than the one included with Java

你应该看看Joda Time。它是比Java中包含的API更好的日期/时间API

#5


0  

I think this would be a valid solution to what your are looking for. It calculates the elapsed time (considering that one working day has 24 hours) without count the bank holidays and weekends in:

我认为这将是您正在寻找的有效解决方案。它计算经过的时间(考虑到一个工作日有24小时),而不计算银行假日和周末:

/**
 * Calculate elapsed time in milliseconds
 * 
 * @param startTime
 * @param endTime
 * @return elapsed time in milliseconds
 */

protected long calculateElapsedTimeAux(long startTime, long endTime) { 
    CustomizedGregorianCalendar calStartTime = new CustomizedGregorianCalendar(this.getTimeZone());
    CustomizedGregorianCalendar calEndTime = new CustomizedGregorianCalendar(this.getTimeZone());
    calStartTime.setTimeInMillis(startTime);
    calEndTime.setTimeInMillis(endTime);
    long ticks = 0;

    while (calStartTime.before(calEndTime)) { // iterate until reaching end 
        ticks = ticks + increaseElapsedTime(calStartTime, calEndTime);
    }

    return ticks;
}

private long increaseElapsedTime(CustomizedGregorianCalendar calStartTime, CustomizedGregorianCalendar calEndTime) {
    long interval;
    long ticks = 0;

    interval = HOURS_PER_DAY*MINUTES_PER_HOUR*SECONDS_PER_MIN*MILLISECONDS_PER_SEC; // Interval of one day

    if ( calEndTime.getTimeInMillis() - calStartTime.getTimeInMillis() < interval) {
        interval = calEndTime.getTimeInMillis() - calStartTime.getTimeInMillis();
    }

    ticks = increaseElapsedTimeAux(calStartTime, calEndTime, interval);
    calStartTime.setTimeInMillis(calStartTime.getTimeInMillis() + interval);

    return ticks;
}

protected long increaseElapsedTimeAux(CustomizedGregorianCalendar calStartTime, CustomizedGregorianCalendar calEndTime, long interval) {
    long ticks = 0;

    CustomizedGregorianCalendar calNextStartTime = new CustomizedGregorianCalendar(this.getTimeZone());
    calNextStartTime.setTimeInMillis(calStartTime.getTimeInMillis() + interval);

    if ( (calStartTime.isWorkingDay(_nonWorkingDays) && calNextStartTime.isWorkingDay(_nonWorkingDays)) ) { // calStartTime and calNextStartTime are working days
        ticks = interval;

    }
    else {
        if (calStartTime.isWorkingDay(_nonWorkingDays)) { // calStartTime is a working day and calNextStartTime is a non-working day
            ticks = (calStartTime.getNextDay().getTimeInMillis() - calStartTime.getTimeInMillis());
        }
        else {
            if (calNextStartTime.isWorkingDay(_nonWorkingDays)) { // calStartTime is a non-working day and calNextStartTime is a working day
                ticks = (calNextStartTime.getTimeInMillis() - calStartTime.getNextDay().getTimeInMillis());
            }
            else {} // calStartTime and calEndTime are non-working days
        }
    }

    return ticks;
}