Date/time workarounds

pull/198/head
M66B 4 years ago
parent 5706835e42
commit 9e366b4cf6

@ -973,44 +973,52 @@ public class Helper {
} }
static DateFormat getTimeInstance(Context context) { static DateFormat getTimeInstance(Context context) {
return Helper.getTimeInstance(context, SimpleDateFormat.MEDIUM); return getTimeInstance(context, SimpleDateFormat.MEDIUM);
}
static DateFormat getDateInstance(Context context) {
return SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
} }
static DateFormat getTimeInstance(Context context, int style) { static DateFormat getTimeInstance(Context context, int style) {
if (context != null && if (context != null &&
(style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM)) { (style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM))
Locale locale = Locale.getDefault(); return new SimpleDateFormat(getTimePattern(context, style));
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context); else
String skeleton = (is24Hour ? "Hm" : "hm");
if (style == SimpleDateFormat.MEDIUM)
skeleton += "s";
String pattern = android.text.format.DateFormat.getBestDateTimePattern(locale, skeleton);
return new SimpleDateFormat(pattern, locale);
} else
return SimpleDateFormat.getTimeInstance(style); return SimpleDateFormat.getTimeInstance(style);
} }
static DateFormat getDateTimeInstance(Context context) { static DateFormat getDateInstance(Context context) {
return getDateTimeInstance(context, null); return getDateInstance(context, SimpleDateFormat.MEDIUM);
} }
static DateFormat getDateTimeInstance(Context context, Locale locale) { private static DateFormat getDateInstance(Context context, int style) {
return Helper.getDateTimeInstance(context, SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM, locale); return SimpleDateFormat.getDateInstance(style);
} }
static DateFormat getDateTimeInstance(Context context, int dateStyle, int timeStyle) { static DateFormat getDateTimeInstance(Context context) {
return getDateTimeInstance(context, dateStyle, timeStyle, null); return getDateTimeInstance(context, SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
} }
static DateFormat getDateTimeInstance(Context context, int dateStyle, int timeStyle, Locale locale) { static DateFormat getDateTimeInstance(Context context, int dateStyle, int timeStyle) {
// TODO fix time format if (context != null &&
(timeStyle == SimpleDateFormat.SHORT || timeStyle == SimpleDateFormat.MEDIUM)) {
DateFormat dateFormat = getDateInstance(context, dateStyle);
if (dateFormat instanceof SimpleDateFormat) {
String datePattern = ((SimpleDateFormat) dateFormat).toPattern();
String timePattern = getTimePattern(context, timeStyle);
return new SimpleDateFormat(datePattern + " " + timePattern);
}
}
return SimpleDateFormat.getDateTimeInstance(dateStyle, timeStyle); return SimpleDateFormat.getDateTimeInstance(dateStyle, timeStyle);
} }
private static String getTimePattern(Context context, int style) {
// https://issuetracker.google.com/issues/37054851
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context);
String skeleton = (is24Hour ? "Hm" : "hm");
if (style == SimpleDateFormat.MEDIUM)
skeleton += "s";
return android.text.format.DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
}
static CharSequence getRelativeTimeSpanString(Context context, long millis) { static CharSequence getRelativeTimeSpanString(Context context, long millis) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
long span = Math.abs(now - millis); long span = Math.abs(now - millis);

Loading…
Cancel
Save