如何使用特定日期设置datepicker日期

时间:2016-08-29 12:09:29

标签: android datepicker

在我的Android应用程序中,员工可以申请从日期到日期的休假。现在,如果我选择从日期作为明天日期,那么到目前为止,它应该从日期或日期之后显示。示例(我从日期选择为31-08-2016,因此,如果我设置为日期,日期选择器应显示日期值从31-08-2016,而不是在该日期值之前。)

我的代码是。

  from_date_btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               final Calendar c = Calendar.getInstance();
               mYear = c.get(Calendar.YEAR);
               mMonth = c.get(Calendar.MONTH);
               mDay = c.get(Calendar.DAY_OF_MONTH);


               mDay = c.get(Calendar.DAY_OF_MONTH);

               // Launch Date Picker Dialog
               DatePickerDialog dialog = new DatePickerDialog(ApplyLeave.this,
                       new DatePickerDialog.OnDateSetListener() {

                           @Override
                           public void onDateSet(DatePicker view, int year,
                                                 int monthOfYear, int dayOfMonth) {
                               // Display Selected date in textbox
                               datestr = dayOfMonth + "-"
                                       + (monthOfYear + 1) + "-" + year;
                                    /*datestr = year + "-"
                                            + (monthOfYear + 1) + "-" + dayOfMonth;*/
                               System.out.println("Datestr is--" + datestr);

                               from_edit.setText(datestr);


                           }

                       }, mDay, mMonth, mYear);

               dialog.getDatePicker().setMinDate(c.getTimeInMillis());
               dialog.show();
           }
       });
        to_date_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Calendar c = Calendar.getInstance();
             /*   mYear = c.get(Calendar.YEAR);
                mMonth = c.get(Calendar.MONTH);
                mDay = c.get(Calendar.DAY_OF_MONTH);*/
                // Launch Date Picker Dialog
                DatePickerDialog dialog1 = new DatePickerDialog(ApplyLeave.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                // Display Selected date in textbox
                                datestr = dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year;
                                    /*datestr = year + "-"
                                            + (monthOfYear + 1) + "-" + dayOfMonth;*/
                                System.out.println("Datestr is--" + datestr);

                                to_edit.setText(datestr);


                            }

                        }, mDay, mMonth, mYear);
                c.set(mYear, mMonth, mDay);

                dialog1.getDatePicker().setMinDate(c.getTimeInMillis());
                dialog1.show();

            }
        });

3 个答案:

答案 0 :(得分:1)

嗨我使用它同样你想要的,请参考下面的代码,

private Long mindatemilliseconds, maxdatemilliseconds;


Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH, 1);
    Calendar c1 = Calendar.getInstance();
    c1.add(Calendar.DAY_OF_MONTH, 35);
    mindatemilliseconds = c.getTimeInMillis();
    maxdatemilliseconds = c1.getTimeInMillis();


DatePickerDialog datePickerDialog = new DatePickerDialog(
            LoanInformationActivity.this,
            DatePickerDialog.THEME_HOLO_DARK, new mDateSetListener2(),
            mYear, mMonth, mDay);
    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * (36524)
            / 100 * 84);

    datePickerDialog.getDatePicker().setDescendantFocusability(
            DatePicker.FOCUS_BLOCK_DESCENDANTS);

    datePickerDialog.getDatePicker().setMinDate(mindatemilliseconds);
    datePickerDialog.getDatePicker().setMaxDate(maxdatemilliseconds);

    datePickerDialog.setCancelable(false);
    datePickerDialog.setCanceledOnTouchOutside(false);
    datePickerDialog.show();



class mDateSetListener2 implements DatePickerDialog.OnDateSetListener {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub


    }

    }

谢谢希望这会对你有所帮助。

答案 1 :(得分:0)

将您选择的日期(例如 31-08-2016 )转换为纪元(长) 的 1472646490000

datePickerDialog.getDatePicker().setMinDate(/*date to set as min in long*/)

答案 2 :(得分:0)

尝试将日期转换为

int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year =  datePicker.getYear();

Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
long date  = calendar.getTimeInMillis();

然后,从日期开始,将其设置为“

dialog1.getDatePicker().setMinDate(date);