我有Date对象数组:
Date[] vakitlarDate;
我希望从该区域获得当前时间的结束时间。
我可以从上面的数组vakitlerDate
我使用了这个功能:
@Override
public void run() {
// nextTime will be calculated here, which is the next time
nextTime = new Date();
Date now = new Date();
try {
Date[] vakitlarDate = vakitler.getVakitlarAsDateArray();
boolean morningIsNextTime = true;
// For loop to get next time of now
for (int i = 0; i < Vakitler.NUM_OF_VAKITLER; i++) {
if (now.after(vakitlarDate[i])) {
continue;
} else {
nextTime = vakitlarDate[i];
currentTimeIndex = i;
morningIsNextTime = false;
break;
}
}
if (morningIsNextTime) {
// Next time is morning of next day
// TODO: add one day here to get next day morning
nextTime = vakitlarDate[0];
}
} catch (ParseException e) {
e.printStackTrace();
}
我无法从该阵列获得最接近的时间。
该区域从早到晚订购。这是同一天的时间。
有什么想法吗?
答案 0 :(得分:1)
我解决了它:
List<Date> vakitlerDateList = getVakitlerAsListOfDate();
Date closest = Collections.min(vakitlerDateList, new Comparator<Date>() {
public int compare(Date d1, Date d2) {
long diff1 = Math.abs(d1.getTime() - now);
long diff2 = Math.abs(d2.getTime() - now);
return diff1 < diff2 ? -1 : 1;
}
});
closest
将是最接近现在的
答案 1 :(得分:0)
我认为你应该使用before()而不是after()