0

To store a Calendar object in SQLite database, i found that the easiest way is to convert the Calendar object to a string and store it in the database as text.

Now, the problem lies in extracting the stored date from the string.

How do I parse the string containing the Calendar object and set it to a Calendar value?

My code is:

    String CREATE_DOCTORS_TABLE = "CREATE TABLE " + TABLE_DOCTORS + "("
            + KEY_ID_DOC + " INTEGER PRIMARY KEY," + KEY_DOCTOR_NAME + " TEXT,"             
            + KEY_CLINIC_ADDRESS + " TEXT," + KEY_LAST_CHECKUP + " TEXT" + ");";
    db.execSQL(CREATE_DOCTORS_TABLE);       

where KEY_LAST_CHECKUP contains a value like this:

java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=4,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=16,DAY_OF_YEAR=198,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=19,SECOND=47,MILLISECOND=823,ZONE_OFFSET=19800000,DST_OFFSET=0]

and i've stored it in a database this way:

values.put(KEY_LAST_CHECKUP, (doctor.getLastCheckUpDate().toString())); // last check up date

Now, how do i retrieve the DAY, MONTH and YEAR from the stored string?

I read about how to convert a date string to a calendar object here: How to convert a date String to a Date or Calendar object?

but my string is not just a date string. It contains a lot of other details too.

What is the best way forward?

Community
  • 1
  • 1
Randomly Named User
  • 1,889
  • 7
  • 27
  • 47

2 Answers2

5

Change your data model to use a Date. This is the usual type to be stored in the database.

You can set the Date to a Calendar by using

Calendar c = Calendar.getInstance();
c.setTime(date);

To retrieve the Date from a Calendar you can use

date = c.getTime();

Using a String to store a Date in a database needs formatting and parsing of the Strings and also no comparision iside the database can be done.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
  • I tried creating my datamodel with Date, instead of String. But then, i cannot use the put method in that situation. That is, values.put(KEY_LAST_CHECKUP, doctor.getLastCheckUpDate(); gives me an error saying that The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Calendar) – Randomly Named User Jul 17 '13 at 11:17
  • @Kaushal What type is the variable `values`? Also you have to use `values.put(KEY_LAST_CHECKUP, doctor.getLastCheckUpDate().getTime());` then to get the `Date` instead of the `Calendar`. – Uwe Plonus Jul 17 '13 at 11:20
  • `values` is a `ContentValues` object. also, `doctor.getLastCheckUpDate()` is a method that returns a calendar object itself. – Randomly Named User Jul 17 '13 at 11:26
  • 1
    For `ContentValues` you can use `values.put(KEY_LAST_CHECKUP, doctor.getLastCheckUpDate().getTimeInMillis());` to store it as `long`. To set the `Calendar` again use `cal.setTimeInMillis()`. – Uwe Plonus Jul 17 '13 at 11:31
  • yeah, that sounds very doable. Now if I put a `long` value using `values.put`, then i can just use the `integer` data type while creating the table in sqlite, right? – Randomly Named User Jul 17 '13 at 11:35
3

If you would like to keep string value in KEY_LAST_CHECKUP column. Try to use SimpleDateFormat.

If you keep long value, you don't need to use SimpleDateFormat.

For insert :

SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String strDate = simpleFormat.format(doctor.getLastCheckUpDate());
values.put(KEY_LAST_CHECKUP, strDate);

For retrieve:

try {
    String strDate = --> from DB
    Date parsedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(strDate);
    Calendar c = Calendar.getInstance();
    c.setTime(parsedDate);
} catch (ParseException e) {
    return "Unknown";
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131