下面的班级将比较固定的日常时间,然后返回最接近的时间。(使用 Joda-Time 库)
public class TimeComparitor {
private List<LocalTime> targetTimes;
public TimeComparitor() {
targetTimes = new ArrayList<LocalTime>();
}
public void addTargetTime(LocalTime target) {
targetTimes.add(target);
}
public LocalTime getClosestFutureTarget(DateTime now) {
LocalTime result = null;
long resultOffset = Long.MAX_VALUE;
ArrayList<String> bigger = new ArrayList<String>();
for (LocalTime target : targetTimes) {
DateTime todayTarget = target.toDateTimeToday();
long differenceMillis = todayTarget.getMillis() - now.getMillis();
if (differenceMillis > 0 && differenceMillis < resultOffset) {
resultOffset = differenceMillis;
result = target;
}
}
return result;
}
1.有resultOffset
返回某个时间和现在的变化,但该方法只返回最接近的时间,如何将resultOffset
'值带出类?我已经阅读了一些从方法中获取两个值的解决方案,但无法使其适应我的代码。
2.还需要有一个从现在起所有以后时间的有序列表,试过这个但没有成功:
public LocalTime getClosestFutureTarget(DateTime now) {
.
.
for (LocalTime target : targetTimes) {
.
.
.
}
//My attempt to Get List of Greater Time spots from now
else if (differenceMillis > 0) {
private ArrayList<String> sortBigger(LocalTime bigTarget){
bigger.add(bigTarget.toString());
Collections.sort(bigger);
}
}
}
return result;
}