如何在Android中将21天的日期选择器值添加

时间:2019-06-20 07:15:43

标签: android datepicker

public void Pickdateheatdryopen(View view) {
        final Calendar calender = Calendar.getInstance ();
        final int year = calender.get ( Calendar.YEAR );
        final int month = calender.get ( Calendar.MONTH );
        final int day = calender.get ( Calendar.DAY_OF_MONTH );
        calender.add(Calendar.DATE,5);


        datePickerDialog = new DatePickerDialog ( CowActivity.this, new DatePickerDialog.OnDateSetListener () {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                /*SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
final Calendar calender = Calendar.getInstance ();
        final int year = calender.get ( Calendar.YEAR );
        final int month = calender.get ( Calendar.MONTH );
        final int day = calender.get ( Calendar.DAY_OF_MONTH );
        calender.add(Calendar.DATE,5);                
Date s = calender.getTime();
                Log.e(TAG,"vale"+sdf.format(s));*/

                inputheatdry.setText ( (dayOfMonth) + "/" + (month+1 ) + "/" + (year) );
               // read1 ();
            }

        }, year, month, day );
        datePickerDialog.show ();
    }

我像上面一样尝试过,但是我没有得到确切的输出。我使用了压延对象并设置了值,但是我没有将选择器值增加几天。

2 个答案:

答案 0 :(得分:2)

首先将21天添加到您的日历中,然后以以下方式获取并为DatePickerDialog设置年,月和日:

final Calendar calender = Calendar.getInstance ();
 calender.add(Calendar.DAY_OF_MONTH,21);
 final int year = calender.get ( Calendar.YEAR );
 final int month = calender.get ( Calendar.MONTH );
 final int day = calender.get ( Calendar.DAY_OF_MONTH);

这将显示从今天DatePickerDialog中选择的21日(例如,2019年6月20日,将选择2019年7月11日)

已更新(要在从DatePickerDialog中选择日期之后添加21天):

        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

            SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
            final Calendar calender = Calendar.getInstance();
            calender.set(Calendar.YEAR,year);
            calender.set(Calendar.MONTH,month);
            calender.set(Calendar.DAY_OF_MONTH,dayOfMonth);
            calender.add(Calendar.DAY_OF_MONTH,21);
            Date s = calender.getTime();
            Log.e(TAG, "vale" + sdf.format(s));
            // read1 ();
        }

答案 1 :(得分:0)

您必须像下面这样

Datafile

在从public void Pickdateheatdryopen(View view) { final Calendar calender = Calendar.getInstance (); calender.add(Calendar.DATE,21); final int year = calender.get ( Calendar.YEAR ); final int month = calender.get ( Calendar.MONTH ); final int day = calender.get ( Calendar.DAY_OF_MONTH ); datePickerDialog = new DatePickerDialog ( CowActivity.this, new DatePickerDialog.OnDateSetListener () { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { inputheatdry.setText ( (dayOfMonth) + "/" + (month+1 ) + "/" + (year) ); // read1 (); } }, year, month, day ); datePickerDialog.show (); } 对象获得day之前,您必须在calendar中添加天。

注意::它将从当前日期开始增加几天。

更新

calendar