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
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
private static final ThreadLocal<DateFormat> DATE_FORMAT = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm"));
If the field is never accessed by multiple threads, rename it to use
@NotThreadSafe private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Suppress false positives by adding the suppression annotation
@SuppressWarnings("DateFormatConstant") to the enclosing element.