如何在 Android 日期选择器中禁用未来的日期

如何在 Android 日期选择器中禁用未来的日期

Java 代码:

mExpireDate.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {


// To show current date in the datepicker
final Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);


DatePickerDialog mDatePicker = new DatePickerDialog(
EventRegisterActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker,
int selectedyear, int selectedmonth,
int selectedday) {


mcurrentDate.set(Calendar.YEAR, selectedyear);
mcurrentDate.set(Calendar.MONTH, selectedmonth);
mcurrentDate.set(Calendar.DAY_OF_MONTH,
selectedday);
SimpleDateFormat sdf = new SimpleDateFormat(
getResources().getString(
R.string.date_card_formate),
Locale.US);


mExpireDate.setText(sdf.format(mcurrentDate
.getTime()));
}
}, mYear, mMonth, mDay);


mDatePicker.setTitle(getResources().getString(
R.string.alert_date_select));
mDatePicker.show();
}
});

怎么做?

79394 次浏览

如果用户选择未来日期,然后更新数据采集器到当前日期(今天)

您可以使用以下代码来检查所选日期是否为将来的日期

final Calendar cal = Calendar.getInstance();
datePickerDob.init(currentYear, currentMonth, currentDay,
new OnDateChangedListener() {


@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar selectedCal = Calendar.getInstance();
selectedCal.set(year, monthOfYear, dayOfMonth);


long selectedMilli = selectedCal.getTimeInMillis();


Date datePickerDate = new Date(selectedMilli);
if (datePickerDate.after(new Date())) {


datePickerDob.updateDate(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));


} else {




}


}
});

还可以使用 compareTo ()方法

datePickerDate.compareTo(new Date());

将接收方与指定的 Date 进行比较,以确定相对顺序。

参数 与之比较的日期。

报税表 如果这个 Date 小于指定的 Date,则为 int < 0; 如果它们相等,则为0; 如果这个 Date 更大,则为 int > 0。

使用 getDatePicker()DatePickerDialog中获取 DatePicker。使用 setMaxDate()将最大日期设置为当前日期:

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

需要 API 等级11。

可以调用 getDatePicker ()。在 DatePickerDialog 上设置 setMaxDate (long)为今天的最大日期。您可以使用您发布的代码片段中的相同名称更新函数。

注意: DatePickerDialog 是我在 Android Docs 中从我发布的链接中引用的对象。

@Override
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
//start changes...
DatePickerDialog dialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
//end changes...
}
return null;
}

试试这个,给你的反馈! ! !

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog = new DatePickerDialog(getActivity(), ondateSet, year, month, day);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
dialog.getDatePicker().setMaxDate(newDate.getTime() - (newDate.getTime() % (24 * 60 * 60 * 1000)));
return  dialog;
}
**new Way**


ed_date.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);


final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;


}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());


// TODO Hide Past Date Here
//  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});

如果你使用材质设计数据采集器:

    Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(this,
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show(getFragmentManager(), "datepicker");
datePickerDialog.setMaxDate(calendar);
 // Custom Your Future Dates


// Example for today and next 3 days :- 3(NUMBER OF NEXT DAYS)*24*60*60*1000l


// As 24 represents hrs in day


// As 60 mins 60 secs and convert it to millisec


//Inside Class which implements DatePickerDialog.OnDateSetListener
private Calendar mCurrentDate;


//Inside OnCreate Method
mDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


mCurrentDate = Calendar.getInstance();
int year = mCurrentDate.get(Calendar.YEAR);
int month = mCurrentDate.get(Calendar.MONTH);
int day = mCurrentDate.get(Calendar.DAY_OF_MONTH);


DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, this, year, month, day);
mDatePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 3 * 24 * 60 * 60 * 1000 l);
}
});




@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mDateEditText.setText(dayOfMonth + "/" + month + "/" + year);


}

下面的代码可以帮助您禁用将来的日期:

在全局范围内声明日历变量:

private Calendar myCalendar = Calendar.getInstance();

在 onCreate 方法中输入以下代码:

DatePickerDialog.OnDateSetListener dateListener = 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();
}


};

在按钮上点击放置以下代码:

DatePickerDialog datePickerDialog=new DatePickerDialog(getActivity(), dateListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));


//following line to restrict future date selection
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
datePickerDialog.show();

把最长日期定到明天 代码中的最后一个数字1表示要设置它的天数。

  pickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 1));

在 Kotlin

cvCalendar.maxDate = System.currentTimeMillis()

在 kotlin 中,我们还可以在 setMaxDate (Date () . time-86400000)中传递此信息

比如说

dpd.datePicker.setMaxDate(Date().time - 86400000)
dpd.show()

这里我使用 dpd 作为 DatePickerDialog 的对象。而且一天有86400000毫秒,所以我设置的最大日期允许在当前日期的前一天。这将在日历中禁用将来的日期。