1
votes

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

            }
1
Your problem really doesn't have anything to do with what you're trying to achieve. The compiler has given you a pretty clear error message - what's confusing you about that error? - Jon Skeet

1 Answers

3
votes

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