How to set a specific date in date picker in andro

2020-04-26 02:48发布

问题:

I tried a lot,but all doesn't work.

I need my date picker the date starts with september 1 2014.But it starts 1900. I set minimum date concept it disappears all past dates. please guys anybody help.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daty);


        click=(ImageView)findViewById(R.id.click);


        hdate=(TextView)findViewById(R.id.hdate);

        timeStamp = new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis()));


        hdate.setText(timeStamp);


        final Calendar myCalendar = Calendar.getInstance();

        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {



            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {

                          myCalendar.set(Calendar.YEAR, year);
                        myCalendar.set(Calendar.MONTH, monthOfYear);
                        myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        click.setVisibility(View.VISIBLE);


                        updateLabel();

            }
            private void updateLabel() {
                // TODO Auto-generated method stub

                  String myFormat = "dd/MM/yyyy";
                  SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                  hdate.setText(sdf.format(myCalendar.getTime()));


            }

        };

        hdate.setOnClickListener(new OnClickListener() {



            public void onClick(View v) {

                DatePickerDialog datePickerDialog = new DatePickerDialog(Daty.this,date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH));




                datePickerDialog.show();
                datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());    

            }


        });


        click.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                         Intent in=new Intent(Daty.this,Mainy.class);
                            startActivity(in);

        }
        });

    }

}

回答1:

class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
    super(c, null, 2013, 4, 21);

    @SuppressWarnings("deprecation")
    Date min = new Date(2013-1900, 4, 21);

    DatePicker p = getDatePicker();
    CalendarView cv = p.getCalendarView(); // should check for null
    long cur = cv.getDate();
    int d = cv.getFirstDayOfWeek();
    p.setMinDate(min.getTime());
    cv.setDate(cur + 1000L*60*60*24*40);
    cv.setFirstDayOfWeek((d + 1) % 7);
    cv.setDate(cur);
    cv.setFirstDayOfWeek(d);
}

}

OR

DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();

Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMinDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;


回答2:

You can set by this code:

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

EDIT

To set Minimum date to your DatePicker you can set it by using :

datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);

which sets today's date as minimum date and all the past dates are disabled.

Note: setMinDate was introduced in API 11

EDIT #2

You need to create custom DatePickerDialog for that :

class DatePickerDialog1964 extends DatePickerDialog {
    DatePickerDialog1964(Context c) {
    super(c, null, 2014, 1, 1);

    @SuppressWarnings("deprecation")
    Date min = new Date(2014-1900, 1, 1);

    DatePicker p = getDatePicker();
    CalendarView cv = p.getCalendarView(); // should check for null
    long cur = cv.getDate();
    int d = cv.getFirstDayOfWeek();
    p.setMinDate(min.getTime());
    cv.setDate(cur + 1000L*60*60*24*40);
    cv.setFirstDayOfWeek((d + 1) % 7);
    cv.setDate(cur);
    cv.setFirstDayOfWeek(d);
  }
}

To use this :

new DatePickerDialog1964(context).show();



回答3:

Simple sollution:

int year = 2014;
int month = 1;
int day = 1;
DatePickerDialog datePickerDialog = new DatePickerDialog(EditProfileScreen.this, 
android.R.style.Theme_Holo_Light_Dialog,
                    onDateSetListener, year, month, day);

datePickerDialog.setTitle(getString(R.string.birthday));
datePickerDialog.show();

So date picker starts on jan 1st 2014 as you wanted :)