Midhun
Midhun

Reputation: 178

My current date not showing in edittext

this is my simple code to get current date and display in a text filed.but the current date is not showing i could not find any error.Please help me out geeks

  month = calendar.get(Calendar.MONTH);
                            day = calendar.get(Calendar.DAY_OF_MONTH);
                            year1 = calendar.get(Calendar.YEAR);
                            Log.d("8999",month+" "+day+" "+year1);
                            DatePickerDialog datePickerDialog = new DatePickerDialog(Reminder.this,
                                    new DatePickerDialog.OnDateSetListener() {
                                        @Override
                                        public void onDateSet(DatePicker view, int 

        year, int monthOfYear, int dayOfMonth) {

                                                et_cal.setText("" + dayOfMonth + " - " + monthOfYear+1 + " - " + year);
                                                Toast.makeText(Reminder.this, "" + dayOfMonth + " - " + monthOfYear + " - " + year, Toast.LENGTH_SHORT).show();
                                            }
                                        },year1,month,day);
                                datePickerDialog.show();

Upvotes: 0

Views: 257

Answers (2)

Arti
Arti

Reputation: 970

Add one line in code If current date is display wrong

month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    year1 = calendar.get(Calendar.YEAR);
    Log.d("8999",month+" "+day+" "+year1);
    DatePickerDialog datePickerDialog = new DatePickerDialog(SearchViewActivity.this,
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int
                        year, int monthOfYear, int dayOfMonth) {

                    int curr_month = monthOfYear+1;

                    et_cal.setText("" + dayOfMonth + " - " + curr_month+" - " + year);
                    Toast.makeText(SearchViewActivity.this, "" + dayOfMonth + " - " + curr_month + " - " + year, Toast.LENGTH_SHORT).show();
                }
            },year1,month,day);
    datePickerDialog.show();

If date not showing then please provide your XML layout file then i elaborate it.

Upvotes: 1

AndroidDev
AndroidDev

Reputation: 21237

Your DatePickerDialog doesn't appear to actually be doing anything. You are not setting any of your Calendar fields with the date that the user selected as the date. You are simply outputting the Calendar fields as they existed prior to your DatePickerDialog listener firing.

I would do it like this:

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

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            et_cal.setText("" + dayOfMonth + " - " + monthOfYear+1 + " - " + year);
        }
    };

Upvotes: 0

Related Questions