title: how to get the values in DatePicker in java Android
body: <p>I want the user to be able to select the date of birth using the custom DatePicker from XML, the issue is that I don't know how to get the values selected and store it as a LocalDate variable because the backend stored it as a LocalDate. Firstly I customized it with DatePickerDialog but the thing is so frustrating, just imagine a user that was born in 1990, the person needs to scroll for a while before reaching that and I don't want to use that again as I came across the custom DatePicker which makes me to select the year, month and day differently. Hope you guys understands what I want to achieved. Here is the DatePicker I used in the XML to get the calendar. I want to get all the values selected and store it as a variable of type LocalDate so that I can use it in the user's registration because date of birth is one of the parameters required.</p>
  <pre><code>    &lt;DatePicker
                                  android:id=&quot;@+id/datePicker&quot;
                                  android:layout_width=&quot;wrap_content&quot;
                                  android:layout_height=&quot;wrap_content&quot;
                                  android:calendarViewShown=&quot;false&quot;
                                  android:datePickerMode=&quot;spinner&quot;
                                  android:spinnersShown=&quot;true&quot;/&gt;
  
  
  This is the one I used before but I don't want to use it again. It's frustrating to scroll down for a longer time if the person that want to register is a bit advanced in age.
  </code></pre>
  <p>this was the XML for it</p>
  <p>&lt;TextView</p>
  <pre><code>                    android:layout_width=&quot;wrap_content&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:gravity=&quot;center_horizontal&quot;
                      android:text=&quot;DATE OF BIRTH&quot;
                      android:textAlignment=&quot;center&quot;
                      android:textAppearance=&quot;@style/Base.TextAppearance.AppCompat.Caption&quot;
                      android:textColor=&quot;@color/grey_40&quot; /&gt;
  
  
  
                  &lt;LinearLayout
                      android:layout_width=&quot;match_parent&quot;
                      android:layout_height=&quot;wrap_content&quot;
                      android:orientation=&quot;horizontal&quot;&gt;
  
                      &lt;Button
                          android:id=&quot;@+id/btn_date_of_birth&quot;
                          style=&quot;@style/Base.Widget.AppCompat.Spinner.Underlined&quot;
                          android:layout_width=&quot;0dp&quot;
                          android:layout_height=&quot;wrap_content&quot;
                          android:layout_weight=&quot;2&quot;
                          android:text=&quot;select here&quot;
                          android:textAppearance=&quot;@style/TextAppearance.AppCompat.Medium&quot;
                          android:textColor=&quot;@color/grey_40&quot; /&gt;
  </code></pre>
  <p>And this was the method in the Java class</p>
  <p>private Button btnDateOfBirth;</p>
  <pre><code>    btnDateOfBirth = findViewById(R.id.btn_date_of_birth);
  
  
  
  private void setDatePicker(final Button btnDateOfBirth){
          Calendar current_calendar = Calendar.getInstance();
          DatePickerDialog dialog = DatePickerDialog.newInstance((view, year, monthOfYear, dayOfMonth) -&gt; {
  
              Calendar calendar = Calendar.getInstance();
              calendar.set(Calendar.YEAR,year);
              calendar.set(Calendar.MONTH,monthOfYear);
              calendar.set(Calendar.DATE,dayOfMonth);
              long date_millis = calendar.getTimeInMillis();
              btnDateOfBirth.setText(getFormattedDateEvent(date_millis));
  
          },
                  current_calendar.get(Calendar.YEAR),
                  current_calendar.get(Calendar.MONTH),
                  current_calendar.get(Calendar.DATE));
          dialog.setThemeDark(false);
          dialog.setAccentColor(getResources().getColor(R.color.cyan_800_overlay));
          //dialog.setMinDate(current_calendar);
          dialog.show(getFragmentManager(),&quot;Date PickerDialog&quot;);
      }
      public static String getFormattedDateEvent(Long dateTime){
          SimpleDateFormat newFormat = new SimpleDateFormat(&quot;EEE, yyyy MMM dd&quot;);
          return newFormat.format(new Date(dateTime));
      }
  Then used a button to get and store the date selected by setting onclick to it and called the method inside the onclick
  
  btnDateOfBirth.setOnClickListener(view -&gt; {
              setDatePicker((Button)view);
          });
  </code></pre>