Find nearest date among several dates to a given date

前端 未结 5 1426
一个人的身影
一个人的身影 2020-12-10 14:58

I have a list of dates and a current date. How can I find the date which is nearest to the current date?

相关标签:
5条回答
  • 2020-12-10 14:59

    If the list is sorted, then you can use Collections.binarySearch() to find the place where the given date would be sorted into the list - the closest one is either right after or right before that index.

    For very large lists, this is much faster than the other solutions, but of course it does require the list to be sorted. If you're going to do such a query multiple times, it would be worth it (performance-wise) to sort the list first.

    0 讨论(0)
  • 2020-12-10 15:04

    If you can use a Set instead of a List, put the dates in a NavigableSet such as TreeSet and use the methods lower and higher.

    NavigableSet<Date> dates = new TreeSet<Date>();
    // add some dates to dates
    Date now = new Date();
    Date highestDateUpUntilNow = dates.lower(now);
    
    0 讨论(0)
  • 2020-12-10 15:13

    You can try this code :

    public static Date closerDate(Date originalDate, Collection<Date> unsortedDates) {
        List<Date> dateList = new LinkedList<Date>(unsortedDates);
        Collections.sort(dateList);
        Iterator<Date> iterator = dateList.iterator();
        Date previousDate = null;
        while (iterator.hasNext()) {
            Date nextDate = iterator.next();
            if (nextDate.before(originalDate)) {
                previousDate = nextDate;
                continue;
            } else if (nextDate.after(originalDate)) {
                if (previousDate == null || isCloserToNextDate(originalDate, previousDate, nextDate)) {
                    return nextDate;
                }
            } else {
                return nextDate;
            }
        }
        return previousDate;
    }
    
    private static boolean isCloserToNextDate(Date originalDate, Date previousDate, Date nextDate) {
        if(previousDate.after(nextDate))
            throw new IllegalArgumentException("previousDate > nextDate");
        return ((nextDate.getTime() - previousDate.getTime()) / 2 + previousDate.getTime() <= originalDate.getTime());
    }
    
    0 讨论(0)
  • 2020-12-10 15:15

    Loop through all dates with the following:
    1. Have a variable that keeps track of the current closest date
    2. Have a variable that is the difference between the current closest date and the current date

    When you find a date with a difference less than that of the what you're keeping track of in (2), update the difference and the current closest date

    At the end, the current closest date is the closest date in the collection

    here's code in python:

    dates = [date(2010,1,2), date(2010,5,6), date(2010,3,4), date(2011, 1, 2), date(2010,10,20), date(2009,2,3)]
    current_date = dates[0]
    current_min = abs(current_date - date.today())
    for d in dates:
        if abs(d - date.today()) < current_min:
            current_min = abs(d - date.today())
            current_date = d
    
    0 讨论(0)
  • 2020-12-10 15:18

    I'd use Collection.min with a custom comparator that "orders" the dates according to distance from current time.

    final long now = System.currentTimeMillis();
    
    // Create a sample list of dates
    List<Date> dates = new ArrayList<Date>();
    Random r = new Random();
    for (int i = 0; i < 10; i++)
        dates.add(new Date(now + r.nextInt(10000)-5000));
    
    // Get date closest to "now"
    Date closest = Collections.min(dates, 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 Long.compare(diff1, diff2);
        }
    });
    
    0 讨论(0)
提交回复
热议问题