When picking the date in datepicker android Studio it only retunrs the current date not the one you selected

Viewed 14

heres my code

irthdate = findViewById(R.id.birthdate); date = findViewById(R.id.date);

    birthdate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            calendar=Calendar.getInstance();
            year= calendar.get(Calendar.YEAR);
            month=calendar.get(Calendar.MONTH);
            dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);

            datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                    datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
                    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                    displaydate=format.format(new Date());
                    datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
                    date.setText(displaydate);

                }
            },year,month,dayOfMonth);
            datePickerDialog.show();
1 Answers

I've just modified your code. It's not tested. But hope it helps.

 birthdate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                String displaydate = year + "-" + month + "-" + day;
                date.setText(displaydate);
            }
        }, year, month, dayOfMonth);
        
        datePickerDialog.show();
    }
}
Related