如何在 android studio 中将天数添加到所选日期

How to add number of days to the selected date in android studio

我在 DatePicker 中选择的日期(不是当前日期或今天日期)是 date1 : SelectedDate.setText(date1) = "05-04-2022".
首先,我想向 date1 添加 5 天以获得 date2 并将其显示在 EditText2 中以获取:InputDate.setText(date2) = "10-04-2022".
其次向 date1 添加 13 天以获取 date3 并将其显示在 EditText3 中以获取:tv_editDate.setText(date3) ="18-04-2022"

   SelectedDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);

            String myFormat = "dd/MM/yyyy"; //In which you need put here
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
            sdf = new SimpleDateFormat("dd/MM/yyyy");

            DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new 
            DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int 
                dayOfMonth) {

                    String date1 = String.valueOf(dayOfMonth) + "/" + 
                    String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);
             
                    SelectDate.setText(date1);                  
                    // Todoo .. add 5 days to date1
                    inputDate.setText(Date2);
                    // Todoo .. add 13 days to date1
                    tv_editDate.setText(date3);
                }
            }, yy, mm, dd);
            datePicker.show();
        }
    });

我找到了解决方案,如果有其他建议,我很高兴:

    inputLabel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final Calendar calendar = Calendar.getInstance();
            int yy = calendar.get(Calendar.YEAR);
            int mm = calendar.get(Calendar.MONTH);
            int dd = calendar.get(Calendar.DAY_OF_MONTH);
            String myFormat = "dd/MM/yyyy"; //In which you need put here
            SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
            sdf = new SimpleDateFormat("dd/MM/yyyy");

            DatePickerDialog datePicker = new DatePickerDialog(Activity_races.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    String date1 = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear+1) + "/" + String.valueOf(year);

                    // Todoo
                    //Given Date in String format
                    String oldDate = date1;
                    //Specifying date format that matches the given date
                    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                    Calendar c = Calendar.getInstance();
                    Calendar c1 = Calendar.getInstance();
                    try{
                        //Setting the date to the given date
                        c.setTime(sdf.parse(oldDate));
                        c1.setTime(sdf.parse(oldDate));
                    }catch(ParseException e){
                        e.printStackTrace();
                    }
                    //Number of Days to add
                    c.add(Calendar.DAY_OF_MONTH, 5);
                    c1.add(Calendar.DAY_OF_MONTH, 13);
                    //Date after adding the days to the given date
                    String Date2 = sdf.format(c.getTime());
                    String Date3 = sdf.format(c1.getTime());
                    //Displaying the new Date after addition of Days
                    SelectDate.setText(date1);
                    inputDate.setText(Date2);
                    tv_Date.setText(Date3);
                }
            }, yy, mm, dd);
            datePicker.show();
        }
    });