How do I find out whether a locale uses 12 or 24 hour time in Java?

后端 未结 3 2007
清酒与你
清酒与你 2021-01-05 07:04

In Java, I want to print just the time of day in hours and minutes and want it to correctly switch between, e.g., \"13:00\" and \"1:00 PM\" according to the locale. How do I

3条回答
  •  情歌与酒
    2021-01-05 07:24

    The locale doesn't explicitly specify whether 12 or 24 hour time formats are preferred. Rather, locale specific date formats are handled by the locale defining the formats directly.

    • If you simply want to use the "locale preferred" time format, just call one of the three DateFormat.getTimeInstance(...) static methods, and use whatever DateFormat it returns.

    • If you have a SimpleDateFormat instance in your hands, you could (if you were prepared to do a significant amount of coding) call toPattern() and parse the resulting pattern to see if it used a 12 or 24 hour dates ... or neither. You could even tweak the pattern to use the "other" form and then call applyPattern(String) to alter the format.

提交回复
热议问题