From: Damyan Ivanov Date: Wed, 27 Feb 2019 19:30:09 +0000 (+0200) Subject: Globals: use a thread-local per-thread date formatter/parser X-Git-Tag: v0.7~50 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=0a060dd111f51bdf4f7c804b11cd8aa7cbe00964;p=mobile-ledger.git Globals: use a thread-local per-thread date formatter/parser the DateFormat classes aren't thread-safe --- 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