Parthiban M
Parthiban M

Reputation: 1104

To set custom date in DatePicker

I'm trying to create an application in which there are two fields, "fromDate" and "toDate". The constraints are the "fromDate" should be more than or equal to current date and the "toDate" should be more than or equal to fromDate. I want the DatePicker to be opened with this constraints. Can anyone help me how I can achieve this.

I used the following code to get the "fromDate" correctly,

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

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();
        }

    };

fromDate_textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            datePickerDialog = new DatePickerDialog(
                    Sell_Product_Activity.this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar
                            .get(Calendar.DAY_OF_MONTH));
            datePickerDialog.show();
            datePickerDialog.getDatePicker().setMinDate(
                    System.currentTimeMillis() - 1000);
        }
    });

Likewise can anyone please help me how I can get the date from the fromDate_textView and set the "toDate" more than or equal to that date.

Upvotes: 1

Views: 2757

Answers (2)

Sagar Zala
Sagar Zala

Reputation: 5134

Use below code:

    //onCreate
    {
    fromDate_textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            myCalendar  = Calendar.getInstance();

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

            DatePickerDialog dialog = new DatePickerDialog(Home.this,
                    mDateSetListener, year, month , day );

            dialog.show();
        }
    });
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int y, int m,
                int d) {
            year = y;
            month  = m+ 1;
            day  = d;

            String date = new StringBuilder().append(day).append("-").append(month)
                .append("-").append(year).append("").toString();

            fromDate_textView.setText(date);
        }
    };

Upvotes: 3

Androider
Androider

Reputation: 3873

try it:

   Calendar cal=Calendar.getInstance(Locale.ENGLISH);
    dp.init(cal.getTime().getYear()+1900, cal.getTime().getMonth(), cal.getTime().getDay(), this);

Also check this tutorial:

http://android-pro.blogspot.in/2010/04/android-date-and-time-controls.html

Upvotes: 1

Related Questions