0

Hello I am using the threeten library that provides me with the object org.threeten.bp.OffsetDateTime. The application works great when I am on EST time, but when I manually change my device to Pacific time to make sure the times are being displayed correctly, I am still getting EST time. I thought when using this object, the object obtains the current time and time zone from the device. I have tried the following and obtain the date string in EST. Any thoughts? Thank you for your help

Code:

OffsetDateTime time = OffsetDateTime.now();
Log.i(TAG, time.tostring());

output: 2017-07-31T19:49:11.828-04:00

paul590
  • 1,385
  • 1
  • 22
  • 43
  • 1
    `OffsetDateTime.now()` uses the current offset of the **default timezone [configured in the JVM**](https://stackoverflow.com/questions/22930726/where-does-android-take-default-timezone-from). You didn't specify how you changed your device's time, but probably it was in a way that didn't affect JVM's configuration. You can check the JVM's default using `ZoneId.systemDefault().getId()`. Get this value to know if your configuration really changed the JVM's default timezone. –  Aug 01 '17 at 12:02
  • @Hugo I tried changing the physical devices timezone by going through settings and my emulator by using emulator (at)Nexus_5_API_25 -timezone Europe/Paris. In both devices the time display what it should be in European time but unfortunately, the log still displays eastern time – paul590 Aug 01 '17 at 14:46
  • Have you checked if the value of `ZoneId.systemDefault().getId()` is also `Europe/Paris`? –  Aug 01 '17 at 15:03
  • @Hugo yes, its still gives me US/Eastern – paul590 Aug 01 '17 at 15:15
  • 1
    So it's not changing JVM's default. Unfortunately I never did this, so I don't know the correct way to change JVM's default in the device (physically or setting the device configs). Although you can also do `TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"))`, if you just want to make a test (this changes for the JVM, but it's lost if you reboot the system) –  Aug 01 '17 at 16:12
  • 1
    @Hugo Thank you for your help I ended out doing this and it worked! – paul590 Aug 02 '17 at 21:44
  • 1
    Instead of changing the JVM default, you can also do `OffsetDateTime.now(ZoneId.of("Europe/Paris"))` –  Aug 02 '17 at 21:48
  • 1
    @Hugo great idea! thank you again! I ended out doing the first option since I have multiple offsetdatetime variables – paul590 Aug 02 '17 at 21:51

0 Answers0