Fix issue with rendering of group update timestamps.

TIL SimpleDateFormat is not thread safe.
Across instances.

God forgive them, for they know not what they did.
This commit is contained in:
Greyson Parrelli
2021-01-23 18:32:56 -05:00
parent 633f4cbbe5
commit 1746869dc3

View File

@@ -40,9 +40,9 @@ import java.util.concurrent.TimeUnit;
public class DateUtils extends android.text.format.DateUtils {
@SuppressWarnings("unused")
private static final String TAG = DateUtils.class.getSimpleName();
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
private static final SimpleDateFormat BRIEF_EXACT_FORMAT = new SimpleDateFormat();
private static final String TAG = DateUtils.class.getSimpleName();
private static final ThreadLocal<SimpleDateFormat> DATE_FORMAT = new ThreadLocal<>();
private static final ThreadLocal<SimpleDateFormat> BRIEF_EXACT_FORMAT = new ThreadLocal<>();
private static boolean isWithin(final long millis, final long span, final TimeUnit unit) {
return System.currentTimeMillis() - millis <= unit.toMillis(span);
@@ -166,7 +166,10 @@ public class DateUtils extends android.text.format.DateUtils {
}
public static boolean isSameDay(long t1, long t2) {
return DATE_FORMAT.format(new Date(t1)).equals(DATE_FORMAT.format(new Date(t2)));
String d1 = getDateFormat().format(new Date(t1));
String d2 = getDateFormat().format(new Date(t2));
return d1.equals(d2);
}
public static boolean isSameExtendedRelativeTimestamp(@NonNull Context context, @NonNull Locale locale, long t1, long t2) {
@@ -214,4 +217,28 @@ public class DateUtils extends android.text.format.DateUtils {
return -1;
}
}
@SuppressLint("SimpleDateFormat")
private static SimpleDateFormat getDateFormat() {
SimpleDateFormat format = DATE_FORMAT.get();
if (format == null) {
format = new SimpleDateFormat("yyyyMMdd");
DATE_FORMAT.set(format);
}
return format;
}
@SuppressLint("SimpleDateFormat")
private static SimpleDateFormat getBriefExactFormat() {
SimpleDateFormat format = BRIEF_EXACT_FORMAT.get();
if (format == null) {
format = new SimpleDateFormat();
BRIEF_EXACT_FORMAT.set(format);
}
return format;
}
}