This question is continuation of my previous post
I have two date fields:

start date (2021-03-07T07:37:15) and end date (2021-03-07T07:37:25) and temp date (2021-03-07T07:37:20) .

I have a list where I have returned 6 records between the start date and end date. How can I filter the records and return the datetime which is closest to temp date?

here in the example records I need to get the 3rd record because the 2021-03-07T07:37:20.004 is closet to the temp date. Example

```
2021-03-07T07:37:15.000
2021-03-07T07:37:19.999
2021-03-07T07:37:20.004
2021-03-07T07:37:20.809
2021-03-07T07:37:22.100
2021-03-07T07:37:22.814

```

In the Example 2 i need to get the 2nd recod , since there are no records found in the same second and 2021-03-07T07:37:19.999 is closest to the target date

```
2021-03-07T07:37:15.000
2021-03-07T07:37:19.999 
2021-03-07T07:37:22.100
2021-03-07T07:37:22.814
2021-03-07T07:37:22.815
2021-03-07T07:37:22.816

```

can i acheive this using two filters ? when i tried with the below code it is returning me null for the first scenario. Please suggest

```
    public RequiredRecord findCloseRecord(List<RequiredRecord > list,  Date tempDate) {
    Date startTime  = new Date(tempDate.getTime() - 5000);  
    Date endTime = new Date(tempDate.getTime() + 5000);
          Log.logInfo(this, "Find close record");
          if (list != null && !list.isEmpty()) {
               List<RequiredRecord > filteredRec = list.stream()
                         .filter(rec -> (rec.getLogRecDateTime() != null ))
                         .sorted(Comparator.comparing(RequiredRecord ::getLogRecDateTime))
                         .collect(Collectors.toList());    

                if (!ClrUtils.isCollectionEmpty(filteredRec)) {   

    List<RequiredRecord > filteredRecEqual = list.stream()
        .filter(rec1 -> (rec1.getLogRecordDateTime() != null && tempDate.equals(rec1.getLogRecordDateTime()))).collect(Collectors.toList());
    if (!ClrUtils.isCollectionEmpty(filteredRecEqual)) {
      filteredRecEqual.get(0);
      return (filteredRecEqual.get(0));
    } else {
      List<RequiredRecord > filteredRec2 =
          list.stream().sorted(Comparator.comparingLong(d -> Math.abs(tempDate.getTime() - d.getLogRecordDateTime().getTime())))
              .collect(Collectors.toList());

      return (filteredRec2.get(0));
    }
          }
          return null;
     }

```