0

I am creating a model where i'm trying to store gregorian calendar value in a column, but its showing me error, Calendar Datatype not supported by realmProxcy.

private String alarmName;
private Boolean alarmActive = true;
private Date alarmTime;
private String alarmTonePath = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
private Boolean alarmVibrate = true;
private  Calendar cal;

@PrimaryKey public int alarmid;

Error:(30, 8) error: Type 'java.util.Calendar' of field 'cal' is not supported how can i store this calendar value and fetch it

Krishna B N
  • 65
  • 1
  • 6

3 Answers3

0

Realm database supports only Date class. You have to evaluate your Calendar instance to Date instance.

Calendar cal = Calendar.getInstance();
Date date = cal.getTime(); //save it to realm

From Realm documentation:

Realm supports the following field types: boolean, byte, short, int, long, float, double, String, Date and byte[]. The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm. Moreover, subclasses of RealmObject and RealmList are supported to model relationships.

https://realm.io/docs/java/latest/#field-types

miljon
  • 2,611
  • 1
  • 16
  • 19
0

Instead of Calendar, use Date from calendar.getTime(), if necessary store Calendar object converter to other type for save and read convert. Example:

public static Calendar toCalendarFromDate(Date date){ 
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  return cal;
}
Thiago Neves
  • 91
  • 1
  • 7
  • HI Thiago, i have implemented the same but, not able to store time along with the date so that i can convert it back to the calendar – Krishna B N Mar 15 '17 at 13:16
0

If you really need to store a Calendar instance, you can take advantage of it being a Serializable and the possibility of storing byte[] arrays in Realm objects - serialize your Calendar to byte array for storage and deserialize from byte array when accessing data.

Your object would look like this:

class MyRealmObject extends RealmObject {
    private String alarmName;
    private Boolean alarmActive = true;
    private Date alarmTime;
    private String alarmTonePath = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
    private Boolean alarmVibrate = true;
    private byte[] serializedCalendar;

    public Calendar getCalendar() {
        return deserializeCalendar(serializedCalendar);
    }

    public void setCalendar(Calendar calendar) {
        this.serializedCalendar = serializeCalendar(calendar);
    }
}

Refer to this answer on Object<>byte[]serialization/deserialization on how to implement methods:

byte[] serializeCalendar(Calendar c);
Calendar deserializeCalendar(byte[] arr);
Community
  • 1
  • 1
maciekjanusz
  • 4,702
  • 25
  • 36
  • This is what i was actually looking for, but i have implemented using a method toCalendar function as below public static Calendar toCalendar(Date date){ Calendar cal = new GregorianCalendar(); cal.setTime(date); Log.e(Constants.TAG, "toCalendar: "+cal); return cal; } Which is the best way @maciekjanusz – Krishna B N Mar 16 '17 at 12:12