From 0a060dd111f51bdf4f7c804b11cd8aa7cbe00964 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 27 Feb 2019 21:30:09 +0200 Subject: [PATCH] Globals: use a thread-local per-thread date formatter/parser the DateFormat classes aren't thread-safe --- .../java/net/ktnx/mobileledger/utils/Globals.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java index 7a9913d8..4562dbae 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Globals.java @@ -31,10 +31,15 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public final class Globals { + private static final ThreadLocal dateFormatter = + new ThreadLocal() { + @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 -- 2.39.5