SimpleDateFormat

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

public class DateUtils {

    public static final SimpleDateFormat dateFmt = new SimpleDateFormat("dd/MM/yyyy");

    public String format(Date inputDate) {
        return dateFmt.format(inputDate);
    }

}

The above code is wrong as its creating a static instance of a SimpleDateFormat. As you aware that the SimpleDateFormat is not thread safe. So the code will ultimately fail if one or more thread access the class at the same time. So we may get different outputs/strange behavior.

So its better create a separate SimpleDateFormat instance for each call.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s