Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 56195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:29:47+00:00 2026-05-10T17:29:47+00:00

I have a list of Date objects, and a target Date. I want to

  • 0

I have a list of Date objects, and a target Date. I want to find the date in the list that’s nearest to the target date, but only dates that are before the target date.

Example: 2008-10-1 2008-10-2 2008-10-4

With a target date of 2008-10-3, I want to get 2008-10-2

What is the best way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:29:48+00:00Added an answer on May 10, 2026 at 5:29 pm

    Sietse de Kaper solution assumes a reverse sorted list, definitely not the most natural thing to have around

    The natural sort order in java is following the ascending natural ordering. (see Collection.sort http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List) documentation)

    From your example,

     target date = 2008-10-03  list = 2008-10-01 2008-10-02 2008-10-04  

    If another developper uses your method with a naive approach he would get 2008-10-01 which is not what was expected

  2. Don’t make assumptions as to the ordering of the list.
  3. If you have to for performance reasons try to follow the most natural convention (sorted ascending)
  4. If you really have to follow another convention you really should document the hell out of it.
  5. private Date getDateNearest(List<Date> dates, Date targetDate){   Date returnDate = targetDate   for (Date date : dates) {     // if the current iteration'sdate is 'before' the target date     if (date.compareTo(targetDate) <= 0) {       // if the current iteration's date is 'after' the current return date       if (date.compareTo(returnDate) > 0){         returnDate=date;       }     }   }     return returnDate; } 

    edit – I also like the Treeset answer but I think it might be slightly slower as it is equivalent to sorting the data then looking it up => nlog(n) for sorting and then the documentation implies it is log(n) for access so that would be nlog(n)+log(n) vs n

  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) { if (this.is(':hidden')) { return… May 11, 2026 at 3:28 pm
  • added an answer In general, the CGI environment is different than your interactive… May 11, 2026 at 3:28 pm
  • added an answer I suggest you use a generic delegate such as Func<T,… May 11, 2026 at 3:28 pm

Related Questions

Stackoverflow is built on MVC and does a bunch of simple but nice things
Was looking to get peoples thoughts on keeping a Lucene index up to date
I am trying to make a search view in Django. It is a search
DataSets were one of the big things in .NET 1.0 and even now when

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.