How to get the date & ldquo; value & rdquo; from DatePicker?

advertisements

I don't know how to get the date value from Datepicker.

PickerDialog.java

package com.example.alecksjohanssen.newyorktimes;

/**
 * Created by AlecksJohanssen on 3/19/2016.
 */
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;

import java.util.Calendar;

public class PickerDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)

    {   DateSettings dateSettings = new DateSettings(getActivity());
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dialog;
        dialog = new DatePickerDialog(getActivity(),dateSettings,year,month,dayofmonth);
        return dialog;
    }
}
DateSetting.java
package com.example.alecksjohanssen.newyorktimes;

import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.Toast;

/**
 * Created by My name Is Hardline on 2/26/2016.
 */
public class DateSettings implements DatePickerDialog.OnDateSetListener {
    Context context;

    public DateSettings(Context context) {

        this.context = context;

    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthofyear,int dayofmonth) {
        Toast.makeText(context, "Selected date" + monthofyear + "/" + dayofmonth + "/" + year, Toast.LENGTH_LONG).show();
    }
}

  public void setDate() {
        PickerDialog pickerDialogs = new PickerDialog();
        pickerDialogs.show(getFragmentManager(), "date_picker");
    }

It's basically it, it's only shows that Selected date is . Still I'm looking for how to extract the date data so that I can put it on my Begin Date :P.


You can try like this

private void datePicker(){
            Calendar calendar = Calendar.getInstance();
            final  SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            DatePickerDialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                System.out.print(dateFormatter.format(newDate.getTime()));
            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
}