How do I check if system is 12 or 24 hour?

AndroidTimeLocale

Android Problem Overview


I am trying to determine if the current locale is set to 12 or 24 hour, and set am/pm accordingly. This is what I have got right now, but it shows am/pm all the time regardless if it is set to 24.

if (DateFormat.is24HourFormat(this))
{
	mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
	int hourOfDay = mHour;
	if (hourOfDay>12)
		views.setTextViewText(R.id.AMPM, "pm");
	if (hourOfDay==12)
		views.setTextViewText(R.id.AMPM, "pm");
	if (hourOfDay<12)
		views.setTextViewText(R.id.AMPM, "am");
}
else {
	views.setTextViewText(R.id.AMPM, "");        	
}

Android Solutions


Solution 1 - Android

Shouldn't it be

if (!DateFormat.is24HourFormat(this))

You want to assign am/pm only when it is not set to 24 hour format, right?

Here is a more compact version:

if (!DateFormat.is24HourFormat(this)) {
    mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
    int hourOfDay = mHour;
    if (hourOfDay >= 12) {
        views.setTextViewText(R.id.AMPM, "pm");
    } else {
        views.setTextViewText(R.id.AMPM, "am");
    }
} else {
    views.setTextViewText(R.id.AMPM, "");
}

Solution 2 - Android

Better solution and whole code sample:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());

// below, the "hour" and "min" are variables,to which you want to set 
//calendar.For example you can take this values from time picker
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);            
                
is24HourFormat = android.text.format.DateFormat.is24HourFormat(context);
                    
if (is24HourFormat) {
    // if system uses 24hourformat dateFormat will format the calender time according to 24hourformat. "HH" means 24hourformat
    CharSequence setTime = dateFormat.format("HH:mm", calendar);

} else {
    // if system doesnt use 24hourformat dateFormat will format the calender time according to 12hourformat. "hh" means 12hourformat and "a" will show am/pm marker
    CharSequence setTime = dateFormat.format("hh:mm a", calendar);
}

Solution 3 - Android

To always display AM/PM in 12-hour format and not something like vorm/nachm in german, use Locale.US for the date format:

/**
 * Returns the time in a localized format. The 12-hours format is always displayed with
 * AM/PM (and not for example vorm/nachm in german).
 *
 * @param context the context
 * @return Localized time (15:24 or 3:24 PM).
 */
public static String getTime(Context context, long time) {
    if (android.text.format.DateFormat.is24HourFormat(context)) {
        return new SimpleDateFormat("HH:mm", Locale.US).format(new Date(time));

    } else {
        return new SimpleDateFormat("hh:mm a", Locale.US).format(new Date(time));
    }
}

Solution 4 - Android

Might not be relevant to you, but I found that despite doing the

DateFormat.is24HourFormat(this)

I still got 12 hour version in the TextView. After a couple days of playing around, I discovered that certain time regions, like Indian Time Zone (+5:30 GMT), despite setting 24 hour format to ON, still force 12 hour format. Changing that to Adak (GMT 09:00)/ United States Time Zone fixed it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontristan202View Question on Stackoverflow
Solution 1 - AndroidEliseoView Answer on Stackoverflow
Solution 2 - AndroidErenView Answer on Stackoverflow
Solution 3 - AndroidmboView Answer on Stackoverflow
Solution 4 - AndroidzombiesauceView Answer on Stackoverflow