title: How to return the date value from DatePickerDialog in Android?
body: <p>I am new to android . I ve created a Date picker in android using following guide .<a href="http://developer.android.com/guide/topics/ui/controls/pickers.html" rel="noreferrer">http://developer.android.com/guide/topics/ui/controls/pickers.html</a></p>
  
  <pre><code> public  class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {
  
  
  
  StringBuilder sb = new StringBuilder();
  public static String date;
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      // Use the current date as the default date in the picker
      final Calendar c = Calendar.getInstance();
      int year = c.get(Calendar.YEAR);
      int month = c.get(Calendar.MONTH);
      int day = c.get(Calendar.DAY_OF_MONTH);
  
     // Create a new instance of DatePickerDialog and return it
     return new DatePickerDialog(getActivity(), this, year, month, day);
     }
  
    public void onDateSet(DatePicker view, int year, int month, int day) {
     // Do something with the date chosen by the user
  
  
  sb.append(year);
  sb.append('-');
  sb.append(month+1);
  sb.append('-');
  sb.append(day);
  
  date = sb.toString();
  System.out.println("The date is "+date);
  
      }
  </code></pre>
  
  <p>I need to return this date value (date = sb.toString()) to my MainActivity   . Since the onDateSet method is void what should I do ?</p>
  
  <p>Additional Information - DatePickerDialog Triggers at the MainActivity class ,But not with single button click . There are several processes happens in side a single button , Date picker will triggers only when certain condition is met . I do not want to display the  date value either . Just want it returned for further processing. </p>
  
  <p>Appreciate any kind of guidance  </p>
  
  <p>Changed onDataset method and justshow()</p>
  
  <pre><code>public void onDateSet(DatePicker view, int year, int month, int day) {
  </code></pre>
  
  <p>// Do something with the date chosen by the user</p>
  
  <pre><code>    sb.append(year);
      sb.append('-');
      sb.append(month+1);
      sb.append('-');
      sb.append(day);
  
      date = sb.toString();
      MainActivity.newdate=sb.toString();
      System.out.println("The date is "+MainActivity.newdate);
  </code></pre>
  
  <p>}
          public void justShow(){</p>
  
  <pre><code>System.out.println("The date is "+MainActivity.newdate);
  </code></pre>
  
  <p>}</p>
  
  <p>This is the relevant Part From Main(After making changes suggested in first reply )</p>
  
  <pre><code>    DateToken mydate=new DateToken();
      String test=dayvals.get(0);
      DialogFragment df=new DatePickerFragment();
  
      if(test.equalsIgnoreCase("day")){
  
  
          df.show(getSupportFragmentManager(), "DatePik");
  
      }
  
      System.out.println("Date is on Main"+newdate);
  
  
    DatePickerFragment dpf=new DatePickerFragment();
    dpf.justShow();
  </code></pre>
  
  <p>newdate is the static String , but still outputs null. In both MainActivity and justShow methods . But in onDataSet method date outputs correctly </p>