EMPIREDB-347
Avoid unneccessary instances of SimpleDateFormat
diff --git a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
index 42df629..1df1b3d 100644
--- a/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
+++ b/empire-db/src/main/java/org/apache/empire/commons/ObjectUtils.java
@@ -75,6 +75,24 @@
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+ // DateOnly Formatter
+ private static final ThreadLocal<SimpleDateFormat> dateOnlyFormatter = new ThreadLocal<SimpleDateFormat>() {
+ @Override
+ protected SimpleDateFormat initialValue()
+ {
+ return new SimpleDateFormat(DATE_FORMAT);
+ }
+ };
+
+ // DateTime Formatter
+ private static final ThreadLocal<SimpleDateFormat> dateTimeFormatter = new ThreadLocal<SimpleDateFormat>() {
+ @Override
+ protected SimpleDateFormat initialValue()
+ {
+ return new SimpleDateFormat(DATETIME_FORMAT);
+ }
+ };
+
private ObjectUtils()
{
// Static Function only
@@ -619,9 +637,9 @@
try
{ String str = v.toString();
if (str.length() > 10)
- return new SimpleDateFormat(DATETIME_FORMAT).parse(str);
+ return dateTimeFormatter.get().parse(str);
else
- return new SimpleDateFormat(DATE_FORMAT).parse(str);
+ return dateOnlyFormatter.get().parse(str);
} catch (Exception e)
{
log.error("Cannot convert value to date!", e);
@@ -641,9 +659,9 @@
public static String formatDate(Date date, boolean withTime)
{
if (withTime)
- return new SimpleDateFormat(DATETIME_FORMAT).format(date);
+ return dateTimeFormatter.get().format(date);
else
- return new SimpleDateFormat(DATE_FORMAT).format(date);
+ return dateOnlyFormatter.get().format(date);
}
/**