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?
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With