【发布时间】:2015-10-08 06:37:36
【问题描述】:
我正在以这种格式从 sqlite 检索日期 = strSqliteDate:= 2015-10-07 12:59:49 PM。我只想在这个日期(2015-10-07)中转换 SQLite 日期时间(strSqliteDate)。我正在尝试解析扩展 SQLiteOpenHelper 的 MyDbHelper 类中的日期。
这是我的代码
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)
如何仅转换日期字符串。感谢感谢
【问题讨论】:
-
使用简单的日期格式
-
String formattedCode = formatter.format("supply your date-time here");"supply your date-time here"似乎显然不是有效的日期字符串,它无法处理。 -
这是重复的帖子?
-
是的。检查链接并了解您的错误。
标签: android