giovanni.gambaro
giovanni.gambaro
Reputation Top 10%
giovanni.gambaro gambaro
1 Snippet  (737th place)
Published
1 Channel
Created
2 Channels
Following
Jul 7, 2017
Last Visit
Jun 26, 2017
Registered
29 points  (1092nd 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  465  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);
    }
}
;