在Android中使用日期选择器时选择错误的日期日期选择器对话框将日期选择为 2018/55/10 有时会选择正确的日期,但大多数情况下会选择错误的日期
Calendar myCalendar = Calendar.getInstance(); DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub myCalendar.set(Calendar.YEAR, year); myCalendar.set(Calendar.MONTH, monthOfYear); myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateLabel(); } }; exp.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub DatePickerDialog d=new DatePickerDialog(createjobs.this, R.style.CalendarDatePickerDialog, date, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)); d.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); d.show(); } }); private void updateLabel() { String myFormat = "yyyy/mm/dd"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); exp.setText(sdf.format(myCalendar.getTime())); }
您在日期格式中犯了一个常见错误:mm是分钟,用于MM获取月份。
mm
MM