I am a French Android developer, so using Locale.getDefault()
causes my DateFormat
to use a 24-hour mode. But, when I set manually my device to 12-hour mode via the setting menu, DateFormat
keeps going in a 24-hour format.
On the contrary, TimePicker
s are set according to my own 12/24-hour setting.
Is there any way to make DateFormat
s behave the same way as TimePicker
s ?
EDIT:
Here is my DateFormat
declaration:
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
And here is where I set my TimePicker
to 12 or 24-hour mode.
tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat((Context) this));
My Solution:
According to @Meno Hochschild's answer below, here is how I solved this tricky problem:
boolean is24hour = android.text.format.DateFormat.is24HourFormat((Context) this);
tp.setIs24HourView(is24hour); // tp is the TimePicker
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (timeFormat instanceof SimpleDateFormat) {
String pattern = ((SimpleDateFormat) timeFormat).toPattern();
if (is24hour) {
timeFormat = new SimpleDateFormat(pattern.replace("h", "H").replace(" a",""), Locale.getDefault());
}
else {
timeFormat = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
}
}
After this, timeFormat
will correctly format dates whether your device is set to display times in a 24-hour format or in a 12-hour one. And the TimePicker
will be correctly set too.
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).
The Original Solution, doesn't work well always. My solution is easier and elegant:
Locale.US By default is 12 hours, then we set as 12 hours.
We check if the user has checked 24 hours format, then we set a Locale with 24 hours as default.
boolean twentyFourHourStyle = android.text.format.DateFormat.is24HourFormat(context);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
if (twentyFourHourStyle) {
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,Locale.FRANCE);
}
Solved with 3 lines ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With