Search code examples
androidkotlinjodatime

Timezone defaults to UTC


I have an application that is running as expected. Many portion of the application displays DateTime. I am using jodatime library.

After adding features to the application, default timezone changed to UTC.

I was wondering why this happen because I did not explicitly set timezone, I am initializing DateTime with, DateTime.now() or DateTime(millis). I know I should set timezones explicitly, but what could be any reasons for jodatime's default timezone to change?


Solution

  • 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, something like this:

        private lateinit var dateTime: DateTime
    
        override fun onCreate() {
            super.onCreate()
            dateTime = DateTime.now()
        }