How can i get the date from the EditText and then store it in database in Android

匿名 (未验证) 提交于 2019-12-03 02:27:02

问题:

i am using an edittext for the user input in which user entered his Date of birth. The input type of the edittext is Date and then i am passing that date to a variable. That date is not passed to that variable and giving some error.

The code i am using is below

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

After that i want to pass that date in database so what is the type require to store that date in database. My logcat is:

    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 

回答1:

Finally got it,

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(); 

Hope this will help you...

Thanks...



回答2:

First you need to convert your edittext's string into date:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); Date dob_var = sdf.parse(dob.getText()); 

now convert dob_var into sqlite standard date string:

DateFormat dateFormatISO8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String strDob = dateFormatISO8601.format(dob_var);  /* save strDob into database */ 


回答3:

try with

 String dob_var=sdf.format(dob.getText().toString().trim()); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!