0

I've saved date to msqlite. When I retrieve it, it gives me this string "Fri Jul 12 13:16:33 GMT+00:00 2019". But, I can't get to convert it to java.sql.Date that I need... Any help is appreciated

        SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<GraphEntry> result = new ArrayList<GraphEntry>();
        Cursor cursor = db.rawQuery("SELECT * FROM graph_table", null);
        if (cursor.moveToFirst()){
            do {
                String date = cursor.getString(cursor.getColumnIndex("date"));
                int poid = cursor.getInt(cursor.getColumnIndex("poid"));

                Log.d("date before", date);

                    GraphEntry graphEntry = new GraphEntry(date, poid);
                    result.add(graphEntry);

            } while(cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return result;
 GraphEntry graphEntry = new GraphEntry(date, poid); 

tells me

GraphEntry (java.sql.Date,int) in GraphEntry cannot be applied to (java.lang.String, int)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Possible duplicate of [How to parse String to java.sql.date](https://stackoverflow.com/questions/12844006/how-to-parse-string-to-java-sql-date). I know the format used there is not the same, but you can adapt using [how to parse output of new Date().toString()](https://stackoverflow.com/questions/4713825/how-to-parse-output-of-new-date-tostring). – Ole V.V. Jul 13 '19 at 08:10
  • If I’ve understood correctly that [msqlite](http://reko.tiira.net/msqlite/) hasn’t got a date datatype, I propose that you save the date in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format, either 2019-07-12 for a date or 2019-07-12T13:16:33Z for a point in time. – Ole V.V. Jul 13 '19 at 08:31
  • Is that you own `GraphEntry` class? If so, `java.sql.date` is the completely wrong class to use in its constructor. One, it’s poorly designed and long outdated, two, it was meant for storing and retrieving dates to and from databases, not for other uses. Consider `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). If not yet on API level 26, use it through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Jul 13 '19 at 08:35

0 Answers0