DateFormat
is not thread-safe. The documentation recommends creating separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
The Google Java Style Guide §5.2.4 requires CONSTANT_CASE
to only be used for static final fields whose contents are deeply immutable and whose methods have no detectable side effects, so fields of type DateFormat
should not use CONSTANT_CASE
.
TIP: Consider using the java.time
API added in Java8, in particular DateTimeFormatter
. One its many advantages over DateFormat
is that it is immutable and thread-safe.
If the date formatter is accessed by multiple threads, consider using ThreadLocal
:
Read full article from DateFormatConstant
No comments:
Post a Comment