Search code examples
javaspring-bootjava-streamjhipsterlocaldate

How would I properly filter dates that are in between a start date and end date, using the Java Streams API?


So I've been using the Java Streams API, and I'm trying to filter dates in my DB, in which the start date of something is before the current date, and the end date is after the current date. I've tried multiple things, but nothing I've tried so far seems to work.

Here's what I've tried so far:

I've tried implementing the ChronoLocalDate interface, and used .isBefore() and .isAfter()`, but the dates did not filter properly

At this point I've now tried sticking with LocalDate, and I've tried converting LocalDate.now() to the yyyy-MM-dd format (which is the date format in my DB) using the following code:

LocalDate rightNow = LocalDate.now();
String formatString = rightNow.format(DateTimeFormatter.ISO_LOCAL_DATE);

However, this doesn't work and results in the following error:

The method format(DateTimeFormatter) is undefined for the type LocalDate

I got the above code from various 2018 examples. Is LocalDate.format() already depreciated?

Also, I haven't been able to find any relevant examples for filtering between a start date and end date using LocalDate.

Any help from here would be greatly appreciated. Thanks in advance.


Solution

  • So I found out what was wrong. I was importing the wrong LocalDate from java.joda instead of java.time.

    So from here, I'm not using ChronoLocalDate anymore, but the LocalDate from java.time, and my filtering now works perfectly. Very silly mistake on my part, but I figured it out.

    Thank you everyone who also tried to help.