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

Time:05-25

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.

CodePudding user response:

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")

CodePudding user response:

I want to use the selected date that is val startDay instead of val date = LocalDate.parse("2020-05-03"). When I replace it, the app crashes.

val startDay = ("$day-$month-$year") here you've created date as dd-MM-yyyy, but by default LocalDate.parse uses DateTimeFormatter.ISO_LOCAL_DATE to parse a string, that parses a string of format yyyy-MM-dd to LocalDate. That's why it's crashing as your date is invalid according to that format.

You have to provide a DateTimeFormatter of pattern dd-MM-yyyy to parse your date.

You can do it like this

val startDay = ("$day-$month-$year")
val dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")
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 startDate = LocalDate.parse(startDay, dateFormatter)
val endDate = startDate.plusMonths(duration.toLong()).format(dateFormatter)
binding.endDate.text = endDate
  • Related