user9214790
user9214790

Reputation:

how to include the end date in a filter of java stream

i am streaming a list which has data corresponding to dates and am using this filter filter(s->s.getTime().before(enddate)) to filter the values from the stream between two dates inclusive of the lastdate.

What can I change here so that it includes the endDate? I tried with replacing before with equals but that doesn't help.

Upvotes: 1

Views: 3395

Answers (2)

Aman J
Aman J

Reputation: 452

To Keep things simple you can just use long values :

filter(s-> (s.getTime()<=enddate.getTime()))

Since this uses only a single filter, and works with (long) numbers, so you can change the filter as per your liking very easily

Upvotes: 1

jschnasse
jschnasse

Reputation: 9558

Assuming you are using java.util.Date you could try

filter(s-> {return s.before(enddate) || s.equals(enddate);})

or simply (thanks to chai-t-rex)

filter(s-> !s.after(enddate));

By my understanding this should return true for all s of type Date before or equal to enddate of type Date

Upvotes: 2

Related Questions