How to check if the system is 12 or 24 hours?

I am trying to determine if the current language value is set to 12 or 24 hours and set am / pm accordingly. This is what I got right now, but it shows am / pm all the time, regardless of whether 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, ""); } 
+46
android time locale
Dec 08 '10 at 21:37
source share
3 answers

Must not 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, ""); } 
+79
Dec 08 '10 at 23:00
source share

Best solution and sample of all code:

 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); android.text.format.DateFormat dateFormat = new android.text.format.DateFormat(); is24HourFormat= dateFormat.is24HourFormat(this); 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); } 
+7
May 24 '15 at 20:58
source share

To always display AM/PM in a 12-hour format, rather than 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)); } } 
+2
Jun 14 '17 at 6:57
source share



All Articles