0

in my Android Project i have a DatePicker which directly starts from year 1900.

Here is the source code:-

private void selectDate()
{
    dpd = new DatePickerDialog(getActivity(),
            new DatePickerDialog.OnDateSetListener()
            {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    c.set(year, monthOfYear, dayOfMonth);
                    selDate=year+"/"+monthOfYear+"/"+dayOfMonth;
                    Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
                }
            }, mYear, mMonth, mDay);
    dpd.show();
}

Here is a screenshot of the same:-

Android DatePicker

As you can see the calendar starts from 1900 but i want it to start from the current date

Also, you can see '2' written on the top left corner, on clicking '2' i can change the year to whatever i want to; but I don't want the '2' symbol in my Date Picker/ how i should i change the name '2' to 'Change year' text?

What should be done? Where do i need to incorporate the necessary changes?

Thanks in advance.

I have edited my code as follows and now i am getting the current date i.e oct 2016

private void selectDate()
{
    dpd = new DatePickerDialog(getActivity(),
            new DatePickerDialog.OnDateSetListener()
            {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
                {
                    c.set(Calendar.YEAR, year);
                    c.set(Calendar.MONTH, monthOfYear);
                    c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    selDate=year+"/"+monthOfYear+"/"+dayOfMonth;
                    Toast.makeText(getActivity(), dayOfMonth + "-" + (monthOfYear + 1) + "-" + year, Toast.LENGTH_LONG).show();
                }
            }, mYear, mMonth, mDay);
    dpd.getDatePicker().setMinDate(System.currentTimeMillis());
    dpd.show();
}

And here is the updated screenshot:

enter image description here

Simran
  • 593
  • 1
  • 14
  • 37
  • Possible duplicate of [How do I set the current date in a DatePicker?](http://stackoverflow.com/questions/6451837/how-do-i-set-the-current-date-in-a-datepicker) – Mike M. Oct 01 '16 at 05:13

5 Answers5

2

Check out this code snippet

   DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    final Calendar myCalendar = Calendar.getInstance();
                    @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);
                        String myFormat = "yyyy-MM-dd"; //In which you need put here
                        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

                     //   expiry.setText(sdf.format(myCalendar.getTime()));

                    }
                }, mYear, mMonth, mDay);
        dpd.getDatePicker().setMinDate(System.currentTimeMillis());
        dpd.show();
Hasan shaikh
  • 738
  • 1
  • 6
  • 16
1

Try this :

Calendar c = Calendar.getInstance();

c.setTimeInMillis(System.currentTimeMillis());
0

You need to pass the current date as an argument when invoking the DatePickerDialog

 // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int mYear = c.get(Calendar.YEAR);
    int mMonth = c.get(Calendar.MONTH);
    int mDay = c.get(Calendar.DAY_OF_MONTH);

As per your code, I don't see where you've assigned the values for mYear, mMonth and mDate.

PsyGik
  • 3,535
  • 1
  • 27
  • 44
0

I hope this piece of snippet works for u..

String strThatDay = datePicker.getYear() + "/" + (datePicker.getMonth() + 1) + "/" + datePicker.getDayOfMonth();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
            Date choosenDate = null;
            try {
                choosenDate = formatter.parse(strThatDay);//catch exception
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            final Calendar todayCal = Calendar.getInstance();
            todayCal.setTime(choosenDate);
PraKi
  • 13
  • 6
0
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog =
    new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
dialog.show();

you can initialize your picker this way. Check this on

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Jogendra Gouda
  • 405
  • 4
  • 17