Search code examples
androidrealm

RealmResult dealing with time constraints


How to modify realm result data when dealing with date and time? ASCENDING and DESCENDING is not enough for me.

Say for example I am getting a task thats within an hour of due?

 RealmResults<Task> tmp = realm.where(Task.class).findAll();
 for (final Task task : tmp) {
            String dateFormat = Utility.TIMEFORMAT;
            String current = "2:30:00 PM";
            Date currentTime = new Date(task.gettime());
            //currentTime = new SimpleDateFormat(dateFormat).parse(current);
                    if( isIncomingWithInHour(currentTime, calendar)){
                       realmresult.add(tmp) /// this result in error.
                    }
        }

As you can see ASCENDING and DESCENDING wont work in this kind of sorting. Anyway to give the reamlResult back to the adapter? Realmresult has an onchangeListener and I want to use that.


Solution

  • Instead of

    RealmResults<Task> tmp = realm.where(Task.class).findAll();
    

    You can do something like

    RealmResults<Task> tasksInOneHour = realm.where(Task.class)
                                            .greaterThanOrEqualTo("time", startTime)
                                            .lowerThan("time", endTime)
                                            .findAll();