androiddatepickerandroid-timepicker

Time validation for android timePickerDialog


I want to disable future timing for the below code. Here I want to give notification if users selects future timings. But, Here date validation is working fine. Only the problem with time selection.

if (fired) {
            fired = false;
            if (date.after(currentDate)) {
                new DoToast(mContext, getResources().getString(
                        R.string.future_date));

            } else {
                TimePickerDialog mTimePicker = new TimePickerDialog(mContext,
                        new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker timePicker,
                                    int selectedHour, int selectedMinute) {

                                String Am_Pm = "";

                                hour = selectedHour;
                                minute = selectedMinute;

                                Calendar datetime = Calendar.getInstance();
                                datetime.set(Calendar.HOUR_OF_DAY, hour);
                                datetime.set(Calendar.MINUTE, minute);

                                if (datetime.get(Calendar.AM_PM) == Calendar.AM)
                                    Am_Pm = "AM";
                                else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
                                    Am_Pm = "PM";
                                String minuteText = "" + minute;
                                if (minuteText.length() < 2) {
                                    minuteText = "0" + minuteText;
                                }
                                if (hour > 12)
                                    hour = hour - 12;

                                date_time.setText(new StringBuilder()
                                        .append(month + 1).append("-")
                                        .append(day).append("-").append(year)
                                        .append(" ")
                                        + "  "
                                        + hour
                                        + ": "
                                        + minuteText
                                        + " "
                                        + Am_Pm);
                            }
                        }, hour, minute, false);
                mTimePicker.setTitle("Select Time");
                hour = c.get(Calendar.HOUR_OF_DAY);
                minute = c.get(Calendar.MINUTE);
                mTimePicker.updateTime(hour, minute);

                mTimePicker.show();
            }
        }

Solution

  • Your validation needs to be triggered from inside onTimeSet, otherwise it will be set before it is checked.

    The following code can be called anywhere in your activity and it should work . I've updated your date_time string method too to a more elegant one. I assume date_time and c are established and accessible.

    TimePickerDialog mTimePicker = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker timePicker,int selectedHour,int selectedMinute) {
                    Calendar temp=Calendar.getInstance();
                    temp.set(Calendar.HOUR_OF_DAY,selectedHour);
                    temp.set(Calendar.MINUTE,selectedMinute);
    
                    if(temp.after(GregorianCalendar.getInstance())){
                        Toast.makeText(mContext.this, "Cannot select a future time", Toast.LENGTH_SHORT).show();
                    } else {
                        Calendar datetime=Calendar.getInstance();
                        datetime.set(Calendar.HOUR_OF_DAY,selectedHour);
                        datetime.set(Calendar.MINUTE,selectedMinute);
                        SimpleDateFormat mSDF=new SimpleDateFormat("MM-dd-yyyy hh: mm a");
                        date_time.setText(mSDF.format(datetime.getTime()));  // make sure this is accessible
                    }
                }
            }, sCal.get(Calendar.HOUR_OF_DAY), sCal.get(Calendar.MINUTE), false);
    // Ensure that c is established
    mTimePicker.updateTime(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
    mTimePicker.show();