如何有效地计算不包括时间块的时间之间的距离?



我正在尝试计算两个日期之间的分钟数,同时排除任意定义且每周发生的时间段。我还需要能够计算反向,在给定时间的情况下,计算 X 分钟数,不包括这些时间段。

例如,我可能有两个周期 [周五下午 5:31 - 周六下午 2:26] 和 [星期二 3:37am - 星期四凌晨 1:14],在计算两个日期之间的分钟数和向前计算时,我不想计算。

我目前有代码只针对一个间隙执行此操作,尽管它不是超级高效并且正在成为我的系统的压力。我还需要适应多个定义的差距,而我目前没有这样做。

我为一个间隙执行此操作的代码如下所示(hideStarthideEnter是间隙的开始和结束日期时间,absoluteLowValue是我计算距离或时间的开始时间):

public int absoluteDistance(DateTime high){
long totalMinutes = new Duration(absoluteLowValue,high).getStandardMinutes();
if (!gapHider.isHidingGaps())
return (int)totalMinutes;
int minutesPerWeek = 10080;
long minutesPerHide = new Duration(hideStart, hideEnd).getStandardMinutes();
long numFullWeeks = totalMinutes/minutesPerWeek;
long remainder = totalMinutes%minutesPerWeek;
totalMinutes -= numFullWeeks*minutesPerHide;
DateTime latestEnd = high;
if (latestEnd.getDayOfWeek() == hideEnd.getDayOfWeek() && latestEnd.getSecondOfDay() < hideEnd.getSecondOfDay()){
latestEnd = latestEnd.minusWeeks(1);
}
while (latestEnd.getDayOfWeek() != hideEnd.getDayOfWeek())
latestEnd = latestEnd.minusDays(1);
latestEnd = latestEnd.withTime(hideEnd.getHourOfDay(),
hideEnd.getMinuteOfHour(),
hideEnd.getSecondOfMinute(),
hideEnd.getMillisOfSecond());
DateTime latestStart = high;
if (latestStart.getDayOfWeek() == hideStart.getDayOfWeek() && latestStart.getSecondOfDay() < hideStart.getSecondOfDay()){
latestStart = latestStart.minusWeeks(1);
}
while (latestStart.getDayOfWeek() != hideStart.getDayOfWeek())
latestStart = latestStart.minusDays(1);
latestStart = latestStart.withTime(hideStart.getHourOfDay(),
hideStart.getMinuteOfHour(),
hideStart.getSecondOfMinute(),
hideStart.getMillisOfSecond());
long timeToNearestEnd = new Duration(latestEnd, high).getStandardMinutes();
long timeToNearestStart = new Duration(latestStart, high).getStandardMinutes();
if (timeToNearestEnd < remainder){
totalMinutes -= minutesPerHide;
}else if (timeToNearestStart < remainder){
totalMinutes -= new Duration(latestStart, high).getStandardMinutes();
}
return (int)totalMinutes;
}
public DateTime timeSinceAbsLow(int index){ 
if (absoluteLowValue != null){
if (!gapHider.isHidingGaps())
return absoluteLowValue.plusMinutes(index);
DateTime date = absoluteLowValue;
long minutesPerWeek = 10080;
long minutesPerHide = new Duration(hideStart, hideEnd).getStandardMinutes();
int difference = (int)(minutesPerWeek - minutesPerHide);
int count = 0;
while (index - count >= difference){
date = date.plusWeeks(1);
count += difference;
}
int remaining = index - count;
DateTime nextStart = date;
while (nextStart.getDayOfWeek() != hideStart.getDayOfWeek())
nextStart = nextStart.plusDays(1);
nextStart = nextStart.withTime(hideStart.getHourOfDay(),
hideStart.getMinuteOfHour(),
hideStart.getSecondOfMinute(),
hideStart.getMillisOfSecond());
long timeDiff = new Duration(date, nextStart).getStandardMinutes();
if (timeDiff < remaining){
date = nextStart.plusMinutes((int)minutesPerHide);
count+= timeDiff;
remaining = index - count;
}
date = date.plusMinutes(remaining);
return date;
}
return new DateTime(); 
}

有没有更好或更简单的方法来执行此过程?我想,如果我添加大量的逻辑来循环浏览"间隙"列表,它只会进一步减慢它的速度。我愿意不使用Jodatime,我只是碰巧正在使用它。任何帮助表示赞赏!

如果我理解正确,您想计算开始日期和结束日期之间的总时间,并减去一个或多个周期。因此,您可以使用Duration对象,然后最终转换为您想要的任何对象(分钟、秒等):

// compute all periods you don't want to count
List<Duration> hideList = new ArrayList<>();
// duration between friday and saturday (assuming they have the values of your example)
hideList.add(new Duration(friday, saturday));
// add as many periods you want
// total duration between start and end dates
Duration totalDuration = new Duration(start, end);
// subtract all periods from total
for (Duration duration : hideList) {
totalDuration = totalDuration.minus(duration);
}
// convert to total number of minutes
long totalMinutes = totalDuration.getStandardMinutes();

我假设hideList中使用的所有日期都在开始日期和结束日期之间(但使用isAfterisBefore方法很容易检查这一点)。


要执行相反的操作,请将totalMinutes添加到开始日期,然后将所有持续时间相加hideList

// sum total minutes to start
DateTime dt = start.plusMinutes((int) totalMinutes);
// sum all the periods
for (Duration duration : hideList) {
dt = dt.plus(duration);
}
// dt is the end date

有一个细节:将Duration转换为分钟数时,您将失去秒和毫秒精度,因此当执行相反的算法时,这些字段将丢失。如果这不是问题,请按上述操作。但是,如果要保留所有精度,只需从添加totalDuration对象开始,而不是添加分钟数:

// sum totalDuratin to start (to preserve seconds and milliseconds precision)
DateTime dt = start.plus(totalDuration);

最新更新