SimpleDateFormat错误的一天

时间:2016-06-12 17:45:21

标签: android android-studio simpledateformat

public void setDate(View view){
        Calendar calendar = Calendar.getInstance();

       new DatePickerDialog(CreateEventActivity.this,listener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar setTime=Calendar.getInstance();
            setTime.set(year,monthOfYear+1,dayOfMonth);
            eventDate=setTime.getTime();

            SimpleDateFormat dayFormat=new SimpleDateFormat("EEEE");
            eventDayOfWeek=dayFormat.format(eventDate);

            Context appContext=getApplicationContext();
            Toast.makeText(appContext,(appContext.getString(R.string.The_chosen_date_is))+": "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year+","+eventDayOfWeek,Toast.LENGTH_LONG).show();
            dateTxt.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
            dayTxt.setText(eventDayOfWeek);


        }
    };

我试图向用户显示他选择的日期(按日期选择器)。数字中的日期很好,但字符串中的日期是错误的,前2天。例如:周一它写周一而不是星期六。

1 个答案:

答案 0 :(得分:1)

你错了一个月。 OnDateSetListener个月与Calendar's一样,从零开始,您不需要+1在此调整它们:

setTime.set(year,monthOfYear+1,dayOfMonth);