简体   繁体   中英

TimeZone.setDefault for a thread in Java 8

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 (eg for a web request) in an appropriate context type, rather than having separate thread locals for time zone, locale etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM