CallMyName
CallMyName

Reputation: 84

How to prevent users from setting the date and time that is before the current date and time

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!

Upvotes: 3

Views: 1253

Answers (3)

Kunal Shah
Kunal Shah

Reputation: 499

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 ");
      }

}

Upvotes: 0

Amit Thaper
Amit Thaper

Reputation: 2137

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.

  }

Upvotes: 2

Manoj Kumar
Manoj Kumar

Reputation: 1520

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();
                                    }

Upvotes: 1

Related Questions