The Issue
While creating formatters to be used in the java application many people declare the formatters as static elements. e.g.:
- private static final SimpleDateFormat LOG_DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
- private static final DecimalFormat LOG_AMOUNT_FORMAT = new DecimalFormat("###########0.00");
Solution/Fix
The same code that is thread safe can be written like so:
private static final String LOG_AMOUNT_FORMAT = "###########0.00";
then in your method create a new instance locally.
SimpleDateFormat dateFormatter = new SimpleDateFormat(LOG_AMOUNT_FORMAT);String formattedDate = dateFormatter.format(myDate);
Hope this helps people avoid the horrible nightmares I had with this issue.
No comments:
Post a Comment