android – 如何将当前日期与日期选择器的用户输入日期进行比较
作者:互联网
我试图限制日期和时间.我希望如果用户尝试将日期设置为小于当前日期,那么它应该显示警报,同样的事情是按时完成.我正在使用日期和时间选择器,我的onDateSetListener和onTimeSetListener如下:
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateTime.set(Calendar.YEAR, year);
dateTime.set(Calendar.MONTH, monthOfYear);
dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
update_date_display();
// ---for setting notification----
day = dayOfMonth;
month = monthOfYear;
AddEditTask.year = year;
c.set(year, month, day);
}
};
protected void update_date_display() {
et_task_date.setText(formatDate.format(dateTime.getTime()));
}
TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
dateTime.set(Calendar.MINUTE, minute);
update_time_display();
// ---for setting notification---
hour = hourOfDay;
AddEditTask.minute = minute;
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
}
};
private void update_time_display() {
et_task_time.setText(formatTime.format(dateTime.getTime()));
}
你能否告诉我在听众中写下什么才能达到我想要的目的.提前致谢…!!!
解决方法:
以这种方式创建一个新的Date对象:
Date now = new Date(System.currentTimeMillis());
并使用int result = now.compareTo(theOtherDate);
结果将是一个int< 0如果现在Date小于theOtherDate Date,如果它们相等则为0,并且int>如果此日期更大,则为0.
标签:android-datepicker,android,date,android-timepicker 来源: https://codeday.me/bug/20191007/1864151.html