Saleh923
Saleh923

Reputation: 105

reading Calendar date

I am using this method to read the date from the calendar and display it on the TextView but it only show me 25:10:2015 and it doesn't change when I choose another date. I wrote event to change the date but i don't know what is wrong: package com.example.user.calendar;

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView datee=(TextView)findViewById(R.id.textView2);
        final Calendar c=Calendar.getInstance();
        final int year =c.get(Calendar.YEAR);
        final int month =c.get(Calendar.MONTH);
        final int day =c.get(Calendar.DAY_OF_MONTH);
        datee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerDialog datepicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        datee.setText(" ");
                        datee.setText(year + ":" + month + ":" + day);
                       // dayyy=datee.getText().toString();
                    }


                }, year, month, day);
                datepicker.setTitle("select date");
                datepicker.show();

            }
        });


    }

Upvotes: 1

Views: 69

Answers (3)

Raymond Mutyaba
Raymond Mutyaba

Reputation: 950

When you 'setText' you are using the variables from 'Calendar.getInstance();' which set those variables according to the current day, month, and year. Instead you should be using the variables that are set by the datePicker.

Instead of:

datee.setText(year + ":" + month + ":" + day);

use this:

datee.setText(year + ":" + monthOfYear + ":" + dayOfMonth);

Also, the month returned by Calendar.Month is off by one (10 instead of 11) because the Java Calendar goes from 0 to 11. So you have to add 1 to the month to get the correct number for the current month. But the month value (dayOfMonth) returned by the datePicker should be correct by default.

Upvotes: 0

Abhinav singh
Abhinav singh

Reputation: 1456

try this I hope its work.

   private DatePickerDialog.OnDateSetListener datePickerListener 
            = new DatePickerDialog.OnDateSetListener() {

    // when dialog box is closed, below method will be called.
    public void onDateSet(DatePicker view, int selectedYear,
            int selectedMonth, int selectedDay) {
        year = selectedYear;
        month = selectedMonth;
        day = selectedDay;

        // set selected date into textview
        tvDisplayDate.setText(new StringBuilder().append(month + 1)
           .append("-").append(day).append("-").append(year)
           .append(" "));

        // set selected date into datepicker also
        dpResult.init(year, month, day, null);

    }
};

Upvotes: 1

vasanth
vasanth

Reputation: 405

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton ib;
    private Calendar cal;
    private int day;
    private int month;
    private int year;
    private EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ib = (ImageButton) findViewById(R.id.imageButton1);
        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_MONTH);
        month = cal.get(Calendar.MONTH);
        year = cal.get(Calendar.YEAR);
        et = (EditText) findViewById(R.id.editText);
        ib.setOnClickListener((View.OnClickListener) this);
    }
@Override
    public void onClick(View v) {
        showDialog(0);
    }
    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        return new DatePickerDialog(this, datePickerListener, year, month, day);
    }

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {
            et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                    + selectedYear);
        }
    };
}

Upvotes: 1

Related Questions