参见英文答案 > SimpleDateFormat ignoring month when parsing 4个
> Getting wrong month when using SimpleDateFormat.parse 3个
> Localdate.format, format is not applied 2个
在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来获取月份.