简体   繁体   中英

Query entity elements by createdAt on specific date range does not work

My entity has createdAt field which is filled by @CreatedDate when the entity is created. This property is of Date type.

class MyEntity {
    @CreatedDate
    private Date createdAt;
}

I would like to filter out all entities in a List that are in a range from startDate to endDate. The problem is that when I used findAllByCreatedAtBetween(Date startDate, Date endDate); it worked but not for all cases. When the entity is created ie: 2019-10-25 14:15:23 I would like to get it also when the user will type as @RequestParam startDate from 2019-10-24 00:00 endDate to 2019-10-25 14:15 and also take this entity. How could I ignore everything that is behind minutes? Is there a way, because when I pass those values as startDate and endDate the entity isn't found, to find it I have to change 14:15 to 14:16.

You can set any parameter without others in LocalDateTime. For example you want handle without minute. You can set your search date parameter like this:

LocalDateTime localDateTime= myDate.atTime(14,15);//The first parameter is for Hour and other one is for minute.

You can find more detail here: Java Time LocalDateTime At Time Functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM