2015-10-08 47 views
-3

我从sqlite以这种格式检索日期= strSqliteDate:= 2015-10-07 12:59:49 PM。我想在这个日期(2015-10-07)只转换SQLite datetime(strSqliteDate)。我试图解析MyDbHelper类中扩展SQLiteOpenHelper的日期。Android:仅在日期时间内转换SQLite日期

这里是我的代码

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; 
    } 

当运行应用程序是应声而得到错误,如抛出:IllegalArgumentException 这是我的日志猫错误

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) 

如何日期only.Thanks字符串转换为欣赏

+0

http://stackoverflow.com/a/18733587 – Sree

+0

使用简单的日期格式 –

+0

'String formattedCode = formatter.format(“supply your date-time here”);'显然**明显**''提供你的日期时间在这里“'不是有效的日期字符串,它**不能被处理**。 –

回答

0

试试这个

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

而不是将日期作为String,请将其作为java.util.Date并使用DateFormat正确地转换为所需的格式(您的情况为yyyy-MM-dd)。

+1

你能解释一下你添加了什么吗? – dsharew

+0

@DegenSharew他简单地展示了SimpleDateFormat的**正确使用方法。 –

+0

@DegenSharew阅读编辑答案 – khandelwaldeval