Improve date localization for API >= 18 (4.3, JELLY_BEAN_MR2)

Fixes #3102
Closes #4597
This commit is contained in:
Geonu Kang
2015-11-19 15:32:33 +09:00
committed by Moxie Marlinspike
parent e4e8511b4c
commit c4f9bc2b5e
2 changed files with 15 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.os.Build;
import android.text.format.DateFormat;
import java.text.SimpleDateFormat;
@@ -41,7 +42,7 @@ public class DateUtils extends android.text.format.DateUtils {
}
private static String getFormattedDateTime(long time, String template, Locale locale) {
String localizedPattern = new SimpleDateFormat(template, locale).toLocalizedPattern();
final String localizedPattern = getLocalizedPattern(template, locale);
return new SimpleDateFormat(localizedPattern, locale).format(new Date(time));
}
@@ -86,12 +87,19 @@ public class DateUtils extends android.text.format.DateUtils {
String dateFormatPattern;
if (DateFormat.is24HourFormat(context)) {
dateFormatPattern = "MMM d, yyyy HH:mm:ss zzz";
dateFormatPattern = getLocalizedPattern("MMM d, yyyy HH:mm:ss zzz", locale);
} else {
dateFormatPattern = "MMM d, yyyy hh:mm:ss a zzz";
dateFormatPattern = getLocalizedPattern("MMM d, yyyy hh:mm:ss a zzz", locale);
}
return new SimpleDateFormat(dateFormatPattern, locale);
}
private static String getLocalizedPattern(String template, Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return DateFormat.getBestDateTimePattern(locale, template);
} else {
return new SimpleDateFormat(template, locale).toLocalizedPattern();
}
}
}