How to set a specific date in date picker in android

后端 未结 3 1225
没有蜡笔的小新
没有蜡笔的小新 2021-01-25 04:24

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 da

相关标签:
3条回答
  • 2021-01-25 04:49

    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 :)

    0 讨论(0)
  • 2021-01-25 05:10

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

    0 讨论(0)
  • 2021-01-25 05:12
    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;
    
    0 讨论(0)
提交回复
热议问题