Aakash
Aakash

Reputation: 101

How to prevent the user selecting past date in Android date picker

Hi i am making an Appointment app i am using firebase database as my backend.

I want to restrict the user selecting the previous dates. I got the solution from here

But I don't know where to put the code to get it work. I am putting my code below please guide me for the same...

Another question is, if the user changes the system date and time the date in datepicker also changes is there any way that i can restrict user to select the current date or tomorrow's date only but not the yesterdays date.

for ex. taking ServerValue.TIMESTAMP and converting this into date and then comparing the selected date and date got from TIMESTAMP.

Appointment.java

/**
 * A simple {@link Fragment} subclass.
 */
public class Appointment extends Fragment {

    @BindView(R.id.apointFirstName)
    EditText apointFirstName;
    @BindView(R.id.apointLastName)
    EditText apointLastName;
    @BindView(R.id.apointPhoneNumber)
    EditText apointPhoneNumber;
    @BindView(R.id.apointEmail)
    EditText apointEmail;
    @BindView(R.id.apointDate)
    EditText apointDate;
    @BindView(R.id.availTimeSpinner)
    Spinner availTimeSpinner;
    @BindView(R.id.bookAppointmentButton)
    Button book_btn;
    @BindView(R.id.cancelAppointmentButton)
    Button cancel_btn;

    Calendar mCal;
    int day, month, year;
    String mCurrentDate;
    String fname,lname,email;
    int phone;
    String timestamp = String.valueOf(ServerValue.TIMESTAMP);

    public Appointment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_appointment, container, false);
        ButterKnife.bind(this,view);


        setCurrentDate();

        apointDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectDate();
            }
        });
        return view;
    }

    // set the current date on the text view
    private void setCurrentDate() {

        mCal = Calendar.getInstance();

        day = mCal.get(Calendar.DAY_OF_MONTH);
        month = mCal.get(Calendar.MONTH);
        year = mCal.get(Calendar.YEAR);

        month = month +1;

        mCurrentDate = (day+"/"+month+"/"+year);

    }

    //open the datepickerdialogue
    private void selectDate() {
        if(getActivity()!=null) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    month = month + 1;
                    apointDate.setText(dayOfMonth + "/" + month + "/" + year);
                }
            }, year, month, day);


            datePickerDialog.show();
        }
    }

}

Upvotes: 0

Views: 1850

Answers (2)

AskNilesh
AskNilesh

Reputation: 69689

Use datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); to disable paste date in date picker

Try this

private void selectDate() {
        if(getActivity()!=null) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    month = month + 1;
                    apointDate.setText(dayOfMonth + "/" + month + "/" + year);
                }
            }, year, month, day);

            datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
            datePickerDialog.show();
        }
    }

the user changes the system date and time the date in datepicker also changes is there any way that i can restrict user to select the current date or tomorrow's

Check this link

  1. Get Real Time - Not Device Set Time in android
  2. Android - Get current time without dependency on device's clock
  3. How to get device independent local date and time Android

Upvotes: 2

user4571931
user4571931

Reputation:

add below line user can not select old data..

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

Upvotes: 5

Related Questions