时间选择器中时间格式不会改变,Kotlin。

huangapple go评论54阅读模式
英文:

time format not changing in timepicker kotlin

问题

I used a timepicker in Android Studio Kotlin to receive input from the user as follows:

private lateinit var editTime : TimePicker

I successfully created the timepicker, but when I display the result in a recyclerview, the format is as follows:

0:4

which should actually be

12:04 AM

I'm not sure how to change the format. I tried this:

var hour = editTime.hour.toString().format("%02d")
val minute = editTime.minute.toString().format("%02d")

but it only worked with a timepicker dialog, not the widget one.

英文:

So I used a timepicker in android studio Kotlin to receive input from the user as the following:

`

private lateinit var editTime : TimePicker

`

I did create the timepicker successfully, but whenever I show the result in a recyclerview the format **is as the following:

0:4

which actually is

12:04 AM

I don't know how to change the format, I tried this:

       var hour = editTime.hour.toString().format("%02d")
        val minute = editTime.minute.toString().format("%02d")

but it only worked with a timepicker dialog not the widget one.

答案1

得分: 1

var hour = String(format: "%02d", editTime.hour)
var min = String(format: "%02d", editTime.minute)
英文:

you can try this

var hour = String.format("%02d", editTime.hour)
var min = String.format("%02d", editTime.minute)

答案2

得分: 0

你可以使用这个方法将小时和分钟转换为所需的格式

fun getTimeString(hour: Int, minute: Int): String {
    val time = LocalTime.of(hour, minute)
    val formatter = DateTimeFormatter.ofPattern("h:mm a")
    return time.format(formatter)
}
英文:

you can use this method to convert hour and minute into required format

    fun getTimeString(hour: Int, minute: Int): String {
        val time = LocalTime.of(hour, minute)
        val formatter = DateTimeFormatter.ofPattern("h:mm a")
        return time.format(formatter)
    }

huangapple
  • 本文由 发表于 2023年4月17日 05:23:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定