Check date between two other dates spring data jpa

后端 未结 4 1061
[愿得一人]
[愿得一人] 2020-12-08 03:34

I have this model:

public class Event {
    private String name;
    private Date start;
    private Date end;
}

and repository as

相关标签:
4条回答
  • 2020-12-08 04:15

    I did use following solution to this:

    findAllByStartDateLessThanEqualAndEndDateGreaterThanEqual(OffsetDateTime endDate, OffsetDateTime startDate);
    
    0 讨论(0)
  • 2020-12-08 04:27

    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

    0 讨论(0)
  • 2020-12-08 04:33

    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);
    
    0 讨论(0)
  • 2020-12-08 04:38

    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.

    0 讨论(0)
提交回复
热议问题