我正在尝试将日期发送到数据库,同时添加一些夹具但我得到以下错误。应用程序正在崩溃。这是代码

时间:2016-03-22 08:44:07

标签: android parsing datepicker

Java代码:

 public class ForgotPinCode extends AppCompatActivity {
        public static TextView SelectedDateView;
        public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            SelectedDateView.setText(" " + (month + 1) + "-" + day + "-" + year);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_pin_code);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        android.support.v7.app.ActionBar ab = getSupportActionBar();

        // Enable the Up button
        if (ab!=null){
            ab.setHomeAsUpIndicator(R.drawable.ic_back);
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setTitle("Forgot PinCode");

        }


        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        SelectedDateView = (TextView) findViewById(R.id.forgot_pincode_DatePickerInput);
        StringBuilder sb = new StringBuilder();
        sb.append(day);
        sb.append("/");
        sb.append(month + 1);
        sb.append("/");
        sb.append(year - 18);
        String a = sb.toString();
        SelectedDateView.setText(a);
        SelectedDateView.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if (hasFocus) {
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    DialogFragment newFragment = new DatePickerFragment();
                    newFragment.show(getFragmentManager(), "datePicker");
                }       }
        });
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

    @SuppressLint("NewApi")
    public void showDatePicker(View v) {

        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");

    }

    public void SubmitRequest(View v) throws InterruptedException, ExecutionException {
        EditText t = (EditText) findViewById(R.id.forgot_pincode_username);
        String username = t.getText().toString();
        t = (EditText) findViewById(R.id.forgot_pincode_password);
        String password = t.getText().toString();
        t = (EditText) findViewById(R.id.forgot_pincode_mobile);
        String mobile = t.getText().toString();
        TextView t1 = (TextView) findViewById(R.id.forgot_pincode_DatePickerInput);
        String DOB = t1.getText().toString();
        StringTokenizer st = new StringTokenizer(DOB, "/");
        String day = "00", month = "00", year = "0000";

        if (st.hasMoreTokens()) {
            day = st.nextToken();
            int monthint= Integer.parseInt(day);
            if (monthint > 0 && monthint < 10) {
                StringBuilder sb = new StringBuilder();
                sb.append("0" + monthint);
                day = sb.toString();
            }
        }
        if (st.hasMoreTokens()) {
            month = st.nextToken();
            int monthint = Integer.parseInt(month);
            if (monthint > 0 && monthint < 10) {
                StringBuilder sb = new StringBuilder();
                sb.append("0" + monthint);
                month = sb.toString();
            }
        }
        if (st.hasMoreTokens()) {
            year = st.nextToken();
        }
        DOB = year+ "-" + month + "-" + day ;
        RequestPinCode obj = new RequestPinCode();
        if (!CheckConnection.checkStatus(this)) {
            new DialogBox(this, "No network found.", "", "Alert!");
            return;
        }
        String res = obj.execute(username, password, mobile, DOB).get();
        if (!res.isEmpty()) {
            if (res.equals("Data Not Valid")) {
                new DialogBox(this, "The data you entered in not valid.", "", "Alert!");
            }
            if (res.equals("true")) {
                new DialogBox(this, "Your pincode has been reset. Kindly view your email for details.",
                        "", "Alert!");
            }
        }
    }

Logcat:

Caused by: java.lang.NumberFormatException: Invalid int: " 3-10-1992"                                                                                        
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:375)
    at java.lang.Integer.parseInt(Integer.java:366)
    at java.lang.Integer.parseInt(Integer.java:332)
    atcom.cybussolutions.wikki.afri_pay.ForgotPinCode.SubmitRequest(ForgotPinCode.java:140)
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    atandroid.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:4084) 
    at android.view.View$PerformClick.run(View.java:16966) 
    at android.os.Handler.handleCallback(Handler.java:615) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 

错误在线“int monthint = Integer.parseInt(day);”在第一个if()条件中。我看到其他亲戚问题,但找不到我的问题的任何批准。我将不胜感激任何帮助。我尝试了所有可能的堆栈溢流方法,但还没有成功。

1 个答案:

答案 0 :(得分:0)

如果要从字符串中解析Date,请使用SimpleDateFormat。 像这样使用它:

SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy");
Date date = fmt.parse(dateString);