在 Android 中使用日期选择器时,它选择了错误的日期 [英] When Using Date Picker In Android It is Picking the Wrong Date
本文介绍了在 Android 中使用日期选择器时,它选择了错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Android 中使用日期选择器时,它选择了错误的日期日期选择器对话框将日期选择为 2018/55/10它有时会选择正确的日期,但大多数时候它会选择错误的日期
When Using Date Picker In Android It is Picking the Wrong Date The date picker dialog is picking the date as 2018/55/10 It sometimes picks the right date but most of the time it is picking wrong date
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
得到月.
You made a common mistake in your date format: mm
is the minute, use MM
to get the month.
这篇关于在 Android 中使用日期选择器时,它选择了错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文