1

我正在尝试将 DatePicker 日期格式化为 SimpleDateFormat(“yyyy-MM-dd HH:mm:ss Z”)。有人告诉我,我需要使用 SimpleDateFormatter 将其解析为 Date 对象 - SimpleDateFormat("yyyy-MM-dd"),然后将其格式化为我需要的格式,如下所示。但是,我在 try catch 块中收到错误“重复的局部变量 eDate”。任何专家都可以检查我的代码并提供建议吗?

更新

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

            }
4

1 回答 1

3

您复制了变量的声明eDate。如果您想要覆盖该值,只需String像这样删除类型声明即可:

eDate = df.format(date);

编辑:

我想你想要的是这样的:

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));
    }
}
于 2012-11-21T14:07:20.863 回答