选择未来日期时,我正在获取当前日期

时间:2019-10-10 10:58:44

标签: android datepicker

这是我的代码,我选择的是未来,但要获取当前日期, 如何获取我选择的日期?

Calendar mcurrentDate = Calendar.getInstance();

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

final DatePickerDialog mDatePicker = new DatePickerDialog(WesternRecMoneyActivity.this, new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker datepicker, int selectedyear, int electedmonth, int selectedday) 
    {
        binding.recvPassExpDate.setText(new 
        StringBuilder().append(day).append("/").append(month + ).append("/").append(year));
        int month_k = selectedmonth + 1;
    }
}, year, month, day);

// Hide Past Date Here
mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();

1 个答案:

答案 0 :(得分:4)

  

选择“未来日期”时我正在获取当前日期

因为您没有设置DatePicker的选定日期

检查您的onDateSet() method

您需要使用selectedday中的selectedmonthselectedyearonDateSet() method

使用此

binding.recvPassExpDate.setText(new 
                              StringBuilder().append(selectedday).append("/").append(selectedmonth 
                                     + 1).append("/").append(selectedyear));

代替这个

binding.recvPassExpDate.setText(new 
                                 StringBuilder().append(day).append("/").append(month 
                                     + 1).append("/").append(year));