If you have specified a pattern in SimpleDateFormat
then you have fixed the 12/24-hour mode, either 12-hour-mode in case of pattern symbol "h" (1-12) or 24-hour-mode in case of pattern symbol "H" (0-23). The alternatives "k" and "K" are similar with slightly different ranges.
That said, specifying a pattern makes your format independent from device setting!
The alternative would be to use DateFormat.getDateTimeInstance() which makes the time style dependent on system locale (if Locale.getDefault()
might change - or you have to deploy a mechanism how to ask the current device locale and then to set in Android-Java Locale.setDefault()
).
Another idea specific for Android is to ask directly the system settings using the string constant TIME_12_24 and then to specify a pattern dependent on this setting. This also seems to be possible by special method DateFormat.is24HourFormat() (note for your attention that Android has TWO different classes with name DateFormat
). Concrete example for this approach:
boolean twentyFourHourStyle =
android.text.format.DateFormat.is24HourFormat((Context) this);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (df instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) df).toPattern();
if (twentyFourHourStyle) {
df = new SimpleDateFormat(pattern.replace("h", "H"), Locale.getDefault());
} else {
df = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
}
} else {
// nothing to do or change
}
You are of course free to refine the code for possible occurrences of k and K or watch out for use of literals h and H (then parse for apostrophs to ignore such parts in replace-method).