我有以下字符串:
18/07/2019 16:20
我尝试使用以下代码将此字符串转换为 LocalDateTime
:
val stringDate = expiration_button.text.toString()
val date = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm")).toString()
java.time.format.DateTimeParseException: Text '18/07/2019 04:30:00' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor
我错过了什么?
最佳答案
我想这会回答你的问题:
val stringDate = expiration_button.text.toString()
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy hh:mm");
val dt = LocalDate.parse(stringDate, formatter);
编辑 1:
它可能会崩溃,因为您使用的是 12 小时制,而不是 24 小时制。
使用大写 H 将小时模式更改为 24 小时模式应该可以解决此问题:
val dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
关于android - 将字符串转换为 LocalDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57096793/