I use a DatePicker
widget in Android for the user to set a date, and want to get the date value when a confirm button is clicked, how can I do that?
And to get current Date, use method getInstance() of Calendar class. Calendar cal = Calendar. getInstance(); mYear = cal.
Then, it's just: datePicker. getDate() . As if it had always existed.
If the Controls task pane is not visible, click More Controls on the Insert menu, or press ALT+I, C. Under Insert controls, click Date Picker. In the Date Picker Binding dialog box, select the field in which you want to store the date picker data, and then click OK.
Try this:
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1); int day = datePicker.getDayOfMonth(); int month = datePicker.getMonth() + 1; int year = datePicker.getYear();
I manged to set the MinDate & the MaxDate programmatically like this :
final Calendar c = Calendar.getInstance(); int maxYear = c.get(Calendar.YEAR) - 20; // this year ( 2011 ) - 20 = 1991 int maxMonth = c.get(Calendar.MONTH); int maxDay = c.get(Calendar.DAY_OF_MONTH); int minYear = 1960; int minMonth = 0; // january int minDay = 25; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_account); BirthDateDP = (DatePicker) findViewById(R.id.create_account_BirthDate_DatePicker); BirthDateDP.init(maxYear - 10, maxMonth, maxDay, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if (year < minYear) view.updateDate(minYear, minMonth, minDay); if (monthOfYear < minMonth && year == minYear) view.updateDate(minYear, minMonth, minDay); if (dayOfMonth < minDay && year == minYear && monthOfYear == minMonth) view.updateDate(minYear, minMonth, minDay); if (year > maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (monthOfYear > maxMonth && year == maxYear) view.updateDate(maxYear, maxMonth, maxDay); if (dayOfMonth > maxDay && year == maxYear && monthOfYear == maxMonth) view.updateDate(maxYear, maxMonth, maxDay); }}); // BirthDateDP.init() } // activity
it works fine for me, enjoy :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With