-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Description
java.text.SimpleDateFormat 内部是有状态的,并非线程安全。
Synchronization
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
org.apache.commons.lang3.time.FastDateFormat 才是线程安全的。
FastDateFormat is a fast and thread-safe version of SimpleDateFormat.
Since FastDateFormat is thread safe, you can use a static member instance:
private static final FastDateFormat DATE_FORMATTER = FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT);
This class can be used as a direct replacement to SimpleDateFormat in most formatting and parsing situations. This class is especially useful in multi-threaded server environments. SimpleDateFormat is not thread-safe in any JDK version, nor will it be as Sun have closed the bug/RFE.