繁体   English   中英

如何在 android Kotlin 中的日期选择器上将月份 Int 添加到选定的日期?

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

我的应用程序中有一个工作日期选择器,它在选择日期后替换了EditText 我想通过一个RadioGroup按钮添加持续时间,该按钮打印一个 Int 以引发结束日期。 我怎样才能做到这一点? 我花了过去两天没有得到我想要的结果。

这是我到目前为止所得到的。


    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()
               }

这是最接近我想要得到的结果的一个。 然而,我想使用选定的日期 val startDay,而不是val date = LocalDate.parse("2020-05-03") 当我更换它时,应用程序崩溃。


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

请让我知道如何获得所需的结果?

谢谢。

您可以of函数来使用LocalDate

这里的例子:

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

我想使用选定的日期val startDay而不是val date = LocalDate.parse("2020-05-03") 当我更换它时,应用程序崩溃。

val startDay = ("$day-$month-$year")在这里您已将日期创建为dd-MM-yyyy ,但默认情况下LocalDate.parse使用DateTimeFormatter.ISO_LOCAL_DATE解析字符串,该字符串解析格式为yyyy-MM-ddLocalDate 这就是为什么它崩溃的原因,因为您的日期根据该格式无效。

您必须提供模式dd-MM-yyyyDateTimeFormatter来解析您的日期。

你可以这样做

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

DatePicker返回年、月和日int值,现在在创建日期时,如val startDay = ("$day-$month-$year")将导致小于 10 的天数和月数为单位数,这将返回1 Jan 20201-1-2020但日期格式化程序预计它是01-01-2020

为了解决这个问题,我们必须在将int值分配给startDay之前对其进行格式化,我们可以使用Stringformat方法返回 2 位数字,例如"%02d".format(intValue)

改变

val startDay = ("$day-$month-$year")

val startDay = "${"%02d".format(day)}-${"%02d".format(month)}-$year"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM