Created
February 6, 2015 13:20
-
-
Save instcode/c139b168c5291efab420 to your computer and use it in GitHub Desktop.
Thread Safe Date Formatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package whatever.utils | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
public class DateUtils { | |
// Simple thread safe date formatter | |
private static class ConcurrentDateFormat extends ThreadLocal<DateFormat> { | |
private String format; | |
public ConcurrentDateFormat(String format) { | |
this.format = format; | |
} | |
@Override | |
protected DateFormat initialValue() { | |
return new SimpleDateFormat(format); | |
} | |
} | |
private static ConcurrentDateFormat DATE_FORMAT = new ConcurrentDateFormat("yyyy-MM-dd HH:mm:ssZ"); | |
public static String getTodayString() { | |
return DATE_FORMAT.get().format(new Date()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment