简体   繁体   English

将日期与空值比较

[英]Compare a date with null value

I am showing some posts (created by the users) in an activity using a custom adapter. 我正在使用自定义适配器在活动中显示一些帖子(由用户创建)。 In the constructor of the custom adapter, I am using the following code to get the query result. 在自定义适配器的构造函数中,我正在使用以下代码来获取查询结果。

ParseQuery query = new ParseQuery("Post");
query.whereWithinMiles("location_point", my_point, 500);
query.whereGreaterThan("expire_date", new Date());
return query;

The target is - the user can set an expire date with each post so that the post will no longer be visible after the expiration. 目标是 -用户可以为每个帖子设置过期日期,以使该帖子在过期后不再可见。 I am trying to compare the expire_date column with current time for checking if the post is expired or not. 我正在尝试将expire_date列与当前时间进行比较,以检查帖子是否过期。

The line query.whereGreaterThan("expire_date", new Date()); query.whereGreaterThan("expire_date", new Date()); was added later. 是后来添加的。 All the previously created rows has a null value in the expire_date column. 先前创建的所有行在expire_date列中都为值。

So each time I run the query, all the rows that has a date object in expire_date column are being checked according to the condition. 因此,每次我运行查询时,都会根据条件检查在expire_date列 具有date对象的所有行。 The rows having a null value in expire_date column are never returned . 在expire_date列 具有空值的行将永远不会返回

Note: The expire_date column is not a mandatory field (user can choose to leave it empty during creating a post). 注意:expire_date列不是必填字段(用户可以选择在创建帖子时将其保留为空)。

Yes, I can set a default date for all the empty/null fields to compare with the expire date. 是的,我可以为所有空/空字段设置默认日期以与到期日期进行比较。 But I was curious to know if there is a way so that I can either return all the rows having a null value or compare the current time with a null object with the whereGreaterThan condition? 但是我很好奇是否有办法让我要么返回所有具有空值的行,要么将当前时间与具有whereGreaterThan条件的空对象进行whereGreaterThan

ParseQuery notExpiredPosts = new ParseQuery("Post");
query.whereGreaterThan("expire_date", new Date());

ParseQuery noExpiredDate = new ParseQuery("Post");
query.doesNotExist("expire_date");

ParseQuery expiredDateQuery = ParseQuery.or(notExpiredPosts, noExpiredDate);

ParseQuery query = new ParseQuery("Post");
query.whereWithinMiles("location_point", my_point, 500);
query.whereMatchesKeyInQuery("objectId", "objectId", expiredDateQuery);
return query;

[(NotExpiredPost || NoExpiredDate) & (location_point within 500 mile)] [((NotExpiredPost || NoExpiredDate)和(500英里内的location_point)]]

This is what you want, isn't it? 这就是您想要的,不是吗?

Hope this helps :) 希望这可以帮助 :)

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

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