Q:

Nearest time-of-day value in List

Is there any way to display element of a list (or any other that might do similar function) comparing to current time.
So, I made an arraylist that contains time, which doesn't have any pattern of repetition, using method to increase it for 10 min (for instance) won't to.
I have following:
Date function that has current time:
Date date = new Date(); 
DateFormat dateformat = new SimpleDateFormat("HH:mm:ss");

ArrayList that has time that I need:
ArrayList<String> time = new ArrayList<String>();

        time.add("5:40");
        time.add("6:40");
        time.add("8:30");
        time.add("9:45");
        time.add("10:35");
        time.add("11:10");
        time.add("11:55");
        time.add("12:20");
        time.add("13:30");
        time.add("14:55");
        time.add("16:00");
        time.add("16:30");
        time.add("17:30");
        time.add("19:00");
        time.add("20:10");
        time.add("21:10");

Now I'm curious, and also because I'm new to this, is there a way to do next:
1) Considering there will be a lot of arrays that will have time, is there a way (perhaps with some code examples) to write a method that will have an argument so user can choose which 'time' they want it to be displayed?
2) Will there be need to use loop once (in this case 'time' array) it reaches the end, or can it be written so it automatically goes to beginning based on current time.
3) Code optimisation: can this be written in more practical way?
I am fairly new to this, so any help (with guidance and code examples) is much appreciated so I can use it in this project and as learning material for future ones.

A:

java.time
Use the LocalTime class rather than String to represent your values. A LocalTime object is for time-of-day without a date and without a time zone.
Create your List.
List<LocalTime> list = new ArrayList<>();

Parsing
Populate the List by parsing the input strings. If your input strings comply with the ISO 8601 standard including a padded zero on single-digit hour, then you need not define any formatting pattern as LocalTime can directly parse such values.
list.add( LocalTime.parse( "09:45" ) );
list.add( LocalTime.parse( "10:35" ) );
list.add( LocalTime.parse( "11:10" ) );

If your input strings lack the padding zero, then define and use a DateTimeFormatter.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "h:m" );
…
LocalTime localTime = LocalTime.parse( input , formatter );

Sorting
Sort the List. The Collections.sort static method is one way.
Collections.sort( list );

Current time
Get the current time. For this a time zone is required (ZoneId), as the wall-clock time for any moment various around the world by time zone. Use a proper time zone name, never the 3-4 letter abbreviations commonly seen such as EST or IST.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalTime now = LocalTime.now( zoneId );

Loop the list
Loop the List comparing each element in the list to now using whatever rules you have in mind for defining “nearest”. The Duration class tracks a span of time as a total number of seconds plus a fractional second in nanoseconds.
Note that Duration class has various methods to assist with comparing. The abs method gives an absolute value (converting negatives to positives). The compareTo method tells you if one duration is larger, smaller, or equal to another.
…
for ( LocalTime localTime : list ) {
    Duration duration = Duration.between( now , localTime );
    …
}

NavigableSet
If you don't mind losing any possible duplicate time values, use a NavigableSet and its ceiling method as shown in the correct answer by Assyrians.

A:

Like @BasilBourque, I suggest storing the times as LocalTimes - but I would also suggest using a NavigableSet such as TreeSet instead of a List (I'm assuming that the times in your list are all unique).
The idea is that TreeSet has a ceiling method that does exactly what you need:

Returns the least element in this set greater than or equal to the given element, or null if there is no such element.

You can then write:
TreeSet<LocalTime> times = new TreeSet<> ();
times.add(LocalTime.parse("05:40"));
times.add(LocalTime.parse("06:40"));
times.add(LocalTime.parse("08:30"));
//...

LocalTime ceiling = times.ceiling(LocalTime.now());
if (ceiling != null) //do something with it

