问题描述
我尝试了一些领域查询,但没有得到我想要的结果
I have tried some realm queries but not getting that result which I want
让我们以下面的表为例(领域表):
let's consider below table for example(Realm table):
id Name DateTimeStamp
1 A 2017-01-01 08:00:00
2 B 2017-01-01 15:00:00
3 C 2017-01-02 08:00:00
现在,我想要所有与日期"2017-01-01"匹配的记录.因此,在这种情况下,我应该从realm表中获得2条记录.
Now I want all those records which match the date "2017-01-01".so for this case I should get 2 records from realm table.Right now I am not getting any record.
有关信息,"DateTimeStamp"列的数据类型为"Date".是的,我需要将日期和时间都存储在数据库中,如表所示.
For information "DateTimeStamp" column datatype is "Date".and yes I need to store date and time both in the database as you can see on the table.
我尝试过的查询是:
long cnt = realm.where(ReservationObject.class).equalTo("DateTimeStamp",dateObject).count();
请帮助我...预先感谢...
Please help me with this...Thanks in advance...
推荐答案
您需要为2017-01-01 00:00:00
和2017-01-02 00:00:00
设置Date
对象并在它们之间进行查询.
You need to set up Date
objects for 2017-01-01 00:00:00
and 2017-01-02 00:00:00
and query inbetween.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1); // make sure month stays valid
calendar.set(Calendar.YEAR, 2017);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Date jan1 = new Date(calendar.getTimeInMillis());
calendar.set(Calendar.DAY_OF_MONTH, 2);
Date jan2 = new Date(calendar.getTimeInMillis());
然后
realm.where(ReservationObject.class)
.greaterThanOrEqualTo("DateTimeStamp", jan1)
.lessThan("DateTimeStamp", jan2)
.findAll()
这篇关于如何使用“日期对象"从“领域数据库"中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!