在 Android 房间查询中有没有办法比较格式日期的大小?

Is there a way to compare the size of format date in Android room query?

我先给你看代码

@Query("SELECT * FROM plan WHERE (DATETIME(startDate, '%Y-%m-%d') <= :temp OR DATETIME(endDate, '%Y-%m-%d') >= :temp) OR specificDate = :temp")
fun getPlanByDate(temp: String): List<PlannerEntity>

在名为 plan 的 table 中,有 startDateendDatespecificDate 列。基于名为 temp 的日期,我只想获取属于 temp 的数据。 temp 的格式是“yyyy-MM-dd”。 startDateendDate 是以“yyyy-MM-dd HH:mm”格式存储的,所以你可以看到我把它改成“yyyy-MM-dd”格式的努力。如何将格式 yyyy-MM-dd HH:mm 更改为 yyyy-MM-dd 格式?

How can I change the format yyyy-MM-dd HH:mm to yyyy-MM-dd format?

使用date(the_column)strftime('%Y-%m-%d',the_column)

但是,我认为您的逻辑是不正确的,因为只有当结束日期小于开始日期时才会排除一行。 根据 :-

你可能想要 AND 而不是 OR
.... WHERE (date(startDate) <= :temp AND date(endDate) >=  :temp) OR date(specificDate) = :temp

或者更简单的 :-

.... WHERE  :temp BETWEEN date(startDate) AND date(endDate) OR date(specificDate) = :temp

您可能希望参考 SQLite Date And Time Functions,其中涵盖了 datedatetimetimejuliandaystrftime 函数(即日期操作)。