by
29 7
1
3
423
1
Top 5% !
Popular
Specified
Created
Modified Jun 27, 2017
This snippet is Public
No tags for this snippet yet.
Languagecsharp
LanguageJava

Date formatting using ThreadLocal variable

How to avoid synchronization for a not thread safe classes
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/2055117/Date-formatting-using-ThreadLocal-variab?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
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); } }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus