Android:DatePicker textview DMY格式,并希望以YMD格式保存

时间:2015-02-26 13:53:25

标签: android simpledateformat

我有一个textview可点击并且有一个datepicker对话框。然后,datepicker将textview中的日期设置为DAY,MONTH,YEAR格式。 我希望以YEAR,MONTH,DAY格式存储它。 我希望它在textview中显示为DMY,但在SQLITE中它必须存储为YDM。 我做了以下方法,但它给了我非法的感觉。

private void updateDisplay() {
    date.setText(new StringBuilder()
            .append(mDay).append("-")
            .append(mMonth + 1).append("-")
            .append(mYear));   
String datepicker = date_tv.getText().toString();
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
datepicker = sdf.format(date_tv);
cv = new ContentValues();
cv.put(DBAdapter.COLUMN_METER_DATE, datepicker);

它给了我以下错误

02-26 08:42:07.114: E/AndroidRuntime(5109): FATAL EXCEPTION: main
02-26 08:42:07.114: E/AndroidRuntime(5109): Process: com.example.mscprojectimagebuttons, PID: 5109
02-26 08:42:07.114: E/AndroidRuntime(5109): java.lang.IllegalArgumentException: Bad class: class android.widget.TextView
02-26 08:42:07.114: E/AndroidRuntime(5109):     at java.text.DateFormat.format(DateFormat.java:296)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at java.text.Format.format(Format.java:93)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at com.example.mscprojectimagebuttons.MeterFragment$2.onClick(MeterFragment.java:98)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at android.view.View.performClick(View.java:4438)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at android.view.View$PerformClick.run(View.java:18422)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at android.os.Handler.handleCallback(Handler.java:733)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at android.os.Looper.loop(Looper.java:136)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at android.app.ActivityThread.main(ActivityThread.java:5017)
02-26 08:42:07.114: E/AndroidRuntime(5109):     at java.lang.reflect.Method.invokeNative(Native Method)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at java.lang.reflect.Method.invoke(Method.java:515)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 02-26 08:42:07.114: E/AndroidRuntime(5109):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

替换

datepicker = sdf.format(date_tv);

通过

datepicker = sdf.format(datepicker);