java - Find nearest date from a list -


i have list of dates , current date.

how can find date nearest current date?

i'd use collection.min custom comparator "orders" dates according distance current time.

final long = system.currenttimemillis();  // create sample list of dates list<date> dates = new arraylist<date>(); random r = new random(); (int = 0; < 10; i++)     dates.add(new date(now + r.nextint(10000)-5000));  // date closest "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);     } }); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -