整个应用程序的日期时间突然改变

问题描述 投票:0回答:1

我有一个运行良好的应用程序。在研究了其他功能之后,我意识到应用程序中的所有日期时间都已更改,并对我的时区进行了负偏移,换句话说,它使用 UTC。

我没有接触任何可能影响 DateTime 的数据库相关代码,也没有更改 JodaTime 版本,也没有更改我的 Room TypeConverter 以将 Datetime 保存在我的数据库中。

android jodatime
1个回答
0
投票

请勿在

DateTime.now()
之前致电
application::onCreate
。这将导致
null
默认
TimeZone
。如果您以某种方式在应用程序类中初始化 DateTime 变量,如下所示:

    private var dateTime: DateTime = DateTime.now()

application::onCreate
之后或内部延迟其调用,如下所示:

    private lateinit var dateTime: DateTime

    override fun onCreate() {
        super.onCreate()
        dateTime = DateTime.now()
    }
© www.soinside.com 2019 - 2024. All rights reserved.