'How can I add months Int to a choosen date on date picker in android Kotlin?

I have a working date picker in my app that replaces an EditText after a date selection. I want to add duration through a RadioGroup button that prints an Int to provoke an end date. How can I do that? I've spent the last two days without getting the result I'm looking to get.

Here is what I've got so far.


    val datePicker = findViewById<DatePicker>(R.id.date_Picker)
            val today = Calendar.getInstance()
            datePicker.init(
                today.get(Calendar.YEAR),
                today.get(Calendar.MONTH),
                today.get(Calendar.DAY_OF_MONTH
            ) { view, year, month, day ->
                val month = month + 1
    
                val startDay = ("$day-$month-$year")
    
                binding.fechadeinicio.text = fechainicio

                val duration = when (binding.duracion.checkedRadioButtonId) {
                            R.id.doce -> 12
                            R.id.veinticuatro -> 24
                            R.id.treintayseis -> 36
                            else -> 36
                        }
    
                  //  val endDate = startDate.plusMonths(Duration.toLong())
                 //   binding.endDate.text = endDate.toString()
               }

Here is the closest one to the result I'm looking to get. Yet, I want to use the selected date val startDay, instead of val date = LocalDate.parse("2020-05-03"). When I replace it, the app crashes.


    val date = LocalDate.parse("2020-05-03")
    // Displaying date
    println("Date : $date")
    // Add 2 months to the date
    val newDate = date.plusMonths(2)
    println("New Date : $newDate")

Please, let me know how I can get the desired result?

Thanks.



Solution 1:[1]

You can use LocalDate using of function.

Example here:

    val date = LocalDate.of(year, month, day)
    // Displaying date
    println("Date : $date")
    // Add 2 months to the date
    val newDate = date.plusMonths(2)
    println("New Date : $newDate")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ticherhaz FreePalestine