how to compare two dates in stream filter in java 8?

15,905

You dont declare what your filter condition actually is:

List<PlaceBook> filtered = checkInVal.stream().filter(string -> 
        string.getCheckInDt().compareTo(checkIn) == 0 /* == 0, for example is missing*/).collect(Collectors.toList());

compareTo by itself returns an int value, that cannot be cast to boolean, which is required by filter.

BTW 'string' is not a good name in the filter.

Share:
15,905
Navaneethan Arun
Author by

Navaneethan Arun

Updated on December 02, 2022

Comments

  • Navaneethan Arun
    Navaneethan Arun over 1 year

    I get two dates from a request object using stream filter. There I have to compare those objects then collect them store in list. But now i get this error. Please help me with it.

    Error:

    Type mismatch: cannot convert from int to boolean

    Code:

    Date checkIn = req.getCheckIn();
    Date checkOut = req.getCheckOut();
    
    List<PlaceBook> filtered = checkInVal.stream().filter(string -> 
                string.getCheckInDt().compareTo(checkIn)).collect(Collectors.toList());
    
    • misko321
      misko321 about 7 years
      Maybe you wanted to use equals instead of compareTo?