-3

I'm retrieving the date from sqlite in this format = strSqliteDate﹕ = 2015-10-07 12:59:49 PM. I want to convert SQLite datetime (strSqliteDate) in this date(2015-10-07) only . I'm trying to parse the date in MyDbHelper class which is extends SQLiteOpenHelper.

Here is my code

public List<All_Post> getAllDescriptions()
     {
        List<All_Post> allPostDescList = new ArrayList<All_Post>();

        String selectQuery = "select * from  " + Table_AllPost_Table + " ORDER BY ActionDate DESC ";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do
            {
                All_Post allPostDesc = new All_Post();
                allPostDesc.setID(cursor.getInt(cursor.getColumnIndex("ALL_ACT_ID")));
                allPostDesc.setStrActivityId(cursor.getString(cursor.getColumnIndex("ActivityId")));
                allPostDesc.setStrRemark(cursor.getString(cursor.getColumnIndex("Remark")));
                allPostDesc.setStringInspectorname(cursor.getString(cursor.getColumnIndex("inspectorName")));
                allPostDesc.setStrNotationNo(cursor.getString(cursor.getColumnIndex("HashTag")));
                allPostDesc.setStrShortName(cursor.getString(cursor.getColumnIndex("Type_ShortRGN")));

                String strSqliteDate = cursor.getString(cursor.getColumnIndex("ActionDate"));
                Log.e("strSqliteDate "," = " + strSqliteDate);
                Format formatter = new SimpleDateFormat("yyyy-MM-dd");
                String formattedCode = formatter.format("supply your date-time here");
                Log.e(" newDate "," = "+formattedCode);

                allPostDesc.setActiondate(formattedCode);

                allPostDescList.add(allPostDesc);
            } while (cursor.moveToNext());
        }
        db.close();
        return allPostDescList;
    }

When run the app was crashed and getting error like IllegalArgumentException Here is my log cat error

10-08 11:52:29.061    9567-9567/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tazeen.classnkk/com.example.tazeen.classnkk.AllPosts_Page}: java.lang.IllegalArgumentException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalArgumentException
            at java.text.DateFormat.format(DateFormat.java:365)
            at java.text.Format.format(Format.java:93)
            at com.example.DBsqlite.MyDbHelper.getAllDescriptions(MyDbHelper.java:387)
            at com.example.tazeen.classnkk.AllPosts_Page.onCreate(AllPosts_Page.java:177)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            at android.app.ActivityThread.access$600(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4340)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

How to convert string in date only.Thanks to appreciate

androidTag
  • 5,053
  • 7
  • 19
  • 28

1 Answers1

0

Try this

Date strSqliteDate = cursor.getDate(cursor.getColumnIndex("ActionDate"));
DatFormat format = new SimpleDateFormat("yyyy-MM-dd");
String yourDate = format.format(strSqliteDate);

Instead of getting the date as a String, get it as a java.util.Date and use the DateFormat to correctly convert into the format you want (yyyy-MM-dd in your case).

Deval Khandelwal
  • 3,458
  • 1
  • 27
  • 38