Date Format Utility with ThreadLocal


DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. DateFormat helps you to format and parse dates for any locale but they are not synchronized.


Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally by using ThreadLocal.


The ThreadLocal class in Java enables you to create variables that can only be read and written by the same thread. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal variable, then the two threads cannot see each other’s ThreadLocal variables.

Example of Date Format Utility:

I have created the date format utility project to achive the mentioned goals :

  • Convert the input date string to the given date format.

String ddMMMYYYYHHMM = "15Jun2016 10:00";

DateFormatUtil.formatDate(ddMMMYYYYHHMM, "ddMMMyyyyHHmm", "yyyy-MM-dd'T'HH:mm:ss");

  • Parse the input date string format.
 String ddMMMyyyy = "06Jul1985";

Note: I have implement the date format utility using ThreadLocal.

ThreadLocal<SimpleDateFormat> inputDateFormatter = new ThreadLocal<SimpleDateFormat>() {
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(inputDateFormat);

You can download the complete code of date format utility from the mentioned path –


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s