微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从EditText获取日期,然后将其存储在Android的数据库中

我正在使用edittext作为用户输入的用户输入他的出生日期. edittext的输入类型是Date,然后我将该日期传递给变量.该日期未传递给该变量并给出一些错误.

我正在使用的代码如下

Edit dob=(EditText)findViewById(R.id.dob);
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy/MM/dd" );
    String dob_var=sdf.format(dob.getText());
    //dob_var=dob.getText().toString();
    System.out.println(dob_var);

之后,我想在数据库中传递该日期,以便将该日期存储在数据库中的类型是什么.
我的logcat是:

    04-05 16:54:22.060: D/AndroidRuntime(3104): Shutting down VM
04-05 16:54:22.146: E/AndroidRuntime(3104): FATAL EXCEPTION: main
04-05 16:54:22.146: E/AndroidRuntime(3104): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.foursquaregame.in/com.foursquaregame.in.Astro_talk}: java.lang.IllegalArgumentException
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2663)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.os.Looper.loop(Looper.java:123)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at java.lang.reflect.Method.invoke(Method.java:521)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at dalvik.system.NativeStart.main(Native Method)
04-05 16:54:22.146: E/AndroidRuntime(3104): Caused by: java.lang.IllegalArgumentException
04-05 16:54:22.146: E/AndroidRuntime(3104):     at java.text.DateFormat.format(DateFormat.java:373)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at java.text.Format.format(Format.java:133)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at com.foursquaregame.in.Astro_talk.onCreate(Astro_talk.java:32)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-05 16:54:22.146: E/AndroidRuntime(3104):     at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2627)
04-05 16:54:22.146: E/AndroidRuntime(3104):     ... 11 more

解决方法:

终于搞定了,

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // Make sure user insert date into edittext in this format.

Date dateObject;

try{
String dob_var=(tx.getText().toString());

dateObject = formatter.parse(dob_var);

date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);
time = new SimpleDateFormat("h:mmaa").format(dateObject);
}

catch (java.text.ParseException e) 
    {
    // Todo Auto-generated catch block
        e.printstacktrace();
        Log.i("E11111111111", e.toString());
    }

    Toast.makeText(getBaseContext(), date + time, Toast.LENGTH_LONG).show();

希望对你有帮助…

谢谢…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐