How do I get the Calendar date on Android?

Asked

Viewed 450 times

-1

I got it inside the 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 out:

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

And this log returns to me: print

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

  • You want the full date (Date object) or the day?

  • I would like dd/mm/YYYY

1 answer

2


It would be interesting to first format the received date. For this you can use the class SimpleDateFormat using the method getTime 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);
}
  • All right, I’ll try

  • Error: "Process: com.example.Gustavo.domanda, PID: 32438 java.lang.Noclassdeffounderror: Failed Resolution of: Landroid/icu/text/Simpledateformat; at com.example.Gustavo.domanda.Agendamentoactivity.onCreate(Agendamentoactivity.java:138)"

  • Is the first part of the code inside or outside the onCreate? Because I put it inside.

  • I already adapted to your code in the last part. What you don’t understand?!

  • Ta, it worked... the import I was doing was that it was wrong. I was with this: import android.icu.text.Simpledateformat; this was right: import java.text.Simpledateformat;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.