Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect AM/PM vs 24 hr clock preference from Java Locale information?

I am looking for a way to tell in a Java program if a particular locale prefers display of time in 12 hour AM/PM or in 24 hour clock. Now, normally I would just use DateFormat to properly format the date as appropriate for the Locale. However, I'm attempting to localize a stem-and-leaf calendar display of transit schedules which requires some direct knowledge of the locale preferences. You can see an example of a schedule here:

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

Any ideas on how I can detect 12 hour AM/PM vs 24 hour programmatically?

like image 297
Brian Ferris Avatar asked Dec 17 '10 00:12

Brian Ferris


1 Answers

You can probe it, like this:

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");
}
like image 109
rrva Avatar answered Nov 10 '22 11:11

rrva