We currently use TimeZone.setDefault to set the default timezone for a thread. I know that with Java 8 calling this method sets the default timezone for the JVM.
Does anyone have suggestions about how to set the default timezone for a thread in Java 8 without having to rework a lot of code?
Thanks,
Ken
Don't use the system defaulting at all. Use ThreadLocal<T>
: either a ThreadLocal<TimeZone>
, or better, a ThreadLocal<ZoneId>
with the java.time
classes. Then you can fetch from there everywhere that you need the default.
Personally I'd try to avoid using a thread local at all, and pass around the context explicitly - or if you do want to have context implicitly, encapsulate all the context (e.g. for a web request) in an appropriate context type, rather than having separate thread locals for time zone, locale etc.
See more on this question at Stackoverflow