Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android datepicker set max date as 14 years ago

I am using this coded to generate date picker in android.

DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener, y, m, d);
datePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

Right now max date is current date. I want to set it as 14 years ago. How can I do that?

like image 390
Ronak Patel Avatar asked May 22 '15 16:05

Ronak Patel


People also ask

What is minDate and maxDate in DatePicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.

What is DatePicker and Timepicker in Android?

Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs. Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or date (month, day, year).

What is the use of DatePicker in Android?

Android DatePicker is a user interface control that is used to select the date by day, month, and year in the android application. DatePicker is used to ensure that the users will select a valid date.


2 Answers

You can do this by using Calender

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -14);
DatePickerDialog datePicker = new DatePickerDialog(this, datePickerListener, y, m, d);
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
like image 138
Anirban Das Avatar answered Oct 20 '22 03:10

Anirban Das


One approach would be by using JodaTime:

datePicker.getDatePicker().setMaxDate(new DateTime().minusYears(14));
like image 27
muilpp Avatar answered Oct 20 '22 05:10

muilpp