我已经实现了一个运行良好的秒表,而没有考虑到银行假期和周末不应计入总持续时间。我一直在寻找一些开源库,我可以在其中获取经过的时间,传递开始瞬间、结束瞬间和一组银行假期(周末不计算在内)。唯一让我更轻松的库是 net.sf.jtemporal,但我仍然需要扩展功能。谁能告诉我是否有一些有用的库来获得想要的功能?
Juan Carlos
问问题
2177 次
5 回答
2
正如我在那里提到的,可能最好和最简单的方法是创建一个包含每天信息的表(从开始/银行假期开始的工作日计数等;每天一行 = 每年 365 行),然后只使用计数功能/ 适当的选择。
于 2008-09-18T13:41:54.833 回答
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;
}
于 2008-09-18T13:54:22.510 回答
0
你也只计算银行营业时间吗?上午 9 点到下午 3 点?还是一天24小时?
于 2008-09-18T13:28:39.937 回答
0
你应该看看Joda Time。它是一种比 Java 中包含的更好的日期/时间 API
于 2008-09-18T13:57:29.070 回答
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;
}
于 2008-09-26T08:54:31.687 回答