如何禁用当前日期之前的日期?
how to disable dates till current date?
如何在 android
中禁用日历中的日期到当前日期
editText_Date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Get Current Date
final Calendar c = Calendar.getInstance();
Date dates = c.getTime();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mDayName = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(dates.getTime());
System.out.println("mDayName = "+mDayName);
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
上面的代码禁用当前日期的过去日期。我想禁用当前日期之前的日期。需要帮助。
解决方案:您将使用日期选择器并设置如下代码
DatePicker datePicker = date.getDatePicker();
datePicker.setMinDate(d.getTime());
试试这个
datePicker.setMinDate(System.currentTimeMillis() - 1000);
使用这个库来选择日期和时间
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
可在此处找到文档:
在这里你可以限制时间选择器或日期选择器
试试这个。
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
Calendar mcurrentDate = Calendar.getInstance();
mcurrentDate.add(Calendar.DATE, 1);
datePickerDialog.getDatePicker().setMinDate(mcurrentDate.getTimeInMillis());
datePickerDialog.show();
试试这个:
editText_Date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Get Current Date
final Calendar c = Calendar.getInstance();
Calendar minimum = Calendar.getInstance();
minimum.set(Calendar.HOUR,0);
minimum.set(Calendar.MINUTE,0);
minimum.set(Calendar.SECOND,0);
minimum.add(Calendar.DAY_OF_YEAR,1);
Date dates = c.getTime();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mDayName = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(dates.getTime());
System.out.println("mDayName = "+mDayName);
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(minimum.getTimeInMillis());
datePickerDialog.show();
}
});
只需复制整个代码,今天就可以拯救您。
致谢 Dimitri Borohhov
如何在 android
中禁用日历中的日期到当前日期editText_Date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Get Current Date
final Calendar c = Calendar.getInstance();
Date dates = c.getTime();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mDayName = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(dates.getTime());
System.out.println("mDayName = "+mDayName);
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
});
上面的代码禁用当前日期的过去日期。我想禁用当前日期之前的日期。需要帮助。
解决方案:您将使用日期选择器并设置如下代码
DatePicker datePicker = date.getDatePicker();
datePicker.setMinDate(d.getTime());
试试这个
datePicker.setMinDate(System.currentTimeMillis() - 1000);
使用这个库来选择日期和时间
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
可在此处找到文档:
在这里你可以限制时间选择器或日期选择器
试试这个。
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
Calendar mcurrentDate = Calendar.getInstance();
mcurrentDate.add(Calendar.DATE, 1);
datePickerDialog.getDatePicker().setMinDate(mcurrentDate.getTimeInMillis());
datePickerDialog.show();
试试这个:
editText_Date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Get Current Date
final Calendar c = Calendar.getInstance();
Calendar minimum = Calendar.getInstance();
minimum.set(Calendar.HOUR,0);
minimum.set(Calendar.MINUTE,0);
minimum.set(Calendar.SECOND,0);
minimum.add(Calendar.DAY_OF_YEAR,1);
Date dates = c.getTime();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mDayName = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(dates.getTime());
System.out.println("mDayName = "+mDayName);
DatePickerDialog datePickerDialog = new DatePickerDialog(Create_slots.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//editText_Date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year); //'dd-mm-yyy'
editText_Date.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth); //'yyy-mm-dd'
date1 = editText_Date.getText().toString();
System.out.println("date1 = "+date1);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(minimum.getTimeInMillis());
datePickerDialog.show();
}
});
只需复制整个代码,今天就可以拯救您。 致谢 Dimitri Borohhov