VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-disable-previous-or-future-dates-in-datepicker-in-android/

⇱ How to Disable Previous or Future Dates in Datepicker in Android? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Disable Previous or Future Dates in Datepicker in Android?

Last Updated : 23 Jul, 2025

In this article, we are going to disable previous or future dates in Datepicker. Most of the time when we use DatePicker in Android we see that all the date in that is enabled. We can select any of the dates. But here we are going to see how to disable past or future dates. Disabling the Past Date can be useful when we are assigning tasks to someone. Then the selected date must be in the future. Disabling the Previous Date can be useful when we are asking for the date of birth for someone. Then the selected date must be in past. Let's implement this feature. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. 

For Max Date:

Get the DatePicker from DatePickerDialog with getDatePicker(). Set the max date to the current date with setMaxDate()

Note: Requires API level 11.

datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());

Below is the code for the MainActivity.java file.  

For min Date:

Similarly, Get the DatePicker from DatePickerDialog with getDatePicker(). Set the min date to the current date with setMinDate():    

datePicker.getDatePicker().setMinDate(calendar.getTimeInMillis());

Below is the code for the MainActivity.java file.  

Output: 


 

Comment
Article Tags:

Explore