Search code examples
javaandroidandroid-timepicker

How to set minimum time to system current time from timepickerdialog


I'm using a time picker dialog to select time but I lately observed that when user selects time it allows user to select the time that is already passed. Example: If the current date is 25-03-2020 and time is 5:00PM but time picker allows to select time below 5:00PM.

So how can I alert user to not select passed time of current date.


Solution

  • Thanks for everyone's help. But somehow i solved the issue with the following approach so i guess that will help future researchers to get a solution.

    At first when user picks date from the date picker and sets date and selects time picker then it generates a date from simple date format and matches if the both dates are same. if yes then it starts check the time with another dateformat.

    here is the code of it. I included everything from date picker.

     (Button_name_for_date_picker).setOnClickListener(view -> {
                Calendar calendar1 = Calendar.getInstance();
                int year = calendar1.get(Calendar.YEAR);
                int month = calendar1.get(Calendar.MONTH);
                int day = calendar1.get(Calendar.DAY_OF_MONTH);
    
                DatePickerDialog dialog = new DatePickerDialog(OrderConfirmationActivity.this,
                        dateSetListener, year, month, day);
                dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
                dialog.getDatePicker().setMaxDate(System.currentTimeMillis() -1000 + (1000*60*60*24*7)); // This allows user to select a date range between a week
                dialog.show();
            });
            dateSetListener = (view, year, month, dayOfMonth) -> {
                month = month + 1;
                if (month < 10){
                    String single_month = "0" + month;
                    (String_name) = dayOfMonth + "/" + single_month + "/" + year;
                    (Button_name).setText(AppointmentDate);
                    Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
                }else{
                    AppointmentDate = dayOfMonth + "/" + month + "/" + year;
                    Appoint_date.setText(AppointmentDate);
                    Appoint_date.setTextColor(getResources().getColor(android.R.color.holo_green_light, getTheme()));
                }
    
            };
    
    
            //This Generates AppointmentTime
            Appoint_time.setOnClickListener(view -> {
                Calendar calendar12 = Calendar.getInstance();
                final int hour = calendar12.get(Calendar.HOUR_OF_DAY);
                int minute = calendar12.get(Calendar.MINUTE);
                TimePickerDialog timePickerDialog = new TimePickerDialog(OrderConfirmationActivity.this, (timePicker, selected_hour, selected_minute) -> {
                    SimpleDateFormat current_date = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
                    String check = current_date.format(new Date());
                    if (AppointmentDate.equals(check)){
                        SimpleDateFormat current_time = new SimpleDateFormat("HH", Locale.getDefault());
                        int check_hour = Integer.parseInt(current_time.format(new Date()));
                        String v = String.valueOf(future_time);
                        Toast.makeText(this, v, Toast.LENGTH_SHORT).show();
                        if (selected_hour < check_hour) {
                            future_time = 1;
                            String v1 = String.valueOf(future_time);
                            Toast.makeText(this, v1, Toast.LENGTH_SHORT).show();
                        }
                    }
    
                    if (selected_hour < 7){
                        time_check = 1;
                    }else if (selected_hour >= 19){
                        time_check = 1;
                    }
    
                    if (selected_hour > 12) {
                        AppointmentTime = (selected_hour - 12) + ":" + selected_minute + " " + "PM";
                        Appoint_time.setText(AppointmentTime);
                    } else if (selected_hour == 12) {
                        AppointmentTime = "12" + ":" + selected_minute + " " + "PM";
                        Appoint_time.setText(AppointmentTime);
                    } else {
                        if (selected_hour != 0) {
                            AppointmentTime = selected_hour + ":" + selected_minute + " " + "AM";
                            Appoint_time.setText(AppointmentTime);
                        } else {
                            AppointmentTime = "12" + ":" + selected_minute + " " + "AM";
                            Appoint_time.setText(AppointmentTime);
                        }
    
                    }
                }, hour, minute, false);
                timePickerDialog.show();
            });