DateFormat with ThreadLocal

Here is a quick tip how to use DateFormat inside a ThreadLocal field.
Why do we need that? The reason is the DateFormat class is not thread-safe but creating its instances is an expensive operation. So, this is kind of a workaround that creates only one instance of DateFormat per thread.

package com.imsavva.test;

import java.text.SimpleDateFormat;
import java.util.Date;

 * @author Savva Kodeikin
public class ThreadLocalDateFormatTest   {

    private static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("dd/MM/yyyy");

    public String formatDate(Date date) {
        return dateFormat.get().format(date);