Check date between two other dates spring data jpa

We Are Going To Discuss About Check date between two other dates spring data jpa. So lets Start this Java Article.

Check date between two other dates spring data jpa

  1. Check date between two other dates spring data jpa

    You should take a look the reference documentation. It's well explained.
    In your case, I think you cannot use between because you need to pass two parameters

  2. Check date between two other dates spring data jpa

    You should take a look the reference documentation. It's well explained.
    In your case, I think you cannot use between because you need to pass two parameters

Solution 1

You should take a look the reference documentation. It’s well explained.

In your case, I think you cannot use between because you need to pass two parameters

Between – findByStartDateBetween … where x.startDate between ?1 and ?2

In your case take a look to use a combination of LessThan or LessThanEqual with GreaterThan or GreaterThanEqual

  • LessThan/LessThanEqual

LessThan – findByEndLessThan … where x.start< ?1

LessThanEqual findByEndLessThanEqual … where x.start <= ?1

  • GreaterThan/GreaterThanEqual

GreaterThan – findByStartGreaterThan … where x.end> ?1

GreaterThanEqual – findByStartGreaterThanEqual … where x.end>= ?1

You can use the operator And and Or to combine both.

Original Author Pau Of This Content

Solution 2

I did use following solution to this:

findAllByStartDateLessThanEqualAndEndDateGreaterThanEqual(OffsetDateTime endDate, OffsetDateTime startDate);

Original Author Michaël Cuypers Of This Content

Solution 3

You can also write a custom query using @Query

@Query(value = "from EntityClassTable t where yourDate BETWEEN :startDate AND :endDate")
public List<EntityClassTable> getAllBetweenDates(@Param("startDate")Date startDate,@Param("endDate")Date endDate);

Edit: For LocalDateTime I just tried this solution for filtering record based on date and type in spring JPA:

Page<MyEntity> findMyEntityByEntityDateBetweenAndType(
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate, @Param("type") String type, Pageable pageable);

Here EntityDate can be like CreatedDate,ModifiedDate etc.

Original Author Vikash Kumar Of This Content

Solution 4

Maybe you could try

List<Article> findAllByPublicationDate(Date publicationDate);

The detail could be checked in this article:

https://www.baeldung.com/spring-data-jpa-query-by-date

Original Author Wu Yaozu Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment