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天。例如:周一它写周一而不是星期六。
答案 0 :(得分:1)
你错了一个月。 OnDateSetListener
个月与Calendar
's一样,从零开始,您不需要+1
在此调整它们:
setTime.set(year,monthOfYear+1,dayOfMonth);