I am currently working with streams in Java. I am creating a list which stores table values as an object from MySQL.
I want to filter the contents by comparing with a year in the filter. However, the date column in the table has the format dd-mm-yyy
. When I try to compare by using getYear()
with my input year, for example 2018
, it is showing an wrong output (I know getYear() has been deprecated).
How to compare two dates using streams which is in the format dd-mm-yyy
?
Below contains the code snippet I am working with:
public List<Payment> findCustomerByYear(int year){
List<Payment> list1 = paymentDao.getAllRecord();
List<Payment> list2 = new ArrayList<>();
list2 = list1.stream()
.filter(i -> i.getPaymentDate().getYear() == year)
.collect(Collectors.toList());
return list2;
}
Is there any other option to compare dates instead of using getYear()
in list2
?