Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timepicker: when to choose 24 or am/pm modes

Tags:

android

I have a few timepickers in my app.

In my country (Spain) we are used to displaying time in 24 hours mode... but in other countries are used to am/pm.

I know how to set a Timepicker to 24 or am/pm mode... But what is the best approach to show am/pm or 24 depending of the device locale or country? How can I know to select one or another mode?

Thank you very much

(Sorry for my poor english)

like image 686
Javi Moya Avatar asked Aug 04 '11 11:08

Javi Moya


2 Answers

Use DateFormat from android.text.format.DateFormat: http://developer.android.com/reference/android/text/format/DateFormat.html

As can be seen from sources (frameworks/base/core/java/android/text/format/DateFormat.java) function DateFormat.is24HourFormat() does much more than just asking for TIME_12_24 from Settings.System.

As I understand DateFormat.is24HourFormat() is a more reliable way to get users time format. For example, because Settings.System.getString() can return null for TIME_12_24 (as seen from DateFormat.java).

like image 79
wonder.mice Avatar answered Nov 20 '22 18:11

wonder.mice


There are several ways to do this, of which the first is to just use the device settings and thus let the user make this decision, but be aware that this can return a null value in some cases and is therefore not as reliable;

String clockType = android.provider.Settings.System.getString(context.getContentResolver(), android.provider.Settings.System.TIME_12_24);

A better solution is given by wonder.mice, which is using:

boolean is24HourFormat = DateFormat.is24HourFormat();

see also the reference page: http://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat(android.content.Context)

like image 40
MrJre Avatar answered Nov 20 '22 18:11

MrJre