在DatePicker中设置当前日期

时间:2016-10-12 16:30:50

标签: android date datepicker

我想设置当前日期,然后我仍然选择当前日期之前和当前日期之后的日期。

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

datePicker = new DatePickerDialog(view.getContext(), new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        daynum = (TextView) getActivity().findViewById(R.id.daynum);
        daynum.setText(view.getDayOfMonth()+ "-" + (view.getMonth()+1)+ "-" + view.getYear());
        new ReadJSON().execute("url"+"day=" + view.getDayOfMonth() + "&" + "month=" + (view.getMonth()+1) + "&" + "year=" + view.getYear());
        new ReadJSON1().execute("url" + "day=" + view.getDayOfMonth() + "&" + "month=" + (view.getMonth()+1) + "&" + "year=" + view.getYear());
    }
}, day, month, year);
datePicker.getDatePicker().setMinDate(c.getTimeInMillis());
datePicker.setTitle("Select Date");
datePicker.show();

2 个答案:

答案 0 :(得分:0)

这很容易实现。

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

//set selected date into datepicker also
dpResult.init(year, month, day, null);

DatePickerDialog dialog = new DatePickerDialog(this, datePickerListener, year, month, day);

private DatePickerDialog.OnDateSetListener datePickerListener 
                = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) 
        {

        }
};

请参阅complete tutorial

答案 1 :(得分:0)

我刚刚找到了我想要的东西。谢谢回复。

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

    datePicker = new DatePickerDialog(view.getContext(), new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            daynum = (TextView) getActivity().findViewById(R.id.daynum);
            daynum.setText(view.getDayOfMonth()+ "-" + (view.getMonth()+1)+ "-" + view.getYear());
            new ReadJSON().execute("url"+"day=" + view.getDayOfMonth() + "&" + "month=" + (view.getMonth()+1) + "&" + "year=" + view.getYear());
            new ReadJSON1().execute("url" + "day=" + view.getDayOfMonth() + "&" + "month=" + (view.getMonth()+1) + "&" + "year=" + view.getYear());
        }
    }, day, month, year);
   //subtract -3000 to select date before current date 
      c.add(Calendar.DATE, -3000);
    datePicker.getDatePicker().setMinDate(c.getTimeInMillis());
    datePicker.setTitle("Select Date");
    datePicker.show();