dev_android
dev_android

Reputation: 493

How to disable future date with Datepicker

I am trying to disable date picker for future date but getting not idea...

Here is the what I have tried:

 bday.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


             Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);


                DatePickerDialog   mDatePicker =new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
                {   
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) 
                    {
                            year = selectedyear; 
                            month = selectedmonth; 
                            day = selectedday;

                            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");      
                            Calendar cal = Calendar.getInstance();


                            String currenttime = new String(dateFormat.format(cal.getTime()));
                            String selectedtime = new String (new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            String futuretime = new String (dateFormat.format(System.currentTimeMillis()));      

                           if(selectedtime==currenttime)
                        { 
                            Toast.makeText(getApplicationContext(), "you were not born in the future", Toast.LENGTH_SHORT).show();
                            bday.setText("");
                        }
                        else
                        {
                     bday.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                    }}
                },year, month, day);
                mDatePicker.setTitle("Please select date");                
                mDatePicker.show();
        }
    });

Stackoverflow suggested to use:

mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

but in above condition how should I use this.? I am trying to disable future date. But not getting any idea :(

Upvotes: 0

Views: 7295

Answers (2)

Keshav Gera
Keshav Gera

Reputation: 11264

package com.keshav.datePicker_With_Hide_Future_Past_Date;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    EditText ed_date;
    int year;
    int month;
    int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed_date=(EditText) findViewById(R.id.et_date);

        ed_date.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Calendar mcurrentDate=Calendar.getInstance();
                year=mcurrentDate.get(Calendar.YEAR);
                month=mcurrentDate.get(Calendar.MONTH);
                day=mcurrentDate.get(Calendar.DAY_OF_MONTH);

                final DatePickerDialog   mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
                {
                    @Override
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
                    {
                              ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
                            int month_k=selectedmonth+1;

                    }
                },year, month, day);
                mDatePicker.setTitle("Please select date");
                // TODO Hide Future Date Here
                mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

                // TODO Hide Past Date Here
                //  mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
                mDatePicker.show();
            }
        });
    }
}

Upvotes: 1

Akshay
Akshay

Reputation: 6142

After Declaring Listener on DatePicker use the mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());

Upvotes: 1

Related Questions