我有一个日期选择器和一个时间戳,如下所示。我试图根据当前日期和当前时间正确验证。对于时间,用户不能选择低于当前系统时间的时间,但我不知道如何将其与datepicker结合使用。
e.g。如果用户在将来选择一个日期,但是选择一个时间,假设18.00并且系统时间是19.00,则当前打印的Toast表示所选时间无效。这是因为我不知道如何检查过去设置或选择的日期。任何想法如何做到这一点?
这是我的时间选择器,目前上述案例失败
public void setTimeReminder() {
etSetTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get current time
Calendar mcurrentTime = Calendar.getInstance();
final int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
final int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(editTaskForm.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker,
int selectedHour, int selectedMinute) {
// select valid time check
if (selectedHour < hour || selectedHour == hour
&& selectedMinute < minute ) {
Toast.makeText(getApplicationContext(),
"Time is not valid",
Toast.LENGTH_LONG).show();
} else {
// Select and format Time correctly
if (selectedHour < 10) {
if (selectedMinute < 10) {
time = "0" + selectedHour + ":"
+ "0" + selectedMinute;
etSetTime.setText(time);
} else {
time = "0" + selectedHour + ":"
+ selectedMinute;
etSetTime.setText(time);
}
}
else {
if (selectedMinute < 10) {
time = selectedHour + ":" + "0"
+ selectedMinute;
etSetTime.setText(time);
} else {
time = selectedHour + ":"
+ selectedMinute;
etSetTime.setText(time);
}
}
}
}
}, hour, minute, true);// Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
}
和日期选择器选择器类
public class StartDatePicker extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
Calendar c = Calendar.getInstance();
int startYear = c.get(Calendar.YEAR);
int startMonth = c.get(Calendar.MONTH);
int startDay = c.get(Calendar.DAY_OF_MONTH);
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
// Use the current date as the default date in the picker
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this,
startYear, startMonth, startDay);
return dialog;
}
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendarPicked = Calendar.getInstance();
calendarPicked.set(Calendar.YEAR, year);
calendarPicked.set(Calendar.MONTH, monthOfYear);
calendarPicked.set(Calendar.DAY_OF_MONTH, dayOfMonth);
if (calendarPicked.getTimeInMillis() < System.currentTimeMillis()) {
DialogFragment dialogFragment = new StartDatePicker();
dialogFragment.show(getSupportFragmentManager(),
"start_date_picker");
Toast.makeText(getApplicationContext(), "Select Valid Date",
Toast.LENGTH_LONG).show();
} else {
// store date
// values used to load into location form if needed
ContentValues values = new ContentValues();
values.put("Day", dayOfMonth);
values.put("Month", monthOfYear);
values.put("Year", year);
// date to be displayed to user
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
// date to be stored in database
SimpleDateFormat actualDate = new SimpleDateFormat("dd/MM/yyyy");
c.set(year, monthOfYear, dayOfMonth);
// to be displayed to user
Date dateV = c.getTime();
// to be stored in database
Date actualD = c.getTime();
// displayed to user
String dateString = sdf.format(dateV);
// stored in database
actualDateStored = actualDate.format(actualD);
// storing the actual date in the database***
date = actualDateStored;
// display day and date to user
SimpleDateFormat outFormat = new SimpleDateFormat("EEE");
String dayShort = outFormat.format(dateV);
String dateOutput = dateString;
dateDisplayed = dayShort + " , " + dateOutput;
etSetDate.setText(dateDisplayed);
}
}
}