0

I am using realm database. I need to compare two dates from which i am storing in model.But the dates i am storing in string.If i use between query its throwing an error.

Error: java.lang.IllegalArgumentException: Field 'appointmentDate': type mismatch. Was STRING, expected [INTEGER].

json: [AppointmentBook = [{id:1},{patientID:20059},{appointmentDate:2017-07-13},{appointmentCustomerName:nitish patra},{appointmentCustomerNumber:9382882382},{appointmentStartTime:18:15},{appointmentEndTime:19:15},{reason:sass},{status:null}]]

 appoint = realm.where(AppointmentBook.class).between("appointmentDate",2017-07-13, 2017-07-14).findAll();

Since i am storing date as string not able to compare two dates.How to achieve this to get date which lies between "2017-07-13" to "2017-07-13".Is there any query to achieve this

Model class:

public class AppointmentBook extends RealmObject{

    @PrimaryKey
    public long id;


    private long patientID;
    private String appointmentDate;
    private String appointmentCustomerName;
    private String appointmentCustomerNumber;
    private String appointmentStartTime;
    private String appointmentEndTime;
    private String reason;
    private String status;


    public AppointmentBook(){

    }

    public AppointmentBook(JsonObject data) throws ParseException {
        this.id = data.get("id").getAsLong();
        this.patientID = data.get("patientID").getAsInt();
        this.appointmentDate = data.get("appointmentDate").getAsString();
        this.appointmentCustomerName = data.get("customerName").getAsString();
        this.appointmentCustomerNumber = data.get("CustomerNumber").getAsString();
        this.appointmentStartTime = data.get("startTime").getAsString();
        this.appointmentEndTime = data.get("endTime").getAsString();
        this.reason = data.get("reason").getAsString();
        this.status = data.get("status").getAsString();
    }



    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getPatientID() {
        return patientID;
    }

    public void setPatientID(long patientID) {
        this.patientID = patientID;
    }

    public String getAppointmentDate() {
        return appointmentDate;
    }

    public void setAppointmentDate(String appointmentDate) {
        this.appointmentDate = appointmentDate;
    }

    public String getAppointmentCustomerName() {
        return appointmentCustomerName;
    }

    public void setAppointmentCustomerName(String appointmentCustomerName) {
        this.appointmentCustomerName = appointmentCustomerName;
    }

    public String getAppointmentCustomerNumber() {
        return appointmentCustomerNumber;
    }

    public void setAppointmentCustomerNumber(String appointmentCustomerNumber) {
        this.appointmentCustomerNumber = appointmentCustomerNumber;
    }

    public String getAppointmentStartTime() {
        return appointmentStartTime;
    }

    public void setAppointmentStartTime(String appointmentStartTime) {
        this.appointmentStartTime = appointmentStartTime;
    }

    public String getAppointmentEndTime() {
        return appointmentEndTime;
    }

    public void setAppointmentEndTime(String appointmentEndTime) {
        this.appointmentEndTime = appointmentEndTime;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "AppointmentBook{" +
                "id=" + id +
                ", patientID=" + patientID +
                ", appointmentDate='" + appointmentDate + '\'' +
                ", appointmentCustomerName='" + appointmentCustomerName + '\'' +
                ", appointmentCustomerNumber='" + appointmentCustomerNumber + '\'' +
                ", appointmentStartTime='" + appointmentStartTime + '\'' +
                ", appointmentEndTime='" + appointmentEndTime + '\'' +
                ", reason='" + reason + '\'' +
                ", status='" + status + '\'' +
                '}';
    }


}
winchester100
  • 147
  • 3
  • 13

1 Answers1

2

Realm supports the Date type. You are storing a String, it doesn't know that it represents a Date.

If you are using Retrofit with Gson you can do something like this :

Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd")
.create();

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setConverter(new GsonConverter(gson))
.build();

source

But I have no idea how you handle your network requests.

andrei
  • 2,934
  • 2
  • 23
  • 36
  • How to achieve this in string. Is there any query or can i cast it before checking – winchester100 Jul 13 '17 at 09:49
  • @abhinayg you can't as far as I know. It's just a string. If you want a Date you should use a Date. You can deserialise your JSON directly to a Date. If you use Gson I think you can specify the date format with setDateFormat – andrei Jul 13 '17 at 09:55
  • how to deserialise JSON to date. will be helpful if you write code for this – winchester100 Jul 13 '17 at 10:00
  • If you keep your Realm models separate from your API responses, this is not even a question that needs to be answered. And life becomes much more simple (and maintainable) – EpicPandaForce Jul 13 '17 at 10:11
  • @EpicPandaForce This is a good point. But having different Realm and API models, doesn't this complicate things by needing to transform one model into the other every time we need to manipulate them? – andrei Jul 13 '17 at 10:29
  • Technically you need to define a mapping either way if your model and the API don't match, if you use the same class you'll need to move the mapping to be a JsonDeserializer. – EpicPandaForce Jul 13 '17 at 10:38
  • @EpicPandaForce : can you please explain how to keep realm models separate from API reference..Examples will be fine. Thanks:) – winchester100 Jul 13 '17 at 11:36
  • @EpicPandaForce : Please check my model class where "appointmentDate" is String. – winchester100 Jul 13 '17 at 11:42
  • @abhinayg Exactly, it's a `String` even though it's appointment***Date*** - clearly it should be a `Date` – EpicPandaForce Jul 13 '17 at 11:43
  • @EpicPandaForce : I changed to date, but how to get the data. this.appointmentDate = data.get("appointmentDate").getAsString(); There is no option for getAsDate(). – winchester100 Jul 13 '17 at 11:50
  • 1
    @abhinayg look at SimpleDateFormat on how to parse the string to a Date – andrei Jul 13 '17 at 11:51
  • @andrei: ok will check.Thanks – winchester100 Jul 13 '17 at 11:53