gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:31 25 4
gpt4 key购买 nike

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

例如,我可能有两个时段 [Fri 5:31pm - Sat 2:26pm] 和 [Tuesday 3:37am - Thursday 1:14am] 在计算两个日期之间的分钟数时我不想计算并在向前计算时。

我目前的代码只针对一个间隙执行此操作,但它不是非常高效并且正在对我的系统造成压力。我还需要适应我目前没有做的多个定义的差距。

我为一个间隙执行此操作的代码如下所示(hideStarthideEnter 是间隙的开始和结束 DateTime,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);

关于java - 如何有效地计算不包括时间 block 的时间之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49054992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com