What is the difference between the two? Which one is better/recommended for setting the timezone?
Looking at the code, the difference is essentially about when the TimeZone default gets set -
In the first case the default takes effect when the first call to TimeZone class is made(essentially lazily) - say TimeZone.getDefault()
, at this point the TimeZone.setDefault
gets called, the user.timezone
is looked up and the default is set.
In the second scenario, the default is set eagerly - when you explicitly call TimeZone.setDefault
I think that is the only difference, either one should in practice work fine.
Update
This is the code from java.util.TimeZone
class
The very first call to TimeZone.getDefault() has this flow, which covers your first point.
TimeZone.getDefault()
-> TimeZone.setDefaultZone() (lookup "user.timezone" , set class variable and return timezone)
-> return defaulttimezone
Next time getDefault() is called, it just returns the class variable
In the case of TimeZone.setDefault(timeZone), the class level variable defaultTimeZone
is being set:
defaultTimeZone = zone;
and returned.
The biggest difference should be that "user.timezone" property and TimeZone's default timezone are stored separately.
See Java SE 8 doc TimeZone#getDefault, TimeZone#setDefault.
The main point is that TimeZone.getDefault
may use "user.timezone" property as it's initial value, but the value will be cached. That means following changes to "user.timezone" won't affect TimeZone.getDefault
. And TimeZone.setDefault
does not change "user.timezone" property.
If you are not sure which one your code or third party libraries depend on, you'd better set both:
System.setProperty("user.timezone", "America/Chicago");
TimeZone.setDefault(null); // or TimeZone.setDefault(TimeZone.getTimeZone("America/Chicago"));
Take Joda Time as an example, it depends on both "user.timezone" property and TimeZone.getDefault
, and "user.timezone" take precedence. See DateTiimeZone.getDefault:
try {
String id = System.getProperty("user.timezone");
if (id != null) { // null check avoids stack overflow
zone = forID(id);
}
} catch (RuntimeException ex) {
// ignored
}
if (zone == null) {
zone = forTimeZone(TimeZone.getDefault());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With