0

I am looking for the most simple and cleanest way to fix the timezone for all dates in an Android app. The idea is to have the app running as if the user were in another timezone. Let me clarify what I am looking for:

Let's say the user's phone is set to America/New_York then I would like my app to show all dates (are in UTC) in the Europe/Amsterdam timezone, regardless of the timezone that is set on the phone itself. And if I make a comparison with a new Date() it would be very nice if that new Date() is also in the current time of the Europe/Amsterdam timezone.

After searching the internet for solutions, I started to get the feeling that I will have to update every place in my app where a Date is used and force the use of the target timezone, like the solution of this stackoverflow post: Converting UTC dates to other timezones

Does anybody know how to get this done in a more easy and cleaner way?

  • 2
    Use the modern time classes that were introduced in Java 8. (LocalDateTime, ZonedDateTime, etc.) – Tenfour04 May 06 '21 at 22:19
  • 1
    If by `new Date()` you mean `java.util.Date` - that object is purely UTC. It has no time zone at all. Besides, you should be using `java.time.*` APIs, either through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) or the newer [desugaring support](https://developer.android.com/studio/write/java8-support#library-desugaring). – Matt Johnson-Pint May 06 '21 at 22:19
  • 1
    *And if I make a comparison with a new Date() it would be very nice if that new Date() is also in the current time of the Europe/Amsterdam timezone.* An old-fashioned `Date` is never in any particular time zone, so this is impossible. – Ole V.V. May 07 '21 at 02:01
  • Does tis perhaps answer your question? [How to set a JVM TimeZone Properly](https://stackoverflow.com/questions/2493749/how-to-set-a-jvm-timezone-properly). I know it’s not specifically about Android. – Ole V.V. May 07 '21 at 19:59

2 Answers2

1

The answer for anyone using java.time, the modern Java date and time API.

java.time does not include an option for setting the JVM default time zone. And wisely so. It’s not something you should want to do. By doing it you affect every program running in the same JVM, and also every part of your program and other program in the JVM may set it differently, ruining your intentions.

Avoid the need

In your time operations be explicit about which time zone you want, and you will always know what you get independently of the JVM setting. Example:

    System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Dushanbe")));

Example output:

2021-05-09T00:36:25.171213+05:00[Asia/Dushanbe]

System.setProperty

If you have already written a lot of code relying on the default time zone of the JVM, the hack to set it is:

    System.setProperty("user.timezone", "Australia/Tasmania");
    System.out.println(ZonedDateTime.now());

This just printed:

2021-05-09T05:38:03.568350+10:00[Australia/Tasmania]

It’s not very robust, though, for the reasons mentioned in the beginning.

If you want validation of the string you are passing, use:

    System.setProperty("user.timezone", ZoneId.of("Australia/Tasmania").getId());

Disclaimer

It seems from your question that you are already using the old, poorly designed and long outdated java.util.Date class and friends. I still wanted to post the answer for users who have the option to go modern. (You may also use each of the two ideas presented with the out-dated API.)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I would try TimeZone.setDefault(TimeZone) like in:

TimeZone.setDefault(TimeZone.getTimeZone("Europe/Amsterdam"));

(advice to check returned time zone, getTimeZone does not throw exception for unknown time zone - or use ZoneId instead of the String)

see TimeZone (this also mentions the user.timezone system property)

but I am not an android programmer/user