DateTime suddenly change for the whole app

  Kiến thức lập trình

I have an application running fine. After working on it for additional features, I realize all my DateTime in the application is changed and negatively offset to my timezone, in other words, it uses UTC.

I did not touch any database related codes that could affect DateTime, and I did not change JodaTime version, nor change my Room TypeConverter to save Datetime in my DB.

Do not call DateTime.now() before application::onCreate. This will result to a null default TimeZone. If you somehow initialize a DateTime variable in an application class like this:

    private var dateTime: DateTime = DateTime.now()

Delay its call after or inside application::onCreate, somethine like this:

    private lateinit var dateTime: DateTime

    override fun onCreate() {
        super.onCreate()
        dateTime = DateTime.now()
    }

LEAVE A COMMENT