在Java中,不考虑周末和银行假期的运行时间
我已经实现了一个秒表,它可以正常工作,而不考虑银行假期和周末不应计入总持续时间。我在寻找一些开源库,在那里我可以获得经过的时间,通过开始时刻、结束时刻和一组银行假期(周末不算在内)。唯一一个让我的事情变得简单的库是net.sf.jtemporal,但我仍然需要扩展它的功能。在Java中,不考虑周末和银行假期的运行时间,java,date,time,stopwatch,elapsed,Java,Date,Time,Stopwatch,Elapsed,我已经实现了一个秒表,它可以正常工作,而不考虑银行假期和周末不应计入总持续时间。我在寻找一些开源库,在那里我可以获得经过的时间,通过开始时刻、结束时刻和一组银行假期(周末不算在内)。唯一一个让我的事情变得简单的库是net.sf.jtemporal,但我仍然需要扩展它的功能。 有人能告诉我是否有一些有用的库来获得想要的功能吗?你也只计算银行营业时间吗?上午9点到下午3点?还是一天24小时?正如我所提到的,最好、最简单的方法可能是创建一个包含每天信息的表(从开始算起的工作日数/银行假期等;每天一行=
有人能告诉我是否有一些有用的库来获得想要的功能吗?你也只计算银行营业时间吗?上午9点到下午3点?还是一天24小时?正如我所提到的,最好、最简单的方法可能是创建一个包含每天信息的表(从开始算起的工作日数/银行假期等;每天一行=每年365行),然后使用计数功能/并进行适当选择 我怀疑你能找到那么具体的东西。但创建自己的逻辑很容易。这里有一些伪代码
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;
}
private long CalculateTimeSpan(日期时间开始日期、日期时间结束日期、数组列表BankHollidays)
{
长刻度=0;
while(BeginDate您应该看看。它是一个比Java附带的日期/时间API好得多的API我认为这是一个有效的解决方案,可以满足您的需求。它计算经过的时间(考虑到一个工作日有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;
}
/**
*以毫秒为单位计算运行时间
*
*@param startTime
*@param endTime
*@返回已用时间(毫秒)
*/
受保护的长计算失效时间(长开始时间,长结束时间){
CustomizedGregorianCalendar calStartTime=新的CustomizedGregorianCalendar(this.getTimeZone());
CustomizedGregorianCalendar calEndTime=新的CustomizedGregorianCalendar(this.getTimeZone());
calStartTime.setTimeInMillis(startTime);
calEndTime.setTimeInMillis(endTime);
长刻度=0;
while(calStartTime.before(calEndTime)){//迭代到结束
滴答声=滴答声+递增的延时(calStartTime、calEndTime);
}
返回滴答声;
}
专用长增量ElapsedTime(自定义EdgRegorianCalendar calStartTime、自定义EdgRegorianCalendar calEndTime){
间隔时间长;
长刻度=0;
间隔=小时/天*分钟/小时*秒/分钟*毫秒/秒;//一天的间隔
if(calEndTime.getTimeInMillis()-calStartTime.getTimeInMillis()
这是一天24小时。我得到了已用计时器,但我想返回该时间段减去该时间段内周末和银行假期的时间。很抱歉,我编辑了我的问题,指定我使用的语言是Java。非常感谢。谢谢,但我已经这样做了,它比Java的时间段好,但无论如何我找到了net。使用CompactPeriodStorage.periodIterator(Period-Period)实现我的目标,您可以获得CloseableIterator,该迭代器在与给定时间段相交的存储的所有时间段上进行迭代。我会使用哈希集来避免重复放置值,但在我看来这有点低效,特别是因为如果粒度为秒,则每天必须迭代60x60x24次。还有其他更有效的方法吗谢谢