Reputation: 1628
I have a DatePicker in my activity. I want to set the year max limit to 2000, so that datepicker dont show years which is exceed year 2000. how can i implement this.currently i have used this code to set current date :
dialog.getDatePicker().setMaxDate(new Date().getTime());
current work img :
I want look this :
this is my code :
protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
// start changes...
DatePickerDialog dialog = new DatePickerDialog(this,
mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
// end changes...
}
return null;
}
Upvotes: 0
Views: 1064
Reputation: 685
use this piece of code
protected Dialog onCreateDialog(int id) {
Date d=new Date();
Calendar cal=Calendar.getInstance();
cal.set(2000, 1, 1, 0, 0);
d.setTime(cal.getTimeInMillis());
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
switch (id) {
case DATE_DIALOG_ID:
// start changes...
DatePickerDialog dialog = new DatePickerDialog(this,
mDateSetListener, cyear, cmonth, cday);
dialog.getDatePicker().setMaxDate(d.getTime());
return dialog;
// end changes...
}
return null;
}
Upvotes: 3
Reputation: 3975
You set current date that's why you are seeing 2014.
You can set bounds of your DatePicker by:
setMinDate(long minDate)
setMaxDate(long maxDate)
How you are going to set the values is explained by MH. in the topic below in details:
Set Limit on the DatePickerDialog in Android?
Upvotes: 1