Home > Back-end >  How to add number of days to the selected date in android studio
How to add number of days to the selected date in android studio

Time:04-12

My selected date in the DatePicker (not the current Date or Today Date) is date1 : SelectedDate.setText(date1) = "05-04-2022".
First i want to add 5 days to date1 to get date2 and display it in EditText2 to get: InputDate.setText(date2) = "10-04-2022".
Second add 13 days to date1 to get date3 and display it in EditText3 to get : 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();
        }
    });

CodePudding user response:

I found the solution and if there is another proposal it is with pleasure:

    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();
        }
    });
  • Related