[JAVA] SimpleDateFormat is thread unsafe

SimpleDateFormat is often used when dealing with dates in Java, but this class is not thread safe Some people may wonder what it is, but sadly I've been addicted to it lately ...

When the thread was crowded and the barber was flowing, the date of 1950 was messed up, so when I looked it up, it was like this

private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public String getDate() {
    return sdf.format(new Date());
}

this,

private final String pattern = "yyyy-MM-dd HH:mm:ss";

public String getDate() {
    return new SimpleDateFormat(pattern).format(new Date());
}

Koya

Dawn!

Recommended Posts

SimpleDateFormat is thread unsafe
Use stream to verify that SimpleDateFormat is thread unsafe
DateFormat is not thread safe
When SimpleDateFormat is garbled like ٢٠١٨١٠٠٤٠٨٣١٣٣٦٥٧
What is thread safe (with Java)