You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@empire-db.apache.org by do...@apache.org on 2021/04/28 12:42:15 UTC

[empire-db] branch master updated: EMPIREDB-347 Avoid unneccessary instances of SimpleDateFormat

This is an automated email from the ASF dual-hosted git repository.

doebele pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/empire-db.git


The following commit(s) were added to refs/heads/master by this push:
     new 9677055  EMPIREDB-347 Avoid unneccessary instances of SimpleDateFormat
9677055 is described below

commit 9677055aac1356fe3f90b05837dbda298fa198d1
Author: Rainer Döbele <do...@apache.org>
AuthorDate: Wed Apr 28 14:42:12 2021 +0200

    EMPIREDB-347
    Avoid unneccessary instances of SimpleDateFormat
---
 .../org/apache/empire/commons/ObjectUtils.java     | 26 ++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

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 @@ public final class ObjectUtils
     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 @@ public final class ObjectUtils
         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 final class ObjectUtils
     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);
     }
     
     /**