簡體   English   中英

將日期與空值比較

[英]Compare a date with null value

我正在使用自定義適配器在活動中顯示一些帖子(由用戶創建)。 在自定義適配器的構造函數中,我正在使用以下代碼來獲取查詢結果。

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

目標是 -用戶可以為每個帖子設置過期日期,以使該帖子在過期后不再可見。 我正在嘗試將expire_date列與當前時間進行比較,以檢查帖子是否過期。

query.whereGreaterThan("expire_date", new Date()); 是后來添加的。 先前創建的所有行在expire_date列中都為值。

因此,每次我運行查詢時,都會根據條件檢查在expire_date列 具有date對象的所有行。 在expire_date列 具有空值的行將永遠不會返回

注意:expire_date列不是必填字段(用戶可以選擇在創建帖子時將其保留為空)。

是的,我可以為所有空/空字段設置默認日期以與到期日期進行比較。 但是我很好奇是否有辦法讓我要么返回所有具有空值的行,要么將當前時間與具有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)和(500英里內的location_point)]]

這就是您想要的,不是嗎?

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM