英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论