]> git.ktnx.net Git - mobile-ledger.git/commitdiff
Globals: use a thread-local per-thread date formatter/parser
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Feb 2019 19:30:09 +0000 (21:30 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 27 Feb 2019 19:30:09 +0000 (21:30 +0200)
the DateFormat classes aren't thread-safe

app/src/main/java/net/ktnx/mobileledger/utils/Globals.java

index 7a9913d8fae9e1a6f41361982e8555f3e11fb942..4562dbaec3f106ef5008283a74bee645785b5967 100644 (file)
@@ -31,10 +31,15 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public final class Globals {
+    private static final ThreadLocal<SimpleDateFormat> dateFormatter =
+            new ThreadLocal<SimpleDateFormat>() {
+                @Override
+                protected SimpleDateFormat initialValue() {
+                    return new SimpleDateFormat("yyyy/MM/dd", Locale.US);
+                }
+            };
     public static String[] monthNames;
     public static String developerEmail = "dam+mole-crash@ktnx.net";
-    private static SimpleDateFormat ledgerDateFormatter =
-            new SimpleDateFormat("yyyy/MM/dd", Locale.US);
     private static Pattern reLedgerDate =
             Pattern.compile("^(?:(\\d+)/)??(?:(\\d\\d?)/)?(\\d\\d?)$");
     public static void hideSoftKeyboard(Activity act) {
@@ -66,10 +71,9 @@ public final class Globals {
         }
         else toParse = dateString;
 
-//        Log.d("globals", String.format("Parsing date '%s'", toParse));
-        return ledgerDateFormatter.parse(toParse);
+        return dateFormatter.get().parse(toParse);
     }
     public static String formatLedgerDate(Date date) {
-        return ledgerDateFormatter.format(date);
+        return dateFormatter.get().format(date);
     }
 }
\ No newline at end of file