I am encountering a problem for my application. I am trying to do something to prevent the user from selecting the date and time that has already past. If the user selects date and the time that has past, an error message will be displayed. The user will be selecting the date and time from the datePicker and timePicker. Anyone have any idea on doing it? Any help will be greatly appreciated. Thanks a lot!
Asked
Active
Viewed 1,230 times
3 Answers
2
You can prevent user to change date time through the broadcast reciever ACTION_TIME_CHANGED. If user change the date time then broadcast reciever will called. Override the onRecieve method in the broadcast reciever. Please make sure to define broadcast reciever in manifest file
@Override
public void onReceive(Context context, Intent intent) {
// You can put conditions the changed date time is not past from the current time and show toast message.
}

Amit Thaper
- 2,117
- 4
- 26
- 49
1
If you're setting the picker value in edittext, you can do this:
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
DateFormat df = new SimpleDateFormat(
"dd/MM/yyyy");
Date day_entered, day_valid;
day_entered = df.parse(your_date_edittext.getText()
.toString());
day_valid = df.parse(df.format(cal.getTime()));
if (day_entered.after(day_valid)) {
Toast msg = Toast
.makeText(
Profile.this,
"Please Enter a valid date",
Toast.LENGTH_LONG);
msg.show();
}

Manoj Kumar
- 1,510
- 3
- 20
- 40
-
Hi, eh what do you mean by setting the picker value in edittext? I set the values directly from the pickers. – CallMyName Sep 12 '12 at 07:01
-
Are you setting the values getting from the picker somewhere like a edittext,textview or something? – Manoj Kumar Sep 12 '12 at 07:04
-
Oh yes, I pass the values from the pickers to an textview. – CallMyName Sep 12 '12 at 07:06
-
then in my code you pass that value in my code instead of `your_date_edittext.getText().toString());` or you can simply use `setmindate()` function :P – Manoj Kumar Sep 12 '12 at 07:10
-
please see [this](http://stackoverflow.com/questions/4943486/android-datepicker-date-limiting) – Manoj Kumar Sep 12 '12 at 07:43
-
Hi, then if I wish to check the time that the user select is not before the current time, how should I do it? – CallMyName Sep 12 '12 at 07:47
-
Just change the before function with after function for your scenario in that example – Manoj Kumar Sep 12 '12 at 07:50
0
if(fromdate!=null && fromdate.length()>0 && todate!=null && todate.length()>0)
{
try
{
sdf = new SimpleDateFormat("yyyy-mm-dd");
date1 = sdf.parse(fromdate);
date2 = sdf.parse(todate);
Calendar cal = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal.setTime(date1);
cal2.setTime(date2);
if(cal.after(cal2)){
System.out.println("Date1 is after Date2");
// Toast.makeText(getApplicationContext(), "from Date1 is after Date2", Toast.LENGTH_SHORT).show();
// clear the dates here........
}
}
catch(Exception e)
{
Log.d("pavan","in exception block ");
}
}

Kunal Shah
- 489
- 1
- 4
- 21