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

时间:2008-09-18 13:21:45

标签: java date time stopwatch elapsed

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

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

你应该看看Joda Time。它是比Java

中包含的API更好的日期/时间API

答案 4 :(得分:0)

我认为这将是您正在寻找的有效解决方案。它计算经过的时间(考虑到一个工作日有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;
}