-4

In my application i have to show dates 5 years back dates from current date like example today is 4th July 2016 but in my date picker it will show as 4th July 2011 and user can not choose date after 4th July 2011 but can choose before 4th July 2011 and backwards.

I have seen some examples like Click here

 DatePickerDialog mDate = new DatePickerDialog(DatePicker.this, date, year, month, day);
            maxDate = Calendar.getInstance();
            maxDate.add(Calendar.YEAR,  -5);
            mDate.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
            mDate.show();

I have tried this and its working perfectly from January 1900 to July 4 2011 but one thing i want that is when i click the date in calendar it shows January 1900 all i want is when i click it it should show from July 2011 and then backward dates.

Community
  • 1
  • 1

1 Answers1

0

Solution-1

You can just set :

long years4 = 126144000; //4 years in seconds
long years_millis4 = years4*1000; //4 years in milliseconds, which is required format in date picker

dateDialog.getDatePicker().setMaxDate((new Date().getTime())-years_millis4);

where dateDialog is a

new DatePickerDialog()

and the param type to set for MaxDate and MinDate is a long

Hope this works for your case.

Apart from this you can add listeners to your date picker using which you can prompt user in case he chooses any date other than the required ones.

Solution 2

You can implement your custom date picker using spinners and custom dates as the list in the spinners. Apply proper conversions while getting the input.

Make sure that java.util.Date.getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • Thank you for your answer but when the date picker opens its shows dates from January 1900 but i want when i open date picker it should show from 2011, 5 years before the current year and user can go by clicking backward dates. – Supragyan Mandal Jul 04 '16 at 13:19
  • Just like max, you can put minimum limit also, and also u can set the initial values by using the same method – Mohammed Atif Jul 04 '16 at 13:23