简体   繁体   English

在特定日期范围内通过 createdAt 查询实体元素不起作用

[英]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.我的实体有 createdAt 字段,该字段在创建实体时由@CreatedDate填充。 This property is of Date type.该属性是Date类型。

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.我想过滤掉List中从 startDate 到 endDate 范围内的所有实体。 The problem is that when I used findAllByCreatedAtBetween(Date startDate, Date endDate);问题是当我使用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.创建实体时,即:2019-10-25 14:15:23 当用户从 2019-10-24 00:00 endDate 到 2019-10-25 14 键入@RequestParam startDate 时,我也想得到它:15 也拿这个实体。 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.有没有办法,因为当我将这些值作为 startDate 和 endDate 传递时,找不到实体,要找到它,我必须将 14:15 更改为 14:16。

You can set any parameter without others in LocalDateTime.您可以在 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您可以在此处找到更多详细信息: Java Time LocalDateTime At Time Functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CURRENT_DATE的JPQL查询-1不起作用 - JPQL query with CURRENT_DATE - 1 does not work 如何按特定月份获取已createAt 参数类型Date 的所有实体? - How to fetch all entities which has createdAt parameter type Date by specific Month? 使用 Hibernate 本机查询插入不适用于 java.util.Date - Insert with Hibernate native query does not work for java.util.Date 带有特定条件计数的 Mongo DB 聚合并按输出投影的日期范围过滤无法按预期工作 - Mongo DB Aggregation with Count for Specific Conditions and filter by Date Range which Outputs a Projection Doesn't work as Expected 日期范围查询不起作用mongoDB - Date Range Query not working mongoDB 实体观察者并不总是有效 - Observer on Entity does not always work 为Hibernate实体的特定字段设置查询 - Setting up query for specific field of Hibernate entity 在存储库中保存实体不起作用SPRING - Saving entity in repository does not work SPRING 使用Spring REST分页在MongoDB中对ObjectId进行日期范围查询 - Date range query on ObjectId in MongoDB using Spring REST pagination 按日期查询(db中的Oracle日期和Java实体中的本地日期)在Spring Java中不起作用 - Query by date(oracle date in db and local date in java entity) is not working in spring java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM