我正在尝试计算两个日期之间的分钟数,同时排除任意定义并每周发生的时间段。我还需要能够计算反向,在给定时间的情况下,计算除了那些时间段之外的X分钟数。
例如,我可能有两个时期[星期五下午5点31分 - 星期六下午2点26分]和[星期二凌晨3点37分 - 星期四凌晨1点14分],在计算两个日期之间的分钟时我不想算数并在计算前进时。
我目前只有一个代码可以解决这个问题,虽然它不是超级高效并且正在成为我系统的压力。我还需要适应我目前不做的多个定义的差距。
我在一个间隙中执行此操作的代码如下所示(hideStart
和hideEnter
是间隙的开始和结束日期时间,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,我恰好正在使用它。任何帮助表示赞赏!
答案 0 :(得分:0)
如果我理解正确,您需要计算开始日期和结束日期之间的总时间,并减去一个或多个周期。因此,您可以使用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
中使用的所有日期都在开始日期和结束日期之间(但使用isAfter
和isBefore
方法很容易检查)
相反,您将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);