Alecks Dang
Alecks Dang

Reputation: 67

How to get date "value" from DatePicker?

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.

Upvotes: 1

Views: 10508

Answers (2)

Andan H M
Andan H M

Reputation: 793

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();
}

Upvotes: 4

Ahmed Khamis
Ahmed Khamis

Reputation: 83

Say you have Calendar mCalendar that you want to init the date picker with it.

    dialog.getDatePicker().init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
        @Override
        public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) {
            mCalendar.set(i, i2, i3);
        }
    });

Then to get the picked date

mCalendar.getTime()

Upvotes: 1

Related Questions