预备日期在android中使用日历

时间:2017-10-25 05:35:35

标签: java android datepicker calendar

我想将日期显示为“当前日期 - 180”天。我怎么能显示这个?我在下面添加我的代码。现在我只能从datepicker对话框中选择日期,不能在textview中预先填充日期。

Calendar calendarFrom = Calendar.getInstance();
    datePickerFrom = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            StringBuilder selectedDate = new StringBuilder().append(dayOfMonth).append("-").append(monthOfYear + 1).append("-").append(year);
            textDateFrom.setText(selectedDate);

        }
    }, calendarTwo.get(Calendar.YEAR), calendarTwo.get(Calendar.MONTH), calendarFrom .get(Calendar.DAY_OF_MONTH));

    textDateFrom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            datePickerFrom .getDatePicker().setMaxDate(new Date().getTime());
            datePickerFrom .show();
        }
    });

1 个答案:

答案 0 :(得分:0)

你可以做到

    Calendar calendarFrom = Calendar.getInstance();
    calendarFrom.add (Calendar.DATE, -180);

    System.out.println(calendarFrom.getTime());