Phan Van Linh Phan Van Linh - 1 year ago 84
Android Question

Realm where with Date return null

Here is my Realm database
enter image description here

Here is my code for find value with where and equalTo, when I use

it working, and if I use
it not working.

You will see in the image below,
return value, and
return null
enter image description here

realm.executeTransaction(new Realm.Transaction() {
public void execute(final Realm bgRealm) {

StepEntry realmNumber = bgRealm.where(StepEntry.class).equalTo("steps", 0).findFirst();

Date date = stepEntry.getDate();
StepEntry realmDate = bgRealm.where(StepEntry.class).equalTo("date", date).findFirst();

How can I make
where work with

Answer Source

If you cannot find your object with equalTo(field, Date) then that means the Date you provide in the condition does not match.

Realm has milisecond precision since 0.90.0, so your Date must match in terms of hours, minutes, seconds, miliseconds (technically the long epoch time value must be the exact same)

StepEntry realmDate = bgRealm.where(StepEntry.class).equalTo("date", date).findFirst();

If there is an object in the database with the given date (date.getTime() is the same as for the date in the database) then it works.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download