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

后端 未结 3 1981
清酒与你
清酒与你 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:12

    I have written small function which detects whether current locale uses 24 or 12 hours:

    public static boolean is24HourLocale() {
        String output = SimpleDateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
        if (output.contains(" AM") || output.contains(" PM")) {
            return false;
        } else {
            return true;
        }
    }
    

提交回复
热议问题