giovanni.gambaro
giovanni.gambaro
giovanni.gambaro gambaro
1 Snippet  (681st place)
Published
1 Channel
Created
2 Channels
Following
Jul 7, 2017
Last Visit
Jun 26, 2017
Registered
29 points  (2971st place)
Reputation
Junior Code Generator
Junior Publisher
Junior Judge
Junior Popular Coder
Junior Autobiographer
Senior Autobiographer
Master Autobiographer

Recent Snippets See all snippets by giovanni.gambaro

public by giovanni.gambaro created Jun 27, 2017  524  1  3  1

Date formatting using ThreadLocal variable

How to avoid synchronization for a not thread safe classes
import java.text.SimpleDateFormat;
import java.util.Date;

public class Foo
{
    // SimpleDateFormat is not thread-safe, so give one to each thread
    private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue()
        {
            return new SimpleDateFormat("yyyyMMdd HHmm");
        }
    };

    public String formatIt(Date date)
    {
        return formatter.get().format(date);
    }
}
;