0

I have a DatePicker in my app.I need to get the values of the date that are selected???

for example:

 dateDisplay=(EditText)findViewById(R.id.date);

As we do in normal edittext like:

final EditText Name = (EditText) this.findViewById(R.id.nametext); 
and we can cast the data of it by using `Name.getText().toString()`

So similarly how can we get the values of date picker to a string?

  • possible duplicate of [Casting and getting values from date picker and time picker in android](http://stackoverflow.com/questions/2592499/casting-and-getting-values-from-date-picker-and-time-picker-in-android) – naXa stands with Ukraine Dec 07 '14 at 22:52

2 Answers2

2

Check this:

Android: get DatePickerDialog in a fragment


Its easy:

DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year,
            int monthOfYear, int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};

DatePickerDialog d = new DatePickerDialog(getActivity(),
        R.style.MyThemee, mDateSetListener, mYear, mMonth, mDay);
d.show();

UpdateDisplay method:

private void updateDisplay() {

    GregorianCalendar c = new GregorianCalendar(mYear, mMonth, mDay);
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");

    editDate.setText(sdf.format(c.getTime()));

    sdf = new SimpleDateFormat("yyyy-MM-dd");

    transDateString=sdf.format(c.getTime());
}// updateDisplay
Community
  • 1
  • 1
moskis
  • 1,136
  • 10
  • 20
0

This will work like charm.

 final Calendar cal = Calendar.getInstance();
       pYear = cal.get(Calendar.YEAR);
       pMonth = cal.get(Calendar.MONTH);
       pDay = cal.get(Calendar.DAY_OF_MONTH);
//----------To Open datePicker----------------
private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener()
     {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                pYear = year;
                pMonth = monthOfYear;
                pDay = dayOfMonth;
                updateDisplay();
            }
     };

//----------To display date in EditText----------------
private void updateDisplay()
     {
           final Calendar cal = Calendar.getInstance();
           currentYear = cal.get(Calendar.YEAR);

           displayBirthdayDate.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(pMonth + 1).append("/")
                        .append(pDay).append("/")
                        .append(pYear));
     }
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65