在Android应用中集成https://github.com/wdullaer/MaterialDateTimePicker此库,这是我选择日期和时间的代码。我能够在月内实现启用未来日期并在月中禁用过去日期但我想要实现过去几个月的禁用选项,并为将来几个月启用选择选项。我的代码是
private void selectDate() {
final TimePickerDialog timePickerDialog12h = TimePickerDialog
.newInstance(new OnTimeSetListener() {
@Override
public void onTimeSet(RadialPickerLayout view,
int hourOfDay, int minute) {
Object c = pad3(hourOfDay);
selectedTime = new StringBuilder()
.append(pad2(hourOfDay)).append(":")
.append(pad(minute)).append(c);
String str_date = selectedDate.toString() + " "
+ selectedTime + "";
String str_date_current = currentDate.toString() + " "
+ mCalendar.get(Calendar.HOUR_OF_DAY) + ":"
+ mCalendar.get(Calendar.MINUTE);
DateFormat formatter = new SimpleDateFormat(
"dd-MM-yyyy hh:mm aa");
DateFormat formatter2 = new SimpleDateFormat(
"dd-MM-yyyy hh:mm");
DateFormat myFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
String reformattedStr = myFormat.format(formatter
.parse(str_date));
// Date date=formatter.parse(str_date);
String reformattedStr2 = myFormat.format(formatter2
.parse(str_date_current));
date = (Date) myFormat.parse(reformattedStr);
Date date2 = (Date) myFormat.parse(reformattedStr2);
if (date2.before(date)) {
bookAppointment();
} else {
selectDate();
Toast.makeText(
ChatThread.this,
"We all wish we could go back in the past ;)",
Toast.LENGTH_SHORT).show();
}
System.out.println("Today is " + date.getTime());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar
.get(Calendar.MINUTE), false);
datePickerDialog = DatePickerDialog.newInstance(
new OnDateSetListener() {
public void onDateSet(DatePickerDialog datePickerDialog,
int year, int month, int day) {
selectedDate = new StringBuilder().append(pad(day))
.append("-")
.append(mCalendar.get(Calendar.MONTH) + 1)
.append("-")
.append(mCalendar.get(Calendar.YEAR));
currentDate = new StringBuilder()
.append(mCalendar.get(Calendar.DAY_OF_MONTH))
.append("-").append(pad(month + 1)).append("-")
.append(pad(year));
DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
try {
Date date1 = format.parse(selectedDate.toString());
Date date2 = format.parse(currentDate.toString());
Calendar cal = Calendar.getInstance();
cal.setTime(date2);
datePickerDialog.setMinDate(cal);
if (date2.equals(date1) || date2.compareTo(date1)<0) {
timePickerDialog12h.show(getFragmentManager(),
"");
} else {
selectDate();
Toast.makeText(
ChatThread.this,
"We all wish we could go back in the past ;)",
Toast.LENGTH_SHORT).show();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
mCalendar.get(Calendar.DAY_OF_MONTH));
// datePickerDialog.onCreateDialog(savedInstanceState)
datePickerDialog.show(getFragmentManager(), "tag");
}
答案 0 :(得分:0)
要在materialdatetimepicker中实现此目的,您应该设置最小日期
Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR);
cal.get(Calendar.MONTH);
cal.get(Calendar.DAY_OF_MONTH);
dpd.setMinDate(cal);
所以DatePickerDialog就像下面的代码
DatePickerDialog dpd = DatePickerDialog.newInstance(
AddTask.this,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(true);
dpd.vibrate(true);
dpd.dismissOnPause(true);
dpd.showYearPickerFirst(false);
Calendar cal = Calendar.getInstance();
cal.get(Calendar.YEAR);
cal.get(Calendar.MONTH);
cal.get(Calendar.DAY_OF_MONTH);
dpd.setMinDate(cal);
dpd.setAccentColor(getResources().getColor(R.color.dark_orange));
dpd.setTitle("Date Picker");
dpd.show(getFragmentManager(), "Date Picker");
答案 1 :(得分:0)
您可以使用此方法,并且您将获得您正在寻找的内容
public void dateSet() {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
selectedtime = selectedtime + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
selectedtimetosend = selectedtime;
selecteddate = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
}
}, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
dpd.setMinDate(now);
try {
final Activity activity = (Activity) getContext();
dpd.show(activity.getFragmentManager(), "Datepickerdialog");
} catch (Exception e) {
Toast.makeText(getContext(), "" + e.toString(), Toast.LENGTH_LONG).show();
}
}
复制并粘贴并查看结果 我在Fragment中使用它。如果你想在活动中改变它。 随意询问