【发布时间】:2018-04-10 21:38:23
【问题描述】:
如何在 Kotlin 中以当地时间获取当前日期(日月和年)和时间(小时、分钟和秒)?
我尝试通过LocalDateTime.now(),但它给了我一个错误说Call requires API Level 26 (curr min is 21)。
如何在 Kotlin 中获取时间和日期?
【问题讨论】:
-
对于低于 26 的 API 级别,只需将 the ThreeTenABP library 添加到您的项目中,导入
org.threeten.bp.LocalDateTime然后按照您的尝试进行操作:LocalDateTime.now(yourTimeZone)(我建议指定时区,但如果您不这样做您将获得 JVM 时区设置)。在this question: How to use ThreeTenABP in Android Project 中查看更多信息。 -
检查此链接以获得您需要的解决方案:stackoverflow.com/questions/3747490/…