我已经采取了日期选择器对话框。我想限制从当前日期到接下来的 3 个月的 select 日期
i have taken datepicker dialog. i want to restrict to select date from current date till its next 3 months
public void DateDialog() {
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
long timeInMilliseconds = 0;
String givenDateString = "Tue Jun 23 16:08:28 GMT+05:30 2016";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
view.setMaxDate(timeInMilliseconds);
datePicker.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
}
};
DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
dpDialog.show();
}
i have taken datepicker dialog. i want to restrict to select date from current date till its next 3 months
您可以为日期选择器设置 MaxDate 和 MinDate 限制
setMinDate(long minDate)
setMaxDate(long maxDate)
通过此获取您的当前日期:
Calendar calendar=Calendar.getInstance();
int mDay = calendar.get(Calendar.DAY_OF_MONTH), mMonth = calendar.get(Calendar.MONTH), mYear = calendar.get(Calendar.YEAR);
并检查是否有 select 数据,即 myMonth < mMonth+3
与否。
Calendar calendar = Calendar.getInstance();//get the current day
calendar.add(Calendar.MONTH,3);
long afterThreeMonthsinMilli=calendar.getTimeInMillis();
DatePickerDialog dpDialog = new DatePickerDialog(getActivity(), listener, mYear, mMonth, mDay);
dpDialog.getDatePicker().setMaxDate(afterThreeMonthsinMilli);
dpDialog.show();
我为我的一个项目做了同样的事情。我相信你和我一样在这里使用图书馆。这是我实现它的方法:
Calendar currentdate= Calendar.getInstance();
Calendar lastdate = Calendar.getInstance();
lastdate.set(currentdate.get(Calendar.YEAR), currentdate.get(Calendar.MONTH), currentdate.get(Calendar.DAY_OF_MONTH) + 90);
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(HomeActivity.this, currentdate.get(Calendar.YEAR), currentdate.get(Calendar.MONTH), currentdate.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setMinDate(currentdate);
datePickerDialog.setMaxDate(lastdate);
datePickerDialog.show(getFragmentManager(), "yourtag");
public void DateDialog() {
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
long timeInMilliseconds = 0;
String givenDateString = "Tue Jun 23 16:08:28 GMT+05:30 2016";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
view.setMaxDate(timeInMilliseconds);
datePicker.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
}
};
DatePickerDialog dpDialog = new DatePickerDialog(this, listener, year, month, day);
dpDialog.show();
}
i have taken datepicker dialog. i want to restrict to select date from current date till its next 3 months
您可以为日期选择器设置 MaxDate 和 MinDate 限制
setMinDate(long minDate)
setMaxDate(long maxDate)
通过此获取您的当前日期:
Calendar calendar=Calendar.getInstance();
int mDay = calendar.get(Calendar.DAY_OF_MONTH), mMonth = calendar.get(Calendar.MONTH), mYear = calendar.get(Calendar.YEAR);
并检查是否有 select 数据,即 myMonth < mMonth+3
与否。
Calendar calendar = Calendar.getInstance();//get the current day
calendar.add(Calendar.MONTH,3);
long afterThreeMonthsinMilli=calendar.getTimeInMillis();
DatePickerDialog dpDialog = new DatePickerDialog(getActivity(), listener, mYear, mMonth, mDay);
dpDialog.getDatePicker().setMaxDate(afterThreeMonthsinMilli);
dpDialog.show();
我为我的一个项目做了同样的事情。我相信你和我一样在这里使用图书馆。这是我实现它的方法:
Calendar currentdate= Calendar.getInstance();
Calendar lastdate = Calendar.getInstance();
lastdate.set(currentdate.get(Calendar.YEAR), currentdate.get(Calendar.MONTH), currentdate.get(Calendar.DAY_OF_MONTH) + 90);
DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(HomeActivity.this, currentdate.get(Calendar.YEAR), currentdate.get(Calendar.MONTH), currentdate.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setMinDate(currentdate);
datePickerDialog.setMaxDate(lastdate);
datePickerDialog.show(getFragmentManager(), "yourtag");