Skip to content

Instantly share code, notes, and snippets.

@instcode
Created February 6, 2015 13:20
Show Gist options
  • Save instcode/c139b168c5291efab420 to your computer and use it in GitHub Desktop.
Save instcode/c139b168c5291efab420 to your computer and use it in GitHub Desktop.
Thread Safe Date Formatter
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