Define AM / PM vs. 24-hour preferences from Java Locale information?

I am looking for a way to tell in a Java program if a particular locale prefers to display time in 12-hour AM / PM or in 24-hour hours. Now, as a rule, I would just use DateFormat to correctly format the date suitable for Locale. However, I am trying to localize the display of the calendar based on glasses and leaves of transit schedules, which requires a direct knowledge of local settings. Here you can see an example of a schedule:

http://onebusaway.org/where/standard/schedule.action?id=1_538

Any ideas on how I can detect 12-hour AM / PM and 24 hours programmatically?

+6
java internationalization localization
source share
2 answers

You can check it out, for example:

boolean hasAmPmClock(Locale locale) { DateFormat stdFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US); DateFormat localeFormat = DateFormat.getTimeInstance(DateFormat.LONG, locale); String midnight = ""; try { midnight = localeFormat.format(stdFormat.parse("12:00 AM")); } catch (ParseException ignore) { } return midnight.contains("12"); } 
+3
source share

I found another way that was slightly less circular than the accepted answer:

 DateFormat format; // ... boolean is24Hour; if (format instanceof SimpleDateFormat) { SimpleDateFormat sdf = (SimpleDateFormat)format; String pattern = sdf.toPattern(); is24Hour = !pattern.contains("a"); } 

Still pretty rude, and it won’t work with other specific DateFormat types, but the standard ways to get a DateFormat object seem to return SimpleDateFormat .

0
source share

All Articles