Set minimum year (use to select DOB) for Datepicker dialog

Shawn.Y

I have create a function for my code which set a rule to restrict user to select the day earlier then systems date from date picker dialog

protected void setMinMax() {
    Calendar minAllowedDate = CommUtils.getMinAllowedDate();
    minAllowedDate.add(Calendar.MILLISECOND, -1000);
      datePickerDialog.getDatePicker().setMinDate(minAllowedDate.getTimeInMillis());

now i want to create another date picker for user to select DOB, what i want to do is to set a rules that user DOB should be on age 4-150 which means the YEAR that user can select should be -4 & -150 from systems date. If using the the code above, what should i put in

minAllowedDate.add(Calendar.MILLISECOND, -1000);

i had tried with below code which give me the year 150 as min option

minAllowedDate.add(Calendar.YEAR, -150);
Shawn.Y

Found out that most of the solution using the method .add(Calendar.YEAR,-4), however this also create limit to the month & date to be select. For example, When the current date is 11/Jun/2018, running that line of code you can have 4 years before(2018-4=2014) but also limit in your month + day which you can only select date before 11/Jun/2018.

After several tried, i had came out the solution which manually set the day & month to 31/Dec so that i can have an option range from 31/12/2014 to 1/1/2014

    Calendar calStart = new GregorianCalendar();
    calStart.setTime(new Date());
    calStart.set(Calendar.MONTH, 11);
    calStart.set(Calendar.DAY_OF_MONTH, 31);
    calStart.add(Calendar.YEAR, -150);

    Calendar calEnd = new GregorianCalendar();
    calEnd.setTime(new Date());
    calEnd.set(Calendar.MONTH, 11);
    calEnd.set(Calendar.DAY_OF_MONTH, 31);
    calEnd.add(Calendar.YEAR,-4);
    datePickerDialog.getDatePicker().setMinDate(calStart.getTimeInMillis());
    datePickerDialog.getDatePicker().setMaxDate(calEnd.getTimeInMillis());

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to set default display as year wise in Android Dialog datepicker

Jquery datepicker, select year

How to select the year FIRST in DatePicker

Set year of datepicker using jquery

Conditionally change year in DOB

Rails string to DOB year

How to select only year on mat-datepicker

Datepicker automatically hide on month or year select

How to have datepicker set default year?

How set maximum date in datepicker dialog in android?

how to set datepicker dialog in android to yesterday date

How to set colors for DatePicker dialog in Xamarin?

How to change Default year(1900) of datepicker Dialog in android?

Use the current value of a DatePicker as the minimum/maximum value of another DatePicker

How to set datepicker on select change?

I want to use digital datepicker dialog and timepicker dialog

How to use a property set in a dialog

Can't Set a Converted Date from String as Minimum Date in DatePicker

Swap month/year select list placement in jQuery UI Datepicker

Select Day or Month or Year in Single Calendar or DatePicker (Flexible Selection Mode)

jquery UI Datepicker fails to update month or year -- must select date

Set datepicker for one year from today in Cypress test

jQuery UI: Datepicker set year range dropdown to 100 years

How to set a default language on select language dialog?

Java OOP - How to get year in DOB and current year then calculate age?

Here I had Use a material datepicker, but it is not showing the current year in calendar

I have two datepicker from and to i want to Set next year into to datepicker

How to set css class for Rails date_select year select?

datePicker with maximum year current year

TOP Ranking

HotTag

Archive