Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the date from the DatePicker widget in Android?

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?

like image 643
David Avatar asked Jun 21 '11 07:06

David


People also ask

How can we get the current date using Datepicker in android?

And to get current Date, use method getInstance() of Calendar class. Calendar cal = Calendar. getInstance(); mYear = cal.

Which method can be used to get the current day from a Datepicker object in android?

Then, it's just: datePicker. getDate() . As if it had always existed.

How do I add date and time picker?

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.


2 Answers

Try this:

 DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);  int day = datePicker.getDayOfMonth();  int month = datePicker.getMonth() + 1;  int year = datePicker.getYear(); 
like image 50
Onuray Sahin Avatar answered Sep 19 '22 10:09

Onuray Sahin


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 :)

like image 45
Abdo Belk Avatar answered Sep 22 '22 10:09

Abdo Belk