首页 > 解决方案 > 我是否正确地构建了这个 firestore 查询?

问题描述

我需要获取指定日期/时间范围内的所有文件(都在同一天从 00:00:00 到 23:59:59)

我尝试了多个查询,文档中没有关于在不同时间查询同一日期的内容。大多数示例显示特定日期/时间,但我需要查询为今天安排的所有文档(动态)

private void loadList() {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    Date todayStart = c.getTime();
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);
    c.set(Calendar.SECOND, 59);
    Date todayEnd = c.getTime();
    Log.d(TAG, "this is the date/time for todayStart" + todayStart);
    Log.d(TAG, "this is the date/time for todayEnd" + todayEnd);

    mFirestoreDB.collection("events")
            .whereGreaterThanOrEqualTo("dateScheduled", todayStart)
            .whereLessThanOrEqualTo("dateScheduled", todayEnd)
            .get()
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {... ect

示例 Firestore 字段:dateScheduled 2019 年 5 月 5 日凌晨 12:00:00 UTC-4

示例 2:

在此处输入图像描述

Log.d 显示了今天的正确日期/时间,我有许多文件都安排了今天的日期。这应该返回今天日期的任何时间范围的所有文件。任何帮助表示赞赏!

标签: javaandroidgoogle-cloud-firestore

解决方案


推荐阅读