简体   繁体   中英

Parse and Format DatePicker date from SimpleDateFormat to SimpleDateFormat

I'm trying to format DatePicker date to the SimpleDateFormat ("yyyy-MM-dd HH:mm:ss Z"). Someone has told me that I need to parse it to Date object - SimpleDateFormat("yyyy-MM-dd") using SimpleDateFormatter and then format it to what I need like below. However I'm getting an error "Duplicate local variable eDate" inside the try catch block. Could any expert kindly review my code and advise?

Updated

@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case EDATE_DIALOG_ID:
            return new DatePickerDialog(this, 
            sDateSetListener, mYear, mMonth, mDay);        
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener sDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, 
            int year, int monthOfYear, int dayOfMonth) {

                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;                                
                updateDate();
            }
    };

    private void updateDate() {
        inputEdate.setText(
            new StringBuilder()                
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));                
    }


    class CreateNewRequest extends AsyncTask<String, String, String> {

        protected String doInBackground(String... args) {

            Calendar c = Calendar.getInstance();

            SimpleDateFormat firstDateFormat = 
            new SimpleDateFormat("yyyy-MM-dd");

            SimpleDateFormat secondDateFormat = 
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

            String eDate = inputEdate.getText().toString();             

            try{

                Date date = firstDateFormat.parse(eDate);
                String eDate = secondDateFormat.format(date);

                }catch(ParseException e) {
                e.printStackTrace();
                }

            String submitDate = secondDateFormat.format(c.getTime());

            List<NameValuePair> params = new ArrayList<NameValuePair>();            

            params.add(new BasicNameValuePair("submitDate", submitDate));
            params.add(new BasicNameValuePair("request_date", eDate));

            }

Your a duplicating the declaration of the variable eDate . If what you want is to overwrite the value, just do it by removing the type declaration String like this:

eDate = df.format(date);

Edit:

I think what you want is this:

class CreateNewRequest extends AsyncTask<String, String, String> {
    protected String doInBackground(String... args) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat firstDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat secondDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");

        String eDate = inputEdate.getText().toString();             

        try {
            Date date = firstDateFormat.parse(eDate);
            eDate = secondDateFormat.format(date);
            submitDate = secondDateFormat.format(c.getTime());
        } catch(ParseException e) {
            e.printStackTrace();
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();            
        params.add(new BasicNameValuePair("submitDate", submitDate));
        params.add(new BasicNameValuePair("request_date", eDate));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM