简体   繁体   English

从SimpleDateFormat到SimpleDateFormat解析和格式化DatePicker日期

[英]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"). 我正在尝试将DatePicker日期格式化为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. 有人告诉我,我需要使用SimpleDateFormatter将其解析为Date对象 - SimpleDateFormat(“yyyy-MM-dd”),然后将其格式化为我需要的内容,如下所示。 However I'm getting an error "Duplicate local variable eDate" inside the try catch block. 但是我在try catch块中收到错误“Duplicate local variable eDate”。 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 . 您复制变量eDate的声明。 If what you want is to overwrite the value, just do it by removing the type declaration String like this: 如果您想要的是覆盖该值,只需删除类型声明String如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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