admin管理员组文章数量:1026989
I have a Kotlin code that retrives date (in text field) from SQLite DB and put it into a LocalDate
variable (pill.startDay
) from whom I show the date as below
val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ITALY)
val startDayStr: String =dtf.format(pill.startDay)
holder.tvStartDate.text = "Start: $startDayStr"
Now I need to show also hours and minutes but I've tryed many patterns (like "dd/MM/uuuu HH:mm" or "dd/MM/uuuu'T'HH:mm" or "dd/MM/uuuu HH:mm z") without success (the app always crashes)
The full data class is:
data class Pill(val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDate = LocalDate.now())
The error is:
FATAL EXCEPTION: main Process: com.example.mytestapp, PID: 24087 java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
I have a Kotlin code that retrives date (in text field) from SQLite DB and put it into a LocalDate
variable (pill.startDay
) from whom I show the date as below
val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ITALY)
val startDayStr: String =dtf.format(pill.startDay)
holder.tvStartDate.text = "Start: $startDayStr"
Now I need to show also hours and minutes but I've tryed many patterns (like "dd/MM/uuuu HH:mm" or "dd/MM/uuuu'T'HH:mm" or "dd/MM/uuuu HH:mm z") without success (the app always crashes)
The full data class is:
data class Pill(val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDate = LocalDate.now())
The error is:
Share Improve this question edited Nov 16, 2024 at 11:50 genespos asked Nov 16, 2024 at 11:34 genesposgenespos 3,3116 gold badges43 silver badges76 bronze badges 2 |FATAL EXCEPTION: main Process: com.example.mytestapp, PID: 24087 java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
2 Answers
Reset to default 4You are getting UnsupportedTemporalTypeException
because you have defined your data with LocalDate
.
LocalDate
doesn't have any information about the time and once you are trying to convert it to time, it causes a crash.
You need to update your data class Pill
to have LocalDateTime
instead of LocalDate
:
data class Pill(
val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDateTime = LocalDateTime.now()
)
The reason you cannot print hours of day, minutes of hour and further down the scala is that a LocalDate
exclusively stores information about day of month, month of year and year.
What you need seems to be a LocalDateTime
, which is combined from a LocalDate
and a LocalTime
.
I have a Kotlin code that retrives date (in text field) from SQLite DB and put it into a LocalDate
variable (pill.startDay
) from whom I show the date as below
val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ITALY)
val startDayStr: String =dtf.format(pill.startDay)
holder.tvStartDate.text = "Start: $startDayStr"
Now I need to show also hours and minutes but I've tryed many patterns (like "dd/MM/uuuu HH:mm" or "dd/MM/uuuu'T'HH:mm" or "dd/MM/uuuu HH:mm z") without success (the app always crashes)
The full data class is:
data class Pill(val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDate = LocalDate.now())
The error is:
FATAL EXCEPTION: main Process: com.example.mytestapp, PID: 24087 java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
I have a Kotlin code that retrives date (in text field) from SQLite DB and put it into a LocalDate
variable (pill.startDay
) from whom I show the date as below
val dtf: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu", Locale.ITALY)
val startDayStr: String =dtf.format(pill.startDay)
holder.tvStartDate.text = "Start: $startDayStr"
Now I need to show also hours and minutes but I've tryed many patterns (like "dd/MM/uuuu HH:mm" or "dd/MM/uuuu'T'HH:mm" or "dd/MM/uuuu HH:mm z") without success (the app always crashes)
The full data class is:
data class Pill(val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDate = LocalDate.now())
The error is:
Share Improve this question edited Nov 16, 2024 at 11:50 genespos asked Nov 16, 2024 at 11:34 genesposgenespos 3,3116 gold badges43 silver badges76 bronze badges 2FATAL EXCEPTION: main Process: com.example.mytestapp, PID: 24087 java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay
- Please share the stacktrace from the crash. – tyg Commented Nov 16, 2024 at 11:37
-
Also, what type is
pill.startDay
? Fully qualified, please. – tyg Commented Nov 16, 2024 at 11:38
2 Answers
Reset to default 4You are getting UnsupportedTemporalTypeException
because you have defined your data with LocalDate
.
LocalDate
doesn't have any information about the time and once you are trying to convert it to time, it causes a crash.
You need to update your data class Pill
to have LocalDateTime
instead of LocalDate
:
data class Pill(
val id: Int,
val pillName: String,
val yesDays: Int,
val pauseDays: Int = 0,
val startDay: LocalDateTime = LocalDateTime.now()
)
The reason you cannot print hours of day, minutes of hour and further down the scala is that a LocalDate
exclusively stores information about day of month, month of year and year.
What you need seems to be a LocalDateTime
, which is combined from a LocalDate
and a LocalTime
.
本文标签: androidLocalDate to string format with HHmmStack Overflow
版权声明:本文标题:android - LocalDate to string format with HH:mm - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659147a2161783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pill.startDay
? Fully qualified, please. – tyg Commented Nov 16, 2024 at 11:38