How do I get the Calendar date on Android?

-1

I have this within onCreate :

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel(myCalendar);

        }

    };

    editText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(AgendamentoActivity.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();

        }
    });

And that's it:

private void updateLabel(Calendar myCalendar) {
    String dia = myCalendar.toString();
    Log.d("TAG", "DATA " + dia);
}

And this Log returns me:

How do I get the date selected from within the day variable?

    
asked by anonymous 22.08.2017 / 21:44

1 answer

2

It would be interesting to format the received date first. For this you can use the SimpleDateFormat class using the getTime method of your calendar. See:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String time = sdf.format(myCalendar.getTime());

Adapting to your code, your method should look like this below:

private void updateLabel(Calendar myCalendar) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String dia = sdf.format(myCalendar.getTime());
    Log.d("TAG", "DATA " + dia);
}
    
22.08.2017 / 21:55